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
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