我已经从数据库主菜单和子菜单中填充了菜单条,在窗体加载中它显示了主菜单和子菜单,但是我想单击子菜单以打开窗体. [英] I have filled menustrip from database mainmenu and sub menu, on form load it is showing main menu and sub menu but i want on click of submenu to open form.

查看:75
本文介绍了我已经从数据库主菜单和子菜单中填充了菜单条,在窗体加载中它显示了主菜单和子菜单,但是我想单击子菜单以打开窗体.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I have filled menustrip from database mainmenu and sub menu,
on form load it is showing main menu and sub menu but i want on click of submenu to open form.



这是我的代码.



this is my code.

public partial class Form1 : Form
    {
        string strcon = @"Data Source= SV3\SQLEXPRESS;Initial Catalog= AarambhStandalone;Integrated Security= true";

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(strcon);
            con.Open();
            SqlCommand cmd = new SqlCommand("select Description from tblMenu", con);
            SqlDataAdapter da = new SqlDataAdapter();
            da.SelectCommand = cmd;
            DataSet ds = new DataSet();
            da.Fill(ds);

            MenuStrip mnu = new MenuStrip();
            if (ds.Tables[0].Rows.Count != 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    mnu.Items.Add(ds.Tables[0].Rows[i]["Description"].ToString(), null, new EventHandler(MenuItemClicked));
                }

                this.Controls.Add(mnu);
            }
        }

        private void MenuItemClicked(object sender, EventArgs e)
        {
            ContextMenuStrip cntx = new ContextMenuStrip();
            List<string> lstMenu = new List<string>();
            SqlConnection con = new SqlConnection(strcon);
            SqlCommand cmd = new SqlCommand();
            SqlDataAdapter dr = new SqlDataAdapter();
            int mnuID = 0;
            string strSelect = "Select mnuID from tblMenu where Description='" + sender.ToString() + "'";
            cmd = new SqlCommand(strSelect, con);
            con.Open();
            dr.SelectCommand = cmd;
            DataSet ds = new DataSet();
            DataSet ds1 = new DataSet();
            dr.Fill(ds);
            if (ds.Tables[0].Rows.Count > 0)
            {
                mnuID = Convert.ToInt16(ds.Tables[0].Rows[0]["mnuID"].ToString());
            }

            strSelect = "select MenuText from tblsubmenu where mnuID='" + mnuID + "'";
            cmd = new SqlCommand(strSelect, con);
            if (con.State == ConnectionState.Closed)
            {
                con.Open();
            }

            dr.SelectCommand = cmd;
            dr.Fill(ds1);
            if (ds1.Tables[0].Rows.Count != 0)
            {
                for (int icnt = 0; icnt < ds1.Tables[0].Rows.Count; icnt++)
                {
                    lstMenu.Add(ds1.Tables[0].Rows[icnt]["MenuText"].ToString());
                }
            }
            foreach (string str in lstMenu)
            {
                cntx.Items.Add(str);
            }
            ToolStripMenuItem tsi = (ToolStripMenuItem)sender;
            tsi.DropDown = cntx;
           
        }

 }



上的



on click of shift definition on want to open shiftdefinition form.
on click of shift definition on want to open Regularization form.

data filled in menustrip from database

推荐答案

MenuClick事件动态添加处理程序到您的工具栏,

Addhandler tsi.Click,MnuClick的地址

并在mnuclick事件中
检查工具条文本并在切换大小写中将其添加到文本符合特定条件的情况下,它将打开该表单
祝您编码愉快!
:)
Add handler for MenuClick event dynamically to your toolstrip,

Addhandler tsi.Click, address of MnuClick

and in mnuclick event
check toolstrip text and pu it in Switch case when text match certain condition it will open that form
Happy Coding!
:)


这篇关于我已经从数据库主菜单和子菜单中填充了菜单条,在窗体加载中它显示了主菜单和子菜单,但是我想单击子菜单以打开窗体.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆