[2005] Context menu click problem!
My application reads a folders subfolders and adds these to a contextmenues.
Then it finds all the files in each subfolders and adds them to their respective menu item. My code:
VB Code:
Dim WithEvents aMenuItem As ToolStripMenuItem
Private Sub RefreshFiles()
Dim dataarray() As String
Dim dataarray2() As String
Dim TempItem As New MenuItem
Dim temp As Integer
For Each dr As Object In System.IO.Directory.GetDirectories("D:\Musik\")
dataarray = dr.ToString().Split("\")
CMenu.Items.Add(dataarray(UBound(dataarray)))
temp = CMenu.Items.Count - 1
aMenuItem = DirectCast(Me.CMenu.Items(temp), ToolStripMenuItem)
For Each fi As Object In System.IO.Directory.GetFiles("D:\Musik\" & dataarray(UBound(dataarray)))
dataarray2 = fi.ToString.Split("\")
aMenuItem.DropDownItems.Add(dataarray2(UBound(dataarray2)))
Next fi
Next
NI.Visible = True
End Sub
Now the problem is that I dont know how to find out if the user has clicked on my submenues! :( Please help
Re: [2005] Context menu click problem!
You need to add an event handler on the Click event of the ToolStripItems you're creating, i.e.
VB Code:
...
Dim tsItem As ToolStripItem = aMenuItem.DropDownItems.Add(dataarray2(UBound(dataarray2)))
AddHandler tsItem.Click, AddressOf OnItemClicked
...
Private Sub OnItemClicked(ByVal sender As System.Object, ByVal e As System.EventArgs)
Me.Text = String.Format("You selected ""{0}""", DirectCast(sender, ToolStripItem).Text)
End Sub
Regards,
- Aaron.
Re: [2005] Context menu click problem!
You actually don't have to use the AddHandler statement. This situation is so common that it is accounted for in the Add method:
VB Code:
aMenuItem.DropDownItems.Add(dataarray2(UBound(dataarray2)), Nothing, New EventHandler(AddressOf OnItemClicked))
The second argument is the image for the menu item, which you can specify if you like.