本文实例讲述了c#使用Dataset读取XML文件动态生成菜单的方法。分享给大家供大家参考。具体实现方法如下:
Step 1:Form1 上添加一个ToolStripContainer控件
Step2:实现代码
private void Form2_Load(object sender, EventArgs e){ CMenuEx menu = new CMenuEx(); string sPath = "D://Menu.xml";//xml的内容 if (menu.FileExit()) { menu.LoadAllMenu(sPath, toolStripContainer1); //读取xml来加载菜单 } else { MessageBox.Show("XML文件加载失败!"); }}/// <summary>/// 菜单读取类/// </summary>public class CMenuEx{ private string _Path; /// <summary> /// 设置XML配置文件路径 /// </summary> public string Path { get { return _Path; } set { _Path = value; } } /// <summary> /// 判断文件是否存在 /// </summary> /// <returns>文件是否存在</returns> public bool FileExit() { if (File.Exists(_Path)) { return true; } else return false; } /// <summary> /// 加载菜单 /// </summary> /// <param name="menuStrip">母菜单对象</param> public void LoadAllMenu(string sXmlPath, ToolStripContainer pToolStripContainer) { DataSet ds = new DataSet(); ds.ReadXml(sXmlPath, XmlReadMode.Auto); string ToolStripPanelType = "TopToolStripPanel"; //查找所有最初一级的菜单 DataView dvMenuOptions = new DataView(ds.Tables["MenuOptions"], "ParentLevel=ID and ToolStripPanelType='" + ToolStripPanelType + "'", "DisplayOrder Asc", DataViewRowState.CurrentRows); string sParentLevel = ""; ToolStripPanel tspTop = pToolStripContainer.TopToolStripPanel; tspTop.Dock = DockStyle.Top; ToolStrip tsTop = new ToolStrip(); tspTop.Join(tsTop); //绑定ToolStrip foreach (DataRowView rvMain in dvMenuOptions) //循环得到主菜单 { sParentLevel = rvMain["ParentLevel"].ToString(); ToolStripMenuItem tsItemParent = new ToolStripMenuItem(); tsItemParent.Text = rvMain["Text"].ToString(); tsItemParent.Name = rvMain["Name"].ToString(); tsTop.Items.Add(tsItemParent);//添加父菜单 //查找父菜单下的所有子菜单 DataView dvSub = new DataView(ds.Tables["MenuOptions"], "ParentLevel<>ID and ParentLevel='" + sParentLevel + "'", "DisplayOrder", DataViewRowState.CurrentRows); foreach (DataRowView rvSub in dvSub) { ToolStripMenuItem itemSub = new ToolStripMenuItem(); itemSub.Text = rvSub["Text"].ToString() + " " + rvSub["ShortCutKeys"].ToString(); //为菜单添加单击事件 itemSub.Click += new EventHandler(toolSubItem_Click); //菜单响应函数 itemSub.Name = rvSub["Method"].ToString(); tsItemParent.DropDownItems.Add(itemSub); } } } //自定义消息响应函数 void toolSubItem_Click(object sender, EventArgs e) { //创建菜单调用方法类的实例 MenuMethod menuMethod = new MenuMethod(); Type type = menuMethod.GetType(); //动态获取方法对象 MethodInfo mi = type.GetMethod(((ToolStripMenuItem)sender).Name); //调用指定方法 if (mi != null) { mi.Invoke(menuMethod, null); } } /// <summary> /// 菜单的方法列表类 /// </summary> class MenuMethod { public void New() { MessageBox.Show("New"); } public void Open() { MessageBox.Show("Open"); } }}
附:xml内容:
<?xml version="1.0" encoding="GB2312" ?><Menus> <MenuOptions> <ID>3766e9a2-7955-44eb-ad87-91ccb798baa7</ID> <ParentLevel>3766e9a2-7955-44eb-ad87-91ccb798baa7</ParentLevel> <DisplayOrder>1</DisplayOrder> <ToolBarItemType>ToolStripButton</ToolBarItemType> <ToolStripItemDisplayStyle>ImageAndText</ToolStripItemDisplayStyle> <ToolStripPanelType>TopToolStripPanel</ToolStripPanelType> <ToolStripDisplayPosition>1</ToolStripDisplayPosition> <TDTVisible>True</TDTVisible> <ShortCutKeys /> <Text>文档工具栏</Text> <Name>DocTool</Name> <Image /> <Expression /> <Assembly /> </MenuOptions> <MenuOptions> <ID>fd75638f-6c10-473d-b6e6-bdfd2c7931d6</ID> <ParentLevel>3766e9a2-7955-44eb-ad87-91ccb798baa7</ParentLevel> <DisplayOrder>0</DisplayOrder> <ToolBarItemType>ToolStripButton</ToolBarItemType> <ToolStripItemDisplayStyle>Image</ToolStripItemDisplayStyle> <ToolStripPanelType /> <ToolStripDisplayPosition /> <TDTVisible>True</TDTVisible> <ShortCutKeys>Ctrl+N</ShortCutKeys> <Text>新建地图文档</Text> <Method>New</Method> <Image>/img/New.ico</Image> <Expression /> <Assembly /> </MenuOptions> <MenuOptions> <ID>9c6238d5-b47d-4b08-933c-ea7c74f6b586</ID> <ParentLevel>3766e9a2-7955-44eb-ad87-91ccb798baa7</ParentLevel> <DisplayOrder>1</DisplayOrder> <ToolBarItemType>ToolStripButton</ToolBarItemType> <ToolStripItemDisplayStyle>Image</ToolStripItemDisplayStyle> <ToolStripPanelType /> <ToolStripDisplayPosition /> <TDTVisible>True</TDTVisible> <ShortCutKeys>Ctrl+O</ShortCutKeys> <Text>打开文档</Text> <Method>Open</Method> <Image>/ico/open.ico</Image> <Expression>Com.Linjon.ArcGIS.PlugIn.File.OpenDocCmd</Expression> <Assembly>Com.Linjon.ArcGIS.PlugIn.dll</Assembly> </MenuOptions></Menus>
希望本文所述对大家的C#程序设计有所帮助。
新闻热点
疑难解答