Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Handling menus in vb.net

Status
Not open for further replies.

bigfoot

Programmer
Joined
May 4, 1999
Messages
1,779
Location
US
I have fooled around with this for 3 days now and it's time to ask.

How do you reference a menu item in code?

'Create the ComponentMenu object
Private objComponentMenu As MenuItem
'Create the main "Messages" menu item
objComponentMenu = New MenuItem("&Component")
'Create another new "Menuxx" menu item
'Add an event handler to handle the user's clicking on the menu item.
Dim mnuMenu1 As New MenuItem("Menu01", New System.EventHandler(AddressOf Me.ComponentMenuHandler))

'Add the menu item to the "Component" menu item
objComponentMenu.MenuItems.Add(mnuMenu1)

Dim mnuMenu2 As New MenuItem("Menu02", New System.EventHandler(AddressOf Me.ComponentMenuHandler))

'Add the menu item to the "Component" menu item
objComponentMenu.MenuItems.Add(mnuMenu2)


'My menu handler
Protected Sub ComponentMenuHandler(ByVal sender As Object, ByVal e As System.EventArgs)
Select Case ????

Ok so what do I switch on? It can't be the index, it changes. It can't be the menu text, it changes.


End Select
End Sub


Help please.
 
The menu object's name:

Select Case sender.name
Case "mnuMenu1"

Case "mnuMenu2"

End Select


I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson
 
Sorry jebenson but the menu has no name. :(
This would be too easy for M$ to add a name property. I'm thinking of subclassing it but I wanted to ask here first.


When I try to use name it gives me this:
An unhandled exception of type 'System.MissingMemberException' occurred in microsoft.visualbasic.dll

Additional information: Public member 'name' on type 'MenuItem' not found.
 
You can check and see how the .equals function works for the particular menu item object.

If sender.equals(menu_item1) then
ElseIf sender.equals(menu_item2) then
etc...
 
I had the same problem a few months ago. The fastest solution is to create your own menu items (it only takes a couple of minutes) and adding the properies your want (like a name or a tag and so on..).

isn't it weird? You can set a menu item's name but not access it???

BTW has this been fixed in VS2005?
 
What I don't understand is why you can't use the text property... is this due to localization? Could you please explain?

Regards, Ruffnekk
---
Is it my imagination or do buffalo wings taste just like chicken?
 
Yep this is fixed in 2005

Christiaan Baes
Belgium

I just like this --> [Wiggle] [Wiggle]
 
I may want to change the text property. Due to localization or if I want to load the text values from a config file, then they will be wrong.
If I use the index and someone adds a new menu, then that will be wrong.

I found tis article where they use constants, and someone said here in the office that it would work because we could use varibles for all of the menu items, but that's a mess of work.

I'm thinking subclassing.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top