I'm trying to work with dynamic context menus in VB.NET and running into
what seems to be a very simple but very annoying problem.
Say my user has created a list of "names". Could be 1,2,5.. or 20 names. I
don 't know at design time. Now I want to create a context menu with these
names as the menu text. Creating the menus is a piece of cake but
determining what name the user actually clicked seems to be impossible.
When you create the menu you either pass it a 'menuitem' class or use
AddressOf to point it to a Sub.
Idealy I'd like all the menus to point to a single Sub and use a 'Select
Case' or something to dertermine what was selected but as I said above I
can't figure out how to pass the from the ContextMenu to the sub.
The only way I can see how to do this is to create 20 or so 'shell' event
handlers at design time for the menu items and then use IF's to match them
up to the contextmenu at run time.
Would look something list this
Public Sub DoWork (nameIndex as int32)
'Actually process the context menu selected
End Sub
Private Sub ContextMenu1_Popup
dim intNumOfNames as Int32 = names.count
if intNumOfNames > 0 then
CM1.MenuItem.Add(names.item(1).ToString, AddressOf (MenuName1))
end if
if intNumOfNames > 1 then
CM1.MenuItem.Add(names.item(2).ToString, AddressOf (MenuName2))
end if
if intNumOfNames > 2 then
CM1.MenuItem.Add(names.item(3).ToString, AddressOf (MenuName3))
end if
Yadda Yadda Yadda
End Sub
Protected Sub MenuName1 (byval sender as yadda)
DoWork (1)
End Sub
Protected Sub MenuName2 (byval sender as yadda)
DoWork (2)
End Sub
Protected Sub MenuName3 (byval sender as yadda)
DoWork (3)
End Sub
And so forth and so forth
This has two drawbacks. One it creates a ceiling for the number of names the
user can be working with and secondly it's damm messy not to mention
inefficent code.
What I'm trying to do is really simply. Determine what menu/name the user
selected. Can any one offer a brighter idea? And please don't say combo or
list box or I'll just cry.
Fotini
what seems to be a very simple but very annoying problem.
Say my user has created a list of "names". Could be 1,2,5.. or 20 names. I
don 't know at design time. Now I want to create a context menu with these
names as the menu text. Creating the menus is a piece of cake but
determining what name the user actually clicked seems to be impossible.
When you create the menu you either pass it a 'menuitem' class or use
AddressOf to point it to a Sub.
Idealy I'd like all the menus to point to a single Sub and use a 'Select
Case' or something to dertermine what was selected but as I said above I
can't figure out how to pass the from the ContextMenu to the sub.
The only way I can see how to do this is to create 20 or so 'shell' event
handlers at design time for the menu items and then use IF's to match them
up to the contextmenu at run time.
Would look something list this
Public Sub DoWork (nameIndex as int32)
'Actually process the context menu selected
End Sub
Private Sub ContextMenu1_Popup
dim intNumOfNames as Int32 = names.count
if intNumOfNames > 0 then
CM1.MenuItem.Add(names.item(1).ToString, AddressOf (MenuName1))
end if
if intNumOfNames > 1 then
CM1.MenuItem.Add(names.item(2).ToString, AddressOf (MenuName2))
end if
if intNumOfNames > 2 then
CM1.MenuItem.Add(names.item(3).ToString, AddressOf (MenuName3))
end if
Yadda Yadda Yadda
End Sub
Protected Sub MenuName1 (byval sender as yadda)
DoWork (1)
End Sub
Protected Sub MenuName2 (byval sender as yadda)
DoWork (2)
End Sub
Protected Sub MenuName3 (byval sender as yadda)
DoWork (3)
End Sub
And so forth and so forth
This has two drawbacks. One it creates a ceiling for the number of names the
user can be working with and secondly it's damm messy not to mention
inefficent code.
What I'm trying to do is really simply. Determine what menu/name the user
selected. Can any one offer a brighter idea? And please don't say combo or
list box or I'll just cry.
Fotini