一个动态生成菜单的例子你参考一下:
public static void GenMenuItem(ref System.Windows.Forms.MainMenu MainMenu,string strUserName,string strDeptNo,string Mkmc,MenuItem m0,int iniMkbm,bool leaf)
{
MenuItem m=new MenuItem();
m.Text=Mkmc;
if (iniMkbm.ToString().Length==4)
{
MainMenu.MenuItems.Add(m);
}
else
{
m0.MenuItems.Add(m);
if (leaf)
{
m.Click+=new EventHandler(event_Handler);
}
}
string mySql="SELECT distinct y.mkbm,y.mkmc,y.sjmkbm,y.sfzjd FROM "+
"(select a.js,b.mkbm from b_czryjs a inner join b_qxqd b on a.js=b.js "+
"where a.yhm='"+strUserName+"' and a.bmbh='"+strDeptNo+"')x,b_mkqd y "+
"where x.mkbm=y.mkbm and y.sjmkbm="+iniMkbm+" order by y.mkbm";
SqlDataReader myDataReader= SystemClass.SystemClass.SelectData(mySql);
while (myDataReader.Read())
{
GenMenuItem(ref MainMenu,strUserName,strDeptNo,myDataReader.GetString(1),m,myDataReader.GetInt32(0),myDataReader.GetBoolean(3));
}
myDataReader.Close();
}
public static void event_Handler(object sender, System.EventArgs e)
{
MenuItem thisMenu=(MenuItem)sender;
string strSql = " select MKBM from B_MKQD where MKMC = '"+thisMenu.Text+"'";
SqlCommand cmd = new SqlCommand();
cmd.Connection = Class1.GetConneciton();
cmd.CommandText = strSql;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds,"T");
string strMKBM = "GUI.frm"+ds.Tables["T"].Rows[0]["MKBM"].ToString();
testshowForm(strMKBM.Trim());
}
public static void GenMenuItem(ref System.Windows.Forms.MainMenu MainMenu,string strUserName,string strDeptNo,string Mkmc,MenuItem m0,int iniMkbm,bool leaf)
{
MenuItem m=new MenuItem();
m.Text=Mkmc;
if (iniMkbm.ToString().Length==4)
{
MainMenu.MenuItems.Add(m);
}
else
{
m0.MenuItems.Add(m);
if (leaf)
{
m.Click+=new EventHandler(event_Handler);
}
}
string mySql="SELECT distinct y.mkbm,y.mkmc,y.sjmkbm,y.sfzjd FROM "+
"(select a.js,b.mkbm from b_czryjs a inner join b_qxqd b on a.js=b.js "+
"where a.yhm='"+strUserName+"' and a.bmbh='"+strDeptNo+"')x,b_mkqd y "+
"where x.mkbm=y.mkbm and y.sjmkbm="+iniMkbm+" order by y.mkbm";
SqlDataReader myDataReader= SystemClass.SystemClass.SelectData(mySql);
while (myDataReader.Read())
{
GenMenuItem(ref MainMenu,strUserName,strDeptNo,myDataReader.GetString(1),m,myDataReader.GetInt32(0),myDataReader.GetBoolean(3));
}
myDataReader.Close();
}
public static void event_Handler(object sender, System.EventArgs e)
{
MenuItem thisMenu=(MenuItem)sender;
string strSql = " select MKBM from B_MKQD where MKMC = '"+thisMenu.Text+"'";
SqlCommand cmd = new SqlCommand();
cmd.Connection = Class1.GetConneciton();
cmd.CommandText = strSql;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds,"T");
string strMKBM = "GUI.frm"+ds.Tables["T"].Rows[0]["MKBM"].ToString();
testshowForm(strMKBM.Trim());
}
{
private MainMenu mainMenu1;
private DataTable tb;
private string strSort;
private int intKey;
private int intText;
private bool isPreview;
public LdlWinBulidMenu(MainMenu mmObject,DataTable dtMenu,string strSortField,int intKey,int intText,bool isPreview)
{
this.mainMenu1=mmObject;
this.tb=dtMenu;
this.strSort=strSortField;
this.intKey=intKey;
this.intText=intText;
this.isPreview=isPreview;
} public MainMenu BulidTopMenu()
{
MenuItem miChildMenuItem;
string strEventMethodName;
string strRowFilter; mainMenu1.MenuItems.Clear(); DataView dvAll=new DataView();
dvAll.Table=tb; DataView dvTop=new DataView();
dvTop.Table=tb;
dvTop.RowFilter="nItemPK <>nParentItemPK and nParentItemPK=1";
dvTop.Sort=strSort;
for (int i=0;i<dvTop.Count;i++)
{
miChildMenuItem=new MenuItem();
miChildMenuItem.Text=dvTop[i][intText].ToString()+"(&"+Convert.ToChar(65+i)+")"; dvAll.RowFilter="nParentItemPK ="+dvTop[i][intKey].ToString();
if (dvAll.Count<1)
{
if (dvTop[i]["vcShortCut"].ToString()!="") miChildMenuItem.Shortcut=(Shortcut)Enum.Parse(typeof(Shortcut), dvTop[i]["vcShortCut"].ToString());
strEventMethodName =dvTop[i]["vcClickEvent"].ToString();
if (strEventMethodName==null || strEventMethodName.Trim().ToString()=="")
{
MessageBox.Show("尚未定义触发事件的方法!",dvTop[i]["vcText"].ToString());
}
else
{
if (this.isPreview)
miChildMenuItem.Click+=(EventHandler)Delegate.CreateDelegate(typeof(EventHandler),this,"TestMenuItemEvent");
// else
// miChildMenuItem.Click+=(EventHandler)Delegate.CreateDelegate(typeof(EventHandler),this, dvTop[i]["vcClickEvent"].ToString());
}
}
mainMenu1.MenuItems.Add(miChildMenuItem); strRowFilter="nItemPK <>nParentItemPK and nParentItemPK="+dvTop[i][intKey].ToString();
BulidSubMenu(miChildMenuItem,strRowFilter);
}
return mainMenu1; }
private void BulidSubMenu(MenuItem miParentMenuItem,string strRowFilter)
{ MenuItem miChildMenuItem;
string strEventMethodName;
string strSubRowFilter;
string strMenuItemText; DataView dvAll=new DataView();
dvAll.Table=tb; DataView dvSub=new DataView();
dvSub.Table=tb;
dvSub.RowFilter=strRowFilter;
dvSub.Sort=strSort;
for (int i=0;i<dvSub.Count;i++)
{
miChildMenuItem=new MenuItem(); strMenuItemText=dvSub[i][intText].ToString();
if (strMenuItemText=="-")
{
if ((i>0)&&(i<dvSub.Count-1)&&(strMenuItemText!=dvSub[i-1][intText].ToString())&&(strMenuItemText!=dvSub[i+1][intText].ToString()))
{miChildMenuItem.Text=strMenuItemText;}
else {continue;}
}
else
{ miChildMenuItem.Text=strMenuItemText+"(&"+Convert.ToChar(65+i)+")"; dvAll.RowFilter="nParentItemPK ="+dvSub[i][intKey].ToString();
if (dvAll.Count<1) {
if (dvSub[i]["vcShortCut"].ToString()!="") miChildMenuItem.Shortcut=(Shortcut)Enum.Parse(typeof(Shortcut), dvSub[i]["vcShortCut"].ToString()); strEventMethodName =dvSub[i]["vcClickEvent"].ToString();
if (strEventMethodName==null || strEventMethodName.ToString()=="")
{
// MessageBox.Show("尚未定义触发事件的方法!",dvSub[i]["vcText"].ToString());
}
else
{
if (this.isPreview)
miChildMenuItem.Click+=(EventHandler)Delegate.CreateDelegate(typeof(EventHandler),this,"TestMenuItemEvent");
// else
// miChildMenuItem.Click+=(EventHandler)Delegate.CreateDelegate(typeof(EventHandler),this, dvSub[i]["vcClickEvent"].ToString());
}
}
} miParentMenuItem.MenuItems.Add(miChildMenuItem);
strSubRowFilter="nItemPK <>nParentItemPK and nParentItemPK="+dvSub[i][intKey].ToString();
BulidSubMenu(miChildMenuItem,strSubRowFilter);
}
}
//for preview time
private void TestMenuItemEvent(object sender, System.EventArgs e)
{
MessageBox.Show("事件被点击","预览提示");
}
}