本文实例讲述了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文件加载失败!"); }
}
///
/// 菜单读取类
///
public class CMenuEx
{
private string _Path;
///
/// 设置XML配置文件路径
///
public string Path
{
get { return _Path; }
set { _Path = value; }
}
///
/// 判断文件是否存在
///
/// 文件是否存在
public bool FileExit()
{
if (File.Exists(_Path))
{ return true; }
else return false;
}
///
/// 加载菜单
///
/// 母菜单对象
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);
}
}
///
/// 菜单的方法列表类
///
class MenuMethod
{
public void New()
{
MessageBox.Show("New");
}
public void Open()
{
MessageBox.Show("Open");
}
}
}
附:xml内容:
3766e9a2-7955-44eb-ad87-91ccb798baa7 3766e9a2-7955-44eb-ad87-91ccb798baa7 1 ToolStripButton ImageAndText TopToolStripPanel 1 True 文档工具栏 DocTool fd75638f-6c10-473d-b6e6-bdfd2c7931d6 3766e9a2-7955-44eb-ad87-91ccb798baa7 0 ToolStripButton Image True Ctrl+N 新建地图文档 New /img/New.ico 9c6238d5-b47d-4b08-933c-ea7c74f6b586 3766e9a2-7955-44eb-ad87-91ccb798baa7 1 ToolStripButton Image True Ctrl+O 打开文档 Open /ico/open.ico Com.Linjon.ArcGIS.PlugIn.File.OpenDocCmd Com.Linjon.ArcGIS.PlugIn.dll
希望本文所述对大家的C#程序设计有所帮助。



