Question Context Menu help needed =D

Terzaghi

Member
Joined
Feb 25, 2010
Messages
8
Programming Experience
Beginner
i have a context menu for my program... items are added to it during runtime using ContextMenu.MenuItems.Add("blahblah")

how do I program the items of the context menu when clicked? i've always created menu items beforehand and by double clicking on it, it generates a sub like
VB.NET:
Expand Collapse Copy
Private Sub MenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem1.Click

End Sub

but for this project, its not possible to tell how many items there will be. :D help pl0x? thanks guys!
 
First up, is there a particular reason that you're using a ContextMenu in .NET 3.5 rather than a ContextMenuStrip? If you're using a ContextMenu then you have one option: specify the method to handle the Click event when you create it, e.g.
VB.NET:
Expand Collapse Copy
myContextMenu.MenuItems.Add("Item Text", AddressOf ItemClickEventHandler)
where ItemClickEventHandler is the method you wrote to handle the item's Click event. In that event handler, just like every event handler, you get a reference to the object that raised the event from the 'sender' parameter.

If you're using a ContextMenuStrip then you can do the same thing, or you can handle the ItemClicked event of the menu. The e.ClickedItem property gives you the item that was clicked.
 
Back
Top