I m new in c#.net, i m working on project which generate menu dynamically, i have done this point, but not retrieve menu text at menu click, i just want to get text of menu.
my code is follows:

namespace myProg
{
    public partial class frmMenu : Form
    {
        DataTable DtMenu;
        MenuStrip MainMenu = new MenuStrip();
        ToolStripMenuItem PopUpMenu = new ToolStripMenuItem();
        ToolStripMenuItem SubMenu = new ToolStripMenuItem();
        ToolStripMenuItem BarMenu = new ToolStripMenuItem();

        public frmMenu()
        {
            InitializeComponent();
        }

        private void frmMenu_Load(object sender, EventArgs e)
        {
            CreateMainMenu();
        }
        private void CreatePopUpMenu(MenuStrip mMenu, string mPopUpName)
        {
            PopUpMenu = new ToolStripMenuItem(mPopUpName);
            PopUpMenu.Text = mPopUpName;
            PopUpMenu.TextAlign = ContentAlignment.BottomRight;
            mMenu.Items.Add(PopUpMenu);
        }
        private void CreateSubMenu(ToolStripMenuItem mPopUpName, string mBarName)
        {
            SubMenu = new ToolStripMenuItem(mBarName);
            SubMenu.Text = mBarName;
            SubMenu.TextAlign = ContentAlignment.BottomRight;
            mPopUpName.DropDown.Items.Add(SubMenu);
        }
        private void CreateBarMenu(ToolStripMenuItem mPopUpName, string mBarName, MenuStrip mMenu, Int32 mMenuId)
        {
            BarMenu = new ToolStripMenuItem(mBarName);
            BarMenu.TextAlign = ContentAlignment.BottomRight;
            BarMenu.Tag = Convert.ToInt32(mMenuId);
            BarMenu.Text = mBarName;
            mPopUpName.DropDown.Items.Add(BarMenu);

            BarMenu.Click += new System.EventHandler(this.FileMenuItemClick);
        }
        private void CreateMainMenu()
        {
            Int32 mTagId = 0;
            Int32 mLastTagId = 0;
            Int32 mSubMenu = 0; // 0=No, 1=Yes
            string mPopName = "";
            string mSubName = "";
            string mBarName = "";

            Users ObjUsers = new Users();

            DtMenu = ObjUsers.GetMainMenu();
            try
            {
                if (DtMenu.Rows.Count > 0)
                {
                    this.MainMenuStrip = MainMenu;
                    MainMenu.Name = "MainMenu";
                    MainMenu.Dock = DockStyle.Top;
                    Controls.Add(MainMenu);
                    for (int m_For = 0; m_For < DtMenu.Rows.Count; m_For++)
                    {
                        if (Convert.ToInt32(DtMenu.Rows[m_For]["tagid"]) != mTagId)
                        {
                            mTagId = Convert.ToInt32(DtMenu.Rows[m_For]["tagid"]);
                            if (mTagId != mLastTagId)
                            {
                                mLastTagId = mTagId;
                                if (mTagId == 1)
                                    mPopName = "Setup";
                                else
                                    mPopName = "Transaction";
                                mSubName = "";
                                mBarName = "";
                                PopUpMenu = new ToolStripMenuItem();
                                SubMenu = new ToolStripMenuItem();
                                BarMenu = new ToolStripMenuItem();
                            }
                            CreatePopUpMenu(MainMenu, mPopName);
                            if (Convert.ToInt32(DtMenu.Rows[m_For]["SubMenuOrClick"]) == 0)
                            {
                                mSubName = Convert.ToString(DtMenu.Rows[m_For]["BarDesc"]);
                                CreateSubMenu(PopUpMenu, mSubName);
                            }
                            else
                            {
                                mBarName = Convert.ToString(DtMenu.Rows[m_For]["BarDesc"]);
                                if (SubMenu.Name == "")
                                    CreateBarMenu(PopUpMenu, mBarName, MainMenu, Convert.ToInt32(DtMenu.Rows[m_For]["menuid"]));
                                else
                                    CreateBarMenu(SubMenu, mBarName, MainMenu, Convert.ToInt32(DtMenu.Rows[m_For]["menuid"]));

                            }
                        }
                        else
                        {
                            if (Convert.ToInt32(DtMenu.Rows[m_For]["SubMenuOrClick"]) == 0)
                            {
                                mSubName = Convert.ToString(DtMenu.Rows[m_For]["BarDesc"]);
                                if (PopUpMenu.DropDown.Items.Count > 0 && SubMenu.DropDown.Items.Count > 0)
                                    CreateSubMenu(SubMenu, mSubName);
                                else
                                    CreateSubMenu(PopUpMenu, mSubName);
                            }
                            else
                            {
                                mBarName = Convert.ToString(DtMenu.Rows[m_For]["BarDesc"]);
                                CreateBarMenu(SubMenu, mBarName, MainMenu, Convert.ToInt32(DtMenu.Rows[m_For]["menuid"]));
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void FileMenuItemClick(object sender, EventArgs e)
        {
              i have problem here, i just want tag value of selected bar, please help me
        }
    }
}

If you have not solved this yet then you need to do something like this:

private void FileMenuItemClick(object sender, EventArgs e)
{
    // get reference to clicked object
    ToolStripMenuItem barMenu = sender as ToolStripMenuItem;
    // test if object is ok and has a menu id
    if (barMenu != null && barMenu.Tag is Int32)
    {
        // get the menu id
        Int32 menuID = (Int32)barMenu.Tag;
        // do action for menu id
        switch (menuID)
        {
            case <MenuID1>:
                // do menu ID 1 task
                break;
            case <MenuID2>:
                // do menu ID 2 task
                break;
                //...
            case <MenuIDn>:
                // do menu ID n task
                break;
            default:
                // do nothing - menu ID not known
                // or throw/show error message
                break;
        }
    }
}

Where <MenuID1>, <MenuID2>, and <MenuIDn> are replaced with a suitable enum for your menu IDs.

[Edit] Also, next time you post please use the [code]

[/code] tags around your code.

Thank you mr. nick, your suggested solution is much beneficial for me, thank u very much.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.