Click here to Skip to main content
15,846,211 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am creating ToolStripMenuItems using a For counter. It reads from a database and populates the list. I have already got the following code to work:

Dim i As Integer
            For i = 0 To apps.Tables("APPS").Rows.Count - 1
                Dim mi As New ToolStripMenuItem
                mi.Text = apps.Tables("APPS").Rows(i).Item("AppName")
                mi.Image = Image.FromFile(apps.Tables("APPS").Rows(i).Item("AppPath") + "\icon.png")
                mi.Tag = mi.Text
                AddHandler mi.Click, AddressOf mi_Click

However, I want to run the application that the button was created to represent when it is clicked. AddHandler doesn't let me supply the mi_Click(mi.Tag) argument. How else can I get this variable forwarded to the sub?

1 solution

Everything you have so far looks correct. Here is the next step.
In the Click EventHandler you should cast the sender, like so:
Private Sub mi_Click(ByVal sender As Object, ByVal e As EventArgs)
   Dim mi As ToolStripMenuItem = DirectCast(sender, ToolStripMenuItem)
   ' You now have access to any Property of the currently clicked MenuItem.
   Dim text As String = mi.Tag
   ' Do other stuff here.
End Sub

Hope it helps :)
Share this answer
Reiss 25-Oct-11 9:36am    
my 5, just what I was going to post
Sander Rossel 25-Oct-11 10:00am    
Thanks :)
saint1997 25-Oct-11 10:05am    
Thank you so much. This worked like a charm. :)
Sander Rossel 25-Oct-11 10:29am    
No problem :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900