• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1359
  • Last Modified:

How do I capture the information of the Toolstripmenuitem that was clicked

How do I capture the Toolstripmenuitem that was clicked.
I have a windows form developed in VS 2005, which has a menustrip which has many toolstripmenuitems, which in turn have child  toolstripmenuitems.

The Toolstripmenuitems are added to the menustrip at runtime dynamically from an xml file.
How do I detect which item within the menustrip was clicked
I used this as my reference to bind the menustrip to the xml file

http://www.codeproject.com/useritems/XML_Driven_Menu_Strip.asp?df=100&forumid=408426&exp=0&select=1993718
0
countrymeister
Asked:
countrymeister
1 Solution
 
so3Commented:
Run the CreateEvents method after the menustrip is poulated from xml, and when you press a toolstripmenuitem in the event child_item_DropDownItemClicked you will have the toolstripmenuitem in e.clickeditem

 private void CreateEvents(MenuStrip menu, ToolStripMenuItem item)
        {
            if (item == null)
            {
                foreach (ToolStripMenuItem child_item in menu.Items)
                {
                    if (child_item.HasDropDownItems)
                    {
                        child_item.DropDownItemClicked += new ToolStripItemClickedEventHandler(child_item_DropDownItemClicked);
                        CreateEvents(menu, child_item);
                    }
                }
                menu.ItemClicked += new ToolStripItemClickedEventHandler(child_item_DropDownItemClicked);
            }
            else
            {
                foreach (ToolStripMenuItem child_item in item.DropDownItems)
                {
                    if (child_item.HasDropDownItems)
                    {
                        child_item.DropDownItemClicked += new ToolStripItemClickedEventHandler(child_item_DropDownItemClicked);
                        CreateEvents(menu, child_item);
                    }
                }
            }
           
        }

        void child_item_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            MessageBox.Show(e.ClickedItem.Text);
        }

Example:

LoadDynamicMenu( XmlPath );
CreateEvents(menustrip1, null);
0
 
countrymeisterAuthor Commented:
That was awesome, thank you
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

Tackle projects and never again get stuck behind a technical roadblock.
Join Now