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>

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值