ContextMenu Help!

stev-io

Member
Joined
Nov 15, 2006
Messages
6
Location
Ballarat, Victoria, Australia
Programming Experience
3-5
I'm new to VB.NET and am creating a little program that will run in the sys tray with a context menu. the menu has sub menus and i want to call the same sub when each of them is clicked. Here's the menu setup:
VB.NET:
'this will add the root items to the menu
mobContextMenu.MenuItems.Add(New MenuItem("Select AWS"))
mobContextMenu.MenuItems.Add("-")
mobContextMenu.MenuItems.Add(New MenuItem("About", New EventHandler(AddressOf AboutBox)))
mobContextMenu.MenuItems.Add("-")
mobContextMenu.MenuItems.Add(New MenuItem("Exit", New EventHandler(AddressOf ExitProgram)))
 
'this will add the states to the select aws menu item
mobContextMenu.MenuItems.Item(0).MenuItems.Add(New MenuItem("VIC"))
mobContextMenu.MenuItems.Item(0).MenuItems.Add(New MenuItem("SA"))
mobContextMenu.MenuItems.Item(0).MenuItems.Add(New MenuItem("WA"))
mobContextMenu.MenuItems.Item(0).MenuItems.Add(New MenuItem("NT"))
mobContextMenu.MenuItems.Item(0).MenuItems.Add(New MenuItem("QLD"))
mobContextMenu.MenuItems.Item(0).MenuItems.Add(New MenuItem("NSW"))
mobContextMenu.MenuItems.Item(0).MenuItems.Add(New MenuItem("TAS"))
 
'this will add the aws items to the states menu item
mobContextMenu.MenuItems.Item(0).MenuItems.Item(0).MenuItems.Add(New MenuItem("Ballarat", New EventHandler(AddressOf ChangeVars)))
mobContextMenu.MenuItems.Item(0).MenuItems.Item(0).MenuItems.Add(New MenuItem("Melbourne", New EventHandler(AddressOf ChangeVars)))
This all works well, but when i call the sub i want to extract some of the sender information that you can see when you debug the program. Is there an easy way to get this info?
 
Last edited by a moderator:
The 'sender' argument IS the object that raised the event, i.e. the menu item that was clicked. The sender is always an Object reference so you must cast it as a ToolStripMenuItem and then you can access any member you want:
VB.NET:
Dim clickedItem As ToolStripMenuItem = DirectCast(sender, ToolStripMenuItem)
Also, is there a particular reason that you've chosen to build the menu in code rather than the designer? The only reason that I can think of would be that you won't know what the actual values are until run time. Otherwise it's easier and neater to use the designer to build your menu and assign event handlers.
 
thanks jmcilhinney
i nutted it out using:
VB.NET:
Dim obj As MenuItem
 
'this will get the AWS selected
For Each obj In mobContextMenu.MenuItems(0).MenuItems(0).MenuItems
If Object.ReferenceEquals(sender, obj) = True Then
strAws = obj.Text
Exit For
End If
Next
 
'this will get the state selected
For Each obj In mobContextMenu.MenuItems(0).MenuItems
 
If Object.ReferenceEquals(sender.parent, obj) = True Then
strState = obj.Text
Exit For
End If
Next
My app doesn't use a form, but is all coded in a module and displayed in the system tray. Yeah, your right, i dynamically load most of the menu items at runtime.
Thanks
 
Last edited by a moderator:
That's a very inefficient and completely unnecessary way to get the information you want. Like I said, the sender IS the menu item so there's no need or point comparing it to anything because you already have it.

Now, from your code I'm thinking that you are using a ContextMenu rather than a ContextMenuStrip. Are you using VB 2005 as your profile says or not? If you are then you should be using a ContextMenuStrip. If you're not then please set your profile properly so that people don't waste their time giving irrelevant advice. The following code will get you the AWS and state:
VB.NET:
Dim item As MenuItem = DirectCast(sender, MenuItem)
Dim aws As String = item.Text
Dim state As String = item.Parent.Text
 
The ContextMenu is still available in .NET 2.0 but mostly for compatibility. The MainMenu, ContextMenu, ToolBar and StatusBar classes have all been replaced by the more functional MenuStrip, ContextMenuStrip, TooStrip and StatusStrip classes, which are all built on a common framework and provide better and more functionality than their older counterparts.
 
Back
Top