|
-
Sep 20th, 2007, 10:52 AM
#1
Thread Starter
Lively Member
[RESOLVED] Dynamic Context Menu
I am trying to create a context menu dynamically the problem is I can get the menuItem_Click sub to trigger. Here is my code:
Code:
Private Sub FindVMX(ByVal strPath As String)
Dim oDir As New System.IO.DirectoryInfo(strPath)
Dim oFiles() As System.IO.FileInfo
Dim i As Integer
Dim tmpVar As Integer
Dim SR As StreamReader
Dim DisplayName As String
lstVirtualMachines.Items.Clear()
oFiles = oDir.GetFiles("*.vmx", SearchOption.AllDirectories)
For i = 0 To oFiles.Length - 1
tmpVar = Strings.InStr(oFiles(i).Name.ToString(), ".")
If Strings.Mid(oFiles(i).Name.ToString(), tmpVar) = ".vmx" Then
lstVirtualMachines.Items.Add(oFiles(i).DirectoryName & "\" & oFiles(i).Name.ToString)
End If
Next
ReDim VirtualMachines(lstVirtualMachines.Items.Count)
For i = 0 To lstVirtualMachines.Items.Count - 1
VirtualMachines(i) = lstVirtualMachines.Items.Item(i)
SR = New StreamReader(lstVirtualMachines.Items.Item(i).ToString)
Dim Line As String
Do
Line = SR.ReadLine
If Strings.InStr(Line, "displayName", CompareMethod.Text) > 0 Then
DisplayName = Strings.Mid(Line, 16)
DisplayName = Strings.Mid(DisplayName, 1, DisplayName.Length - 1)
lstVirtualMachines.Items.RemoveAt(i)
lstVirtualMachines.Items.Insert(i, DisplayName)
'Create Context Menu Item
AddContextMenu(DisplayName, 1)
End If
Loop Until Line Is Nothing
SR.Close()
Next
End Sub
Public Sub AddContextMenu(ByVal Text As String, Optional ByVal SubMenu As Integer = 0)
Dim mnuItem As New MenuItem
mnuItem.Text = Text
mnuContextMenu.MenuItems.Add(mnuItem)
If SubMenu = 1 Then
Dim mnuItemStart As New MenuItem()
Dim mnuItemStop As New MenuItem()
mnuItemStart.Text = "Start"
mnuItemStop.Text = "Stop"
mnuItem.MenuItems.Add(mnuItemStart)
mnuItem.MenuItems.Add(mnuItemStop)
End If
Debug.WriteLine(mnuContextMenu.MenuItems.Count)
AddHandler mnuItem.Click, AddressOf Me.menuItem_Click
NotifyIcon1.ContextMenu = mnuContextMenu
End Sub
Private Sub menuItem_Click(ByVal sender As Object, ByVal e As System.EventArgs)
MsgBox(sender.ToString)
End Sub
-
Sep 20th, 2007, 07:30 PM
#2
Re: Dynamic Context Menu
You are creating three menu items (one parent and two children), yet you're only attaching the event handler to the parent. That means the event handler will be executed when you click the parent menu item, but what of the children? You don't attach any event handlers to them so nothing will happen when they are clicked.
Also, you're using an unnecessarily long-winded method of adding the items. It's as easy as this:
vb.net Code:
Dim parent As MenuItem = mnuContextMenu.MenuItems.Add(Text)
parent.MenuItems.Add("Start", AddressOf menuItem_Click)
parent.MenuItems.Add("Stop", AddressOf menuItem_Click)
You tell the parent the name of the item and the handler for its Click event and the item is created for you. Simple methods like this are usually preferable to multiple steps as you were using.
-
Sep 21st, 2007, 11:55 AM
#3
Thread Starter
Lively Member
Re: Dynamic Context Menu
Thanks, That answered my question!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|