Results 1 to 3 of 3

Thread: [2005] Context menu click problem!

  1. #1

    Thread Starter
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    [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:
    1. Dim WithEvents aMenuItem As ToolStripMenuItem
    2.  
    3. Private Sub RefreshFiles()
    4.         Dim dataarray() As String
    5.         Dim dataarray2() As String
    6.         Dim TempItem As New MenuItem
    7.         Dim temp As Integer
    8.         For Each dr As Object In System.IO.Directory.GetDirectories("D:\Musik\")
    9.             dataarray = dr.ToString().Split("\")
    10.             CMenu.Items.Add(dataarray(UBound(dataarray)))
    11.             temp = CMenu.Items.Count - 1
    12.             aMenuItem = DirectCast(Me.CMenu.Items(temp), ToolStripMenuItem)
    13.             For Each fi As Object In System.IO.Directory.GetFiles("D:\Musik\" & dataarray(UBound(dataarray)))
    14.                 dataarray2 = fi.ToString.Split("\")
    15.                 aMenuItem.DropDownItems.Add(dataarray2(UBound(dataarray2)))
    16.             Next fi
    17.         Next
    18.         NI.Visible = True
    19.     End Sub

    Now the problem is that I dont know how to find out if the user has clicked on my submenues! Please help
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  2. #2
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177

    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:
    1. ...
    2.             Dim tsItem As ToolStripItem = aMenuItem.DropDownItems.Add(dataarray2(UBound(dataarray2)))
    3.             AddHandler tsItem.Click, AddressOf OnItemClicked
    4.             ...
    5.  
    6.  
    7.   Private Sub OnItemClicked(ByVal sender As System.Object, ByVal e As System.EventArgs)
    8.       Me.Text = String.Format("You selected ""{0}""", DirectCast(sender, ToolStripItem).Text)
    9.    End Sub
    Regards,

    - Aaron.

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

    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:
    1. 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.
    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

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