c#使用Dataset读取XML文件动态生成菜单的方法
程序员文章站
2022-06-21 20:31:25
本文实例讲述了c#使用dataset读取xml文件动态生成菜单的方法。分享给大家供大家参考。具体实现方法如下:
step 1:form1 上添加一个toolstripco...
本文实例讲述了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#程序设计有所帮助。
上一篇: 使用C#编写自己的区块链挖矿算法