ITPub博客

首页 > Linux操作系统 > Linux操作系统 > C# TreeView菜单,MenuStrip菜单递归动态生成例子

C# TreeView菜单,MenuStrip菜单递归动态生成例子

原创 Linux操作系统 作者:iDotNetSpace 时间:2009-04-15 15:08:17 0 删除 编辑

这是在权限系统中设计中常用到的两种菜单处理方式

    private DataTable ModuleTable;

 
private void FrmMain_Load(object sender, EventArgs e)
        {
   
                
try
                {
                    InitMenu();
                }
                
catch
                {
                    MenuTree.Nodes.Clear();
                }



    
/// 
        
/// 初始化菜单
        
/// 

        protected void InitMenu()
        {
            DataSet ds
= new DataSet();
            ds
=bll.GetModuleTypeList("");
            RGP.BLL.RGP_Modules bll 
= new RGP.BLL.RGP_Modules();
            ModuleTable 
= ds.Tables[0];  //取得所有数据得到DataTable 

            
//加载TreeView菜单
            LoadNode(MenuTree.Nodes, "0");

            
//加载MenuStrip菜单
            ToolStripMenuItem topMenu = new ToolStripMenuItem();
            LoadSubMenu(
ref topMenu, 0);

        }

        
/// 
        
/// 递归创建TreeView菜单(模块列表)
        
/// 

        
/// 子菜单
        
/// 子菜单上级ID
        protected void LoadNode(TreeNodeCollection node, string MtID)
        {
            DataView dvList 
= new DataView(ModuleTable);
            dvList.RowFilter 
= "ModuleTypeSuperiorID=" + MtID;  //过滤父节点 
            TreeNode nodeTemp;
            
foreach (DataRowView dv in dvList)
            {
                nodeTemp 
= new TreeNode();
                nodeTemp.Tag 
= dv["ModuleTypeID"].ToString() + ",0";
                nodeTemp.Text 
= dv["ModuleTypeName"].ToString();  //节点名称                 
                node.Add(nodeTemp);  //加入节点 
                
//nodeTemp.SelectAction = TreeNodeSelectAction.Expand;
                
//nodeTemp.Expanded = false;

                
this.LoadNode(nodeTemp.Nodes, nodeTemp.Tag.ToString().Split(',')[0]);  //递归 

                
//增加子模块分类
                RGP.BLL.RGP_Modules bll = new RGP.BLL.RGP_Modules();
                DataSet Module 
= bll.GetModuleList("ModuleTypeID=" + dv["ModuleTypeID"].ToString());
                
int i = 0;
                
foreach (DataRow child_dr in Module.Tables[0].Rows)
                {
                    
if ((UserBox.GetUserTable().IsLimit == true
                            
|| (child_dr["ModuleDisabled"].ToString().ToLower() == "true"
                            
&& UserHandle.ValidationModule(int.Parse(child_dr["ModuleID"].ToString()), RGP_Tag.Browse)))
                            
&& child_dr["IsMenu"].ToString().ToLower() == "true")
                    {
                        TreeNode ChildNode 
= new TreeNode(child_dr["ModuleName"].ToString());
                        ChildNode.Tag 
= child_dr["ModuleID"].ToString() + ",1";
                        ChildNode.Expand();
                        nodeTemp.Nodes.Add(ChildNode);
                        i
++;
                    }
                }

                
//删除不必要的模块分类节点。
                if (i == 0)
                {
                    MenuTree.Nodes.Remove(nodeTemp);
                }
            }
        }

        
/// 
        
/// 递归创建MenuStrip菜单(模块列表)
        
/// 

        
/// 父菜单项
        
/// 父菜单的ID
        private void LoadSubMenu(ref ToolStripMenuItem topMenu, int MtID)
        {
            DataView dvList 
= new DataView(ModuleTable);
            
//过滤出当前父菜单下在所有子菜单数据(仅为下一层的)
            dvList.RowFilter = "ModuleTypeSuperiorID=" + MtID.ToString();
            ToolStripMenuItem subMenu;
            
foreach (DataRowView dv in dvList)
            {
                
//创建子菜单项
                subMenu = new ToolStripMenuItem();
                subMenu.Text 
= dv["ModuleTypeName"].ToString();
                
//判断是否为顶级菜单
                if (MtID == 0)
                {
                    
this.SysMainMenu.Items.Add(subMenu);
                }
                
else
                {
                    topMenu.DropDownItems.Add(subMenu);
                }

                
//递归调用
                LoadSubMenu(ref subMenu, Convert.ToInt32(dv["ModuleTypeID"]));

                
//增加子模块分类
                RGP.BLL.RGP_Modules bll = new RGP.BLL.RGP_Modules();
                DataSet Module 
= bll.GetModuleList("ModuleTypeID=" + dv["ModuleTypeID"].ToString());
                
int i = 0;
                
foreach (DataRow child_dr in Module.Tables[0].Rows)
                {
                    
if ((UserBox.GetUserTable().IsLimit == true
                            
|| (child_dr["ModuleDisabled"].ToString().ToLower() == "true"
                            
&& UserHandle.ValidationModule(int.Parse(child_dr["ModuleID"].ToString()), RGP_Tag.Browse)))
                            
&& child_dr["IsMenu"].ToString().ToLower() == "true")
                    {
                        ToolStripMenuItem ChildMenu 
= new ToolStripMenuItem(child_dr["ModuleName"].ToString());
                        
subMenu.DropDownItems.Add(ChildMenu);
                        i
++;
                    }
                }


                
//删除不必要的模块分类节点。
                if (i == 0)
                {
                    
this.SysMainMenu.Items.Remove(subMenu);
                }
            }
        }

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/12639172/viewspace-589269/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2008-01-04

  • 博文量
    2376
  • 访问量
    5315235