Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2007-09-08
2
Medium Priority
?
1,274 Views
Last Modified: 2013-11-26
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
Comment
Question by:countrymeister
2 Comments
 
LVL 6

Accepted Solution

by:
so3 earned 2000 total points
ID: 19854456
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
 
LVL 1

Author Comment

by:countrymeister
ID: 19854870
That was awesome, thank you
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

783 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question