Click here to Skip to main content
15,884,629 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
There is a myToolStripMenuItem that i add some submenu to it (submenuName changes dynamically)
ToolStripMenuItem tsmi = new ToolStripMenuItem(submenuName, null, new EventHandler(submenuName_Click));

How to find that the user click on which submenu?
Updated 29-Mar-22 4:23am
BillWoodruff 16-Nov-14 5:45am    
The code in the CP Tip you were referred to is very poor quality. There is a much better way to handle this task if you are dealing with a scenario in which:

1. you need to add and remove new ToolStripMenuItems frequently at run-time.

2. you are willing to have each ToolStripMenuItem's Text Property be a unique string.

If you want to see an example of this type of solution to menu management, just ask, here.

check How to add ToolStripMenuItems to a MenuStrip or ContextMenu dynamically[^]
When you add menu items give unique text for them, then you can easily identify which menu item clicked by event sender object.
Sample code:
ToolStripMenuItem menu = new ToolStripMenuItem(submenuName);
menu.Click += new EventHandler(menu_Click);
// add the event as below
void menu_Click(object sender, EventArgs e)
    var menuItem = sender as MenuItem; 
    var menuText = menuItem.Text;

    switch(menuText) {
        case "MenuItem1":
           // menu item1 clicked .. do something 

        case "MenuItem2":
          // menu item2 clicked .. do something 
         . ...
Share this answer
4L4K1 16-Nov-14 1:01am    
thank you it works. the fastest solvation ever
DamithSL 16-Nov-14 1:03am    
you are welcome!
BillWoodruff 16-Nov-14 5:46am    
My vote of #3. Damith, sorry, but that CP Tip is very poor quality code. fyi: imho, a vote of #3 is not a down-vote.
DamithSL 16-Nov-14 5:51am    
It's Ok, but I'm really glad if you can go and vote for that tip with constructive feedback, why you think it is poor quality and how it can be improved. it might help all CP community.
BillWoodruff 16-Nov-14 6:06am    
Hi Damith, I went further than that: I reported that Tip: imho, it should never have been posted. Also, I would (down-vote or) leave constructive (I hope) feedback on only recent articles/tips I came across: those that showed evidence the author had responded to user comments in the last few years.

Unfortunately, there are so many poor quality confused/irrelevant/regurgitate-documentation articles and tips on CP, that it would be a full-time job to report them, or try and improve them. But, please don't think ... based on that statement ... that I am unhappy in any way with CP: I like CP, and there are so many great articles and tips, real gems that have professional code quality, that are maintained/extended for years by the authors. The cup is more than half-full ! And, there are lots of high-quality answers here on QA, including some of yours :)

cheers, Bill
Use the Name propeerty instead of the Text property
Share this answer
Another nice way to add a string array to Dropdownitems, including an Eventhandler, all in one line
;) I just love stuff like that!

string[] types = { "1", "2", "3" };

myToolStripMenuItem.DropDownItems.AddRange(types.Select(text => new ToolStripMenuItem(text, null, new EventHandler(menu_Click))).ToArray());
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900