• WINFORM权限系统开发系列教程(七)菜单管理模块


    菜单列表

     代码

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using Xwy.WindowsFormsApp.Common;
    using Xwy.WindowsFormsApp.DAL;
    using Xwy.WindowsFormsApp.FModels;
    using Xwy.WindowsFormsApp.Models;
    
    namespace Xwy.WindowsFormsApp.sm
    {
        public partial class FrmMenuList : Form
        {
            public FrmMenuList()
            {
                InitializeComponent();
            }
            MenuDAL menuDAL = new MenuDAL();
    
            private void FrmMenuList_Load(object sender, EventArgs e)
            {
                LoadCboParents();
                LoadMenuList();
            }
    
            /// <summary>
            /// 加载菜单列表
            /// </summary>
            private void LoadMenuList()
            {
                int selMenuId = cboParents.SelectedValue.ToString().GetInt();
                string mName = txtMenuName.Text.Trim();
                List<MenuInfoAllModel> menuList = menuDAL.GetMenuList(selMenuId, mName);
                dgvMenus.AutoGenerateColumns = false;
                dgvMenus.DataSource = menuList;
    
            }
    
            /// <summary>
            /// 下拉框绑定
            /// </summary>
            private void LoadCboParents()
            {
                DataTable dtParent = menuDAL.GetParentList();
                DataRow dr = dtParent.NewRow();
                dr["ParentId"] = 0;
                dr["MenuName"] = "请选择";
                //dtParent.Rows.Add(dr);//表的末尾添加
                dtParent.Rows.InsertAt(dr, 0);//插入到第一行
                cboParents.DataSource = dtParent;
                cboParents.DisplayMember = "MenuName";
                cboParents.ValueMember = "ParentId";
            }
    
            private void btnSearch_Click(object sender, EventArgs e)
            {
                LoadMenuList();
            }
    
            private void btnAdd_Click(object sender, EventArgs e)
            {
                ShowMenuInfoPage(0);
            }
    
            private void ShowMenuInfoPage(int menuId)
            {
                FrmMenuInfo frmMenuInfo = new FrmMenuInfo();
                frmMenuInfo.MdiParent = this.MdiParent;
                frmMenuInfo.Tag = new FInfoModel
                {
                    FId = menuId,
                    ReloadList = LoadMenuList
                };
                frmMenuInfo.Show();
            }
    
            private void dgvMenus_CellContentClick(object sender, DataGridViewCellEventArgs e)
            {
                var currCell = dgvMenus.Rows[e.RowIndex].Cells[e.ColumnIndex] as DataGridViewLinkCell;
                string cellValue = currCell.FormattedValue.ToString().Trim();
                MenuInfoAllModel menuInfo = dgvMenus.Rows[e.RowIndex].DataBoundItem as MenuInfoAllModel;
                switch (cellValue)
                {
                    case "修改":
                        ShowMenuInfoPage(menuInfo.MenuId);
                        break;
                    case "删除":
                        //提示
                        if (MsgBoxHelper.MsgBoxConfirm("删除菜单", "您确定要删除该条菜单数据吗?删除菜单数据会同角色菜单关系数据一并删除?") == DialogResult.OK)
                        {
                            //调用删除方法
                            bool blDel=menuDAL.DeleteMenu(menuInfo.MenuId);
                            if(blDel)
                            {
                                MsgBoxHelper.MsgBoxShow("成功提示",$"菜单:{menuInfo.MenuName} 信息删除成功!");
                                LoadMenuList();
                            }
                            else
                            {
                                MsgBoxHelper.MsgErrorShow( $"菜单:{menuInfo.MenuName} 信息删除失败!");
                            }
                        }
                        break;
                }
            }
        }
    }

    菜单信息窗口

     代码

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using Xwy.WindowsFormsApp.Common;
    using Xwy.WindowsFormsApp.DAL;
    using Xwy.WindowsFormsApp.FModels;
    using Xwy.WindowsFormsApp.Models;
    
    namespace Xwy.WindowsFormsApp.sm
    {
        public partial class FrmMenuInfo : Form
        {
            public FrmMenuInfo()
            {
                InitializeComponent();
            }
            MenuDAL menuDAL = new MenuDAL();
            FInfoModel fModel=null;
            int menuId=0;
            string oldName = "";//修改前的名称
            private void FrmMenuInfo_Load(object sender, EventArgs e)
            {
                LoadCboParents();//加载父级下拉框
                LoadCboFrmNames();//加载关联页面下拉框
                if (this.Tag != null)
                {
                    fModel = this.Tag as FInfoModel;
                    if (fModel != null)
                    {
                        menuId = fModel.FId;
                    }
                }
                if (menuId > 0)//修改页面加载
                {
                    //加载菜单信息
                    MenuInfoModel menuInfo = menuDAL.GetMenuInfoById(menuId);
                    if (menuInfo != null)
                    {
                        txtMenuName.Text = menuInfo.MenuName;
                        oldName = menuInfo.MenuName;
                        txtMKey.Text = menuInfo.MKey;
                        cboParent.SelectedValue = menuInfo.ParentId;
                        if (!string.IsNullOrEmpty(menuInfo.FrmName))
                        {
                            cboFrmName.SelectedValue = menuInfo.FrmName;
                        }
    
                    }
                }
                else
                {
                    txtMenuName.Clear();
                    txtMKey.Clear();
                }
            }
    
            private void LoadCboFrmNames()
            {
                DataTable dtParent = menuDAL.GetAllMenu();
                DataRow dr=dtParent.NewRow();
                dr["MenuId"] = "0";
                dr["MenuName"] = "请选择";
                dtParent.Rows.InsertAt(dr, 0);
                cboParent.DataSource = dtParent;
                cboParent.DisplayMember = "MenuName";
                cboParent.ValueMember = "MenuId";
    
            }
    
            private void LoadCboParents()
            {
                string assName = this.GetType().Assembly.GetName().Name;
                Type[] types=this.GetType().Assembly.GetTypes();
                List<FormInfo> fList = new List<FormInfo>();
                foreach (Type t in types)
                {
                    string tName = t.BaseType.Name;
                    if (tName == "Form")
                    {
                        FormInfo fi = new FormInfo();
                        Form f=(Form)Activator.CreateInstance(t);
                        fi.FName = t.FullName.Substring(assName.Length+1);
                        fi.FText = f.Text;
                        fList.Add(fi);
                        f.Dispose();
                    }
                }
                FormInfo fi0 = new FormInfo()
                {
                    FName = "",
                    FText = "请选择"
                };
                fList.Insert(0, fi0);
                cboFrmName.DataSource = fList;
                cboFrmName.DisplayMember = "FText";
                cboFrmName.ValueMember = "FName";
                cboFrmName.SelectedIndex = 0;
    
            }
    
            private void btnOk_Click(object sender, EventArgs e)
            {
                //1 接收页面信息输入
                string mName = txtMenuName.Text.Trim();
                int parentId = cboParent.SelectedValue.GetInt();
                string frmName = cboFrmName.SelectedValue.ToString();
                string mKey = txtMKey.Text.Trim();
    
    
                //2 菜单名称是否为空
                if (string.IsNullOrEmpty(mName))
                {
                    MsgBoxHelper.MsgErrorShow("菜单名称不能为空");
                    txtMenuName.Focus();
                    return;
                }
    
                //3 不能重名(判断是否存在)
                if (menuId == 0 || (menuId > 0 && oldName != mName))
                {
                    //判断菜单名是否已经存在
                    if (menuDAL.ExistMenuName(mName))
                    {
                        MsgBoxHelper.MsgErrorShow("菜单名称已存在");
                        txtMenuName.Focus();
                        return;
                    }
                }
    
                //4 封装信息
                MenuInfoModel menuInfo = new MenuInfoModel()
                { 
                    MenuName=mName,
                    ParentId=parentId,
                    FrmName=frmName,
                    MKey=mKey           
                };
    
                bool bl = false;
                //5 执行添加、修改
                if (menuId == 0)
                {
                    //执行添加操作
                    bl = menuDAL.AddMenuInfo(menuInfo);
                }
                else
                {
                    //执行修改操作
                    menuInfo.MenuId = menuId;
                    bl = menuDAL.UpdateMenuInfo(menuInfo);
    
                }
                string actMsg = menuId > 0 ? "修改" : "添加";
                if (bl)
                {
                    MsgBoxHelper.MsgBoxShow($"{actMsg}菜单", $"菜单:{mName}信息{actMsg}成功");
                    //刷新列表数据
                    fModel.ReloadList?.Invoke();
                }
                else
                {
                    MsgBoxHelper.MsgErrorShow($"菜单:{mName}信息{actMsg}失败");
                }
            }
    
            private void btnClose_Click(object sender, EventArgs e)
            {
                this.Close();
            }
        }
    }
  • 相关阅读:
    Codevs1684 垃圾陷阱
    Codevs1540银河英雄传说[并查集]
    Poj1182食物链[并查集]
    树的顺序遍历的应用
    树的顺序遍历
    ARTS打卡
    定位iOS代码中崩溃的位置
    leetcode 24
    leetcode 24
    Drafter简单介绍
  • 原文地址:https://www.cnblogs.com/xiewenyu/p/13083623.html
Copyright © 2020-2023  润新知