2016-12-06 140 views
2

我正在研究一個應用程序,我必須從數據庫動態地加載工具條上的菜單。到目前爲止,我已經創建了菜單及其工作,現在我必須在工具條按鈕單擊事件上顯示該菜單。如何顯示按鈕上的菜單項點擊工具條

這裏是我的代碼:

private void toolstripform_Load(object sender, EventArgs e) 
{ 
    this.IsMdiContainer = true; 
    this.Controls.Add(toolStrip1); 
    string slctcmd = string.Format("SELECT * FROM MNU_PARENT where MENUPARVAL = 2 "); 
    DataTable dt = qc.DataReaderTable(slctcmd); 
    foreach(DataRow dr in dt.Rows) 
    { 
     ToolStripMenuItem MnuStripItem = new ToolStripMenuItem(dr["MAINMNU"].ToString()); 
     SubMenu(MnuStripItem, dr["MENUPARVAL"].ToString()); 
     toolStrip1.Items.Add(MnuStripItem); 
    } 
} 
public void SubMenu(ToolStripMenuItem mnu, string submenu) 
{ 
    string slctcmd = string.Format("SELECT FRM_NAME FROM MNU_SUBMENU WHERE MENUPARVAL='" + submenu + "'"); 
    DataTable dt = qc.DataReaderTable(slctcmd); 
    foreach(DataRow dr in dt.Rows) 
    { 
     ToolStripMenuItem SSMenu = new ToolStripMenuItem(dr["FRM_NAME"].ToString(), null, new EventHandler(ChildClick)); 
     mnu.DropDownItems.Add(SSMenu); 
    } 
} 
private void ChildClick(object sender, EventArgs e) 
{ 
    // MessageBox.Show(string.Concat("You have Clicked ", sender.ToString(), " Menu"), "Menu Items Event",MessageBoxButtons.OK, MessageBoxIcon.Information); 
    string slctcmd = string.Format("SELECT FRM_CODE FROM MNU_SUBMENU WHERE FRM_NAME= '" + sender.ToString() + "'"); 
    DataTable dtransaction = qc.DataReaderTable(slctcmd); 
    Assembly frmAssembly = Assembly.LoadFile(Application.ExecutablePath); 
    foreach(Type type in frmAssembly.GetTypes()) 
    { 
     //MessageBox.Show(type.Name); 
     if (type.BaseType == typeof (Form)) 
     { 
      if (type.Name == dtransaction.Rows[0][0].ToString()) 
      { 
       Form frmShow = (Form) frmAssembly.CreateInstance(type.ToString()); 
       // then when you want to close all of them simple call the below code 
       foreach(Form form in this.MdiChildren) 
       { 
        form.Close(); 
       } 
       frmShow.MdiParent = this; 
       frmShow.StartPosition = FormStartPosition.CenterScreen; 
       //frmShow.WindowState = FormWindowState.Maximized; 
       //frmShow.ControlBox = false; 
       frmShow.Show(); 
      } 
     } 
    } 
} 

它目前呈現這樣的: currently showing this..

我想它顯示是這樣的: I want like this..

回答

0
 MnuStripItem.MouseDown += new MouseEventHandler(mainM_MouseDown); 

private void mainM_MouseDown(object sender, MouseEventArgs e) 
    { 
     //if (e.Button == MouseButtons.Left) 
     if (e.Button == MouseButtons.Right) 
     { 
      cMenuS_main_it.Show(Cursor.Position); 
     } 
    }