动态加载菜单的源代码(根据用户的权限动态加载功能菜单)
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, null, null, formArgs);
f.MdiParent = this.ParentForm;
f.Show();
}
catch
...{
MessageBox.Show("该功能不能被打开!");
}
}
... {
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, null, null, formArgs);
f.MdiParent = this.ParentForm;
f.Show();
}
catch
...{
MessageBox.Show("该功能不能被打开!");
}
}