动态加载菜单

动态加载菜单的源代码(根据用户的权限动态加载功能菜单)

private   void  button1_Click( object  sender, System.EventArgs e)
        
{
            
this.mainMenu1.MenuItems.Clear();

            
int parentID,i,parentID1;

            System.Data.SqlClient.SqlConnection connect 
= new System.Data.SqlClient.SqlConnection("workstation id=TXCHEN;packet size=4096;user id=sa;data source=TXCHEN;persist security info=False;initial catalog=cmis");
            System.Data.SqlClient.SqlDataAdapter  da 
= new System.Data.SqlClient.SqlDataAdapter("SELECT GroupID, OrderNo, MenuID, MenuName, FormName, Arguments, isnull(ParentID,0)ParentID FROM UserGroupMenus where GroupID="+(1).ToString() +" order by LevelID,ParentID,OrderNo",connect);
            System.Data.DataSet ds 
= new DataSet();

            da.Fill(ds);

            System.Data.DataTable dt 
= ds.Tables[0];
            MenuItemX menuItem,parentMenuItem
=null ;
            parentID 
= 0;
            parentID1 
= 0;
            
for(i=0;i<dt.Rows.Count;i++)
            
{
                parentID 
= Convert.ToInt32(dt.Rows[i]["ParentID"]);
                menuItem 
= new MenuItemX();
                menuItem.Text 
= dt.Rows[i]["MenuName"].ToString();
                menuItem.MenuItemID 
= Convert.ToInt32(dt.Rows[i]["MenuID"]);
                menuItem.FormName 
= dt.Rows[i]["FormName"].ToString();
                menuItem.FormArguments 
= dt.Rows[i]["Arguments"].ToString();
                menuItem.Click 
+= new EventHandler(menuItem_Click);
                
                
if (parentID1 == parentID)//上次菜单的上级ID=本菜单的上级ID;
                {
                    
if (parentID==0//如果是根级菜单
                    {
                        
this.mainMenu1.MenuItems.Add(menuItem);
                        }

                    
else
                    
{
                        parentMenuItem.MenuItems.Add(menuItem);
                    }

                }

                
else
                
{
                    parentMenuItem 
= getMenuItem(this.mainMenu1 , parentID);//找父级菜单项

                    
if( parentMenuItem != null )
                    
{
                        parentMenuItem.MenuItems.Add(menuItem);
                    }

                }

                parentID1 
= parentID;
            }

        }

        
private   MenuItemX getMenuItem(System.Windows.Forms.Menu parentMenu, int  parentID)
        
{
            MenuItemX menuItem
= null;
            
int i ;
            
if (parentMenu.IsParent)
            
{
                
for(i=0;i<parentMenu.MenuItems.Count;i++)
                
{
                    
if( ((MenuItemX)(parentMenu.MenuItems[i])).MenuItemID == parentID)//在子菜单项中找到
                    {
                        menuItem 
= (MenuItemX)parentMenu.MenuItems[i];
                        
break;
                    }

                    
else
                    
{
                        menuItem 
= getMenuItem(parentMenu.MenuItems[i],parentID);//递归子菜单项
                        if( menuItem != null)//找到则退出循环
                        {
                            
break;
                        }

                    }

                }

            }

            
else
            
{
                
return menuItem;
            }


            
return menuItem;
        
        }

        
private   void  menuItem_Click( object  sender, System.EventArgs e)
        
{
            MenuItemX menuItem 
= (MenuItemX)sender;
            
int menuItemID = menuItem.MenuItemID;
            
string formName = menuItem.FormName;
            
string[] formArg = null ;
            
if( menuItem.FormArguments.Length>0)
            
{
                formArg 
= new string[1] ;
                formArg[
0=menuItem.FormArguments ;
            }


            
this.openForm(formName,formArg);

        }

        
private   void  openForm( string  formName, object [] formArgs)
        
{
            Form f 
= null;
            
if( formName.Length > 0 )
                formName 
= "cash."+formName;
            
else
            
{
                MessageBox.Show(
"该功能没有定义!");
                
return;
            }

            
try
            
{
                Type Dlg 
= Type.GetType(formName);
                f 
= (Form)Dlg.InvokeMember(null, System.Reflection.BindingFlags.CreateInstance, nullnull, formArgs);
                f.MdiParent 
= this.ParentForm;
                f.Show();
            }

            
catch
            
{
                MessageBox.Show(
"该功能不能被打开!");
            }

        }

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值