Question ContextMenuStrip itemclicked event

Rossmc

Member
Joined
Mar 17, 2009
Messages
12
Programming Experience
10+
I dynamically build a context menu. I have three separate forms all using the same context menu, so I built a function that returns a context menu and placed it in a module.

This all works perfectly, except submenu items don't raise the contextmenu.itemclicked event at all?

Some sample code:
------------------
On a control on the calling form:
If e.Button = Windows.Forms.MouseButtons.Right Then
cms = Menu_Popup(sender, e)
cms.Show(Me, New Point(e.X, e.Y))
End If

The menu building code in the module:
------------------------------------
Function Menu_Popup() as ContextMenuStrip
Dim cms As New ContextMenuStrip
Dim fdoInsert As New System.Windows.Forms.ToolStripMenuItem
Dim fdoDelete As New System.Windows.Forms.ToolStripMenuItem
Dim fdoSep1 As New System.Windows.Forms.ToolStripSeparator
Dim fdoHeading As New System.Windows.Forms.ToolStripMenuItem
Dim fdoSummary As New System.Windows.Forms.ToolStripMenuItem
Dim fdoVerbose As New System.Windows.Forms.ToolStripMenuItem

With cms
.Items.AddRange(New System.Windows.Forms.ToolStripItem() {fdoInsert, fdoDelete, fdoSep1, fdoHeading})
.Name = "cmsfdoFinancial"
.Size = New System.Drawing.Size(158, 198)

'Insert
fdoInsert.Name = "Insert"
fdoInsert.Size = New System.Drawing.Size(157, 22)
fdoInsert.Text = "Insert..."

'Delete
fdoDelete.Name = "Delete"
fdoDelete.Size = New System.Drawing.Size(157, 22)
fdoDelete.Text = "Delete"

'Heading
fdoHeading.DropDownItems.AddRange(New System.Windows.Forms.ToolStripItem() {fdoSummary, fdoVerbose})
fdoHeading.Name = "Heading"
fdoHeading.Size = New System.Drawing.Size(157, 22)
fdoHeading.Text = "Heading Style"

'Summary
fdoSummary.Name = "Summary"
fdoSummary.Size = New System.Drawing.Size(152, 22)
fdoSummary.Text = "Summary"

'Verbose
fdoVerbose.Name = "Verbose"
fdoVerbose.Size = New System.Drawing.Size(152, 22)
fdoVerbose.Text = "Verbose"
End With

Return cms

Now, on the cms_ItemClicked event on the form, e.clickeditem.name will contain the name specified above unless I click on one of the submenu items in which case it will be blank.

Any ideas out there?

Thanks
 
Problem Solved

Luckily I was able to solve my own problem. So for anyone who has this requirement: a single context menu, called from multiple different forms, with minor option differences based on source form. If you want to create a single contextmenu and not duplicate the menu on multiple forms, try this:

I created a new class - mycms
The class inherits ContextMenuStrip

I use the constructor to determine which 'flavour' of the menu should be built. Ie. dim cms as new mycms(me.name)

You then construct the menu in the class.
The class has an event called submenuitemclicked(sender, e)

When adding a submenuitem to the menu use the AddHandler function to call a procedure that will raise the event, ie.
AddHandler SomeMenuObject.Click, AddressOf SubmenuItem

Feel free to contact me for details.
 
Back
Top