adding routine to sub menus

Zeekabal

New member
Joined
Oct 4, 2011
Messages
4
Programming Experience
Beginner
Hi,

I am trying to populate a submenu dynamically whenever an item is clicked, so the chain is as follow

menu strip ---> menu item (when it is pressed the submenu are populated dynamically) ---sub menus

now i need to assign a routine to execute a certain task (lets say a msgbox for now) whenever a submenu item is clicked.

could you please advise on how can i do that?

Best Regards,
Z
 
Presumably, when you populate the menu you are calling DropDownItems.Add on your parent menu item. That Add method has an overload that lets you specify the method to handle the added item's Click event.

You will have had to already written the method. The easiest way to do that is to get the IDE to do it for you from an existing menu item, then just remove the Handles clause and, most likely, change the name.

When calling Add, you use AddHandler and the name of your method to create the EventHandler delegate to pass as an argument.

Inside the event handler, the 'sender' parameter contains a reference to the menu item that was clicked, so you can get the text or whatever else you need.
 
thank you very much for your prompt reply,

in fact that what i currently have in my code,

Private Sub ModifyElementToolStripMenuItem_MouseEnter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ModifyElementToolStripMenuItem.MouseEnter


ModifyElementToolStripMenuItem.DropDownItems.Clear() //to clear the submenu list
modifyentry() // a small routine to populate the submenu items


End Sub

however in the modifyentry () routine, this is how I am populating the submenu items,

tMenu1 = ModifyElementToolStripMenuItem


tMenu1.DropDownItems.Add(arword1(1)) //arword1 is an array of values,

Now I need to continue my program by small routines to do the following for example,

when dropdownitem number 1 is clicked, generate msgbox(1)
when dropdownitem number 2 is clicked, generate msgbox(2) and so on..

How the code for this would look like?
I am sorry for my newbiness in this ocean :D and thank a million in advance,

Best Regards,
Z
 
Back
Top