本文实例讲述了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#程序设计有所帮助。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。