Results 1 to 3 of 3

Thread: [RESOLVED] Dynamic Context Menu

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2007
    Location
    Kentucky, USA
    Posts
    82

    Resolved [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

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. Dim parent As MenuItem = mnuContextMenu.MenuItems.Add(Text)
    2.  
    3. parent.MenuItems.Add("Start", AddressOf menuItem_Click)
    4. 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Sep 2007
    Location
    Kentucky, USA
    Posts
    82

    Resolved 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
  •  



Click Here to Expand Forum to Full Width