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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

dynamic Context menus

Status
Not open for further replies.

fotinaki

Programmer
Jun 4, 2003
12
FR
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
 
Try something like the following, since you have already created the Context menu, I am just giving you the delegate part which handles the context menu click

Private Sub menuClick(ByVal sender As Object, ByVal e As System.EventArgs)

Dim sMenuText As String

sMenuText= CType(sender, system.Windows.Forms.MenuItem).Text
Select Case sMenuText
Case "Name1"

MessageBox.Show("Name1 Clicked")
Case "Name2"
MessageBox.Show("Name2 Clicked")
End Select
End Sub

 
A friend gave me the following solution.It is exactly what I need!Thanks you anyway!

Dim names() As String = {"james", "steve", "dave", "fred"}

Private Sub ContextMenu1_Popup(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ContextMenu1.Popup
Dim intNumOfNames As Int32 = names.Length

ContextMenu1.MenuItems.Clear()
Dim m As String
For Each m In names
ContextMenu1.MenuItems.Add(m, AddressOf MenuHandler)
Next

End Sub

Public Sub DoWork(ByVal nameIndex As Int32)
'Actually process the context menu selected
MessageBox.Show("help")
End Sub

Protected Sub MenuHandler(ByVal sender As Object, ByVal e As System.EventArgs)

Dim m As MenuItem
m = DirectCast(sender, MenuItem)

DoWork(m.Index)
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top