簡體   English   中英

如何在按鈕上顯示菜單項

[英]how to show menu item on button click on toolstrip

我正在開發一個應用程序,必須從數據庫動態加載工具條上的菜單。 到目前為止,我已經創建了菜單及其功能,現在必須在工具欄按鈕單擊事件上顯示該菜單。

這是我的代碼:

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();
            }
        }
    }
}

當前顯示如下: 目前正在顯示此。

我希望它顯示如下: 我要這樣

     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);
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM