如何在按钮上显示菜单项单击工具条

时间:2016-12-06 10:02:07

标签: c# sql-server-2008

我正在开发一个应用程序,我必须从数据库动态加载工具条上的菜单。到目前为止,我已经创建了菜单及其工作,现在我必须在工具条按钮单击事件上显示该菜单。

这是我的代码:

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..

1 个答案:

答案 0 :(得分: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);
        }
    }