基本信息
源码名称:C# winform动态生成菜单示例源码
源码大小:0.07M
文件格式:.rar
开发语言:C#
更新时间:2018-01-02
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559
本次赞助数额为: 4 元×
微信扫码支付:4 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
namespace DemoMenu
{
public partial class FrmMain : Form
{
public FrmMain()
{
InitializeComponent();
}
List<Modul> modulList;
private void FrmMain_Load(object sender, EventArgs e)
{
modulList = new List<Modul>
{
new Modul{ModulId=1,ModulName="用户管理",FormName="",ParentId=0},
new Modul{ModulId=2,ModulName="角色管理",FormName="",ParentId=0},
new Modul{ModulId=3,ModulName="用户查询",FormName="",ParentId=1},
new Modul{ModulId=4,ModulName="根据ID查询",FormName="",ParentId=3},
new Modul{ModulId=5,ModulName="根据Name查询",FormName="",ParentId=3},
new Modul{ModulId=6,ModulName="角色添加",FormName="FrmAddRole",ParentId=2},
new Modul{ModulId=7,ModulName="角色修改",FormName="FrmUpdateRole",ParentId=2}
};
AddTopMenu();
}
//先添加第一级菜单
private void AddTopMenu()
{
foreach (var item in modulList)
{
if (item.ParentId==0)
{
//创建菜单项
ToolStripMenuItem menuItem = new ToolStripMenuItem();
menuItem.Name = "Menu" item.ModulId;
menuItem.Text = item.ModulName;
//添加子菜单
AddChildMenu(item.ModulId, menuItem);
this.menuStrip1.Items.Add(menuItem);
}
}
}
//添加子菜单
private void AddChildMenu(int parentId,ToolStripMenuItem menuItem)
{
//先找到该子菜单
foreach (var item in modulList)
{
if (item.ParentId==parentId)
{
//创建子菜单
ToolStripMenuItem childItem = new ToolStripMenuItem();
childItem.Name = "Menu" item.ModulId;
childItem.Text = item.ModulName;
childItem.Tag = item.FormName;
//添加事件
if (item.FormName!="")
{
childItem.Click =new EventHandler(childItem_Click);
}
//添加子菜单
menuItem.DropDownItems.Add(childItem);
//递归添加子菜单的子菜单
AddChildMenu(item.ModulId, childItem);
}
}
}
//点击事件 处理函数
private void childItem_Click(object sender, EventArgs e)
{
//获取窗体名
string formName = (sender as ToolStripMenuItem).Tag.ToString();
//打开窗口 (反射)
Type type = Type.GetType("DemoMenu." formName);
dynamic form=Activator.CreateInstance(type);
form.ShowDialog();
}
}
}