Results 1 to 4 of 4

Thread: ContextMenu - Popup

  1. #1

    Thread Starter
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    ContextMenu - Popup

    I am using the following code to display a popup menu for a particular node of a treeview:

    VB Code:
    1. Private Sub TreeView1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TreeView1.MouseUp
    2.         Dim mnuContextMenu As New ContextMenu()
    3.         If e.Button = Windows.Forms.MouseButtons.Right Then
    4.             Dim mnuItemNew() As MenuItem = New MenuItem() _
    5.                         {New MenuItem("Item 1"), _
    6.                         New MenuItem("Item 2"), _
    7.                         New MenuItem("Item 3")}
    8.  
    9.             Dim TVWMenu As New ContextMenu(mnuItemNew)
    10.             TVWMenu.Show(TreeView1, New Point(e.X, e.Y))
    11.         End If
    12.     End Sub

    My question is how do I have a separator line appear Item 2 and Item 3? How can I tell which item was clicked? I have tried the following code but it didn't work:

    VB Code:
    1. Private Sub ContextMenuStrip1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ContextMenuStrip1.Click
    2.         MsgBox(ContextMenuStrip1.Text)
    3.     End Sub

    Thanks
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


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

    Re: ContextMenu - Popup

    If you want to place a seperator in a menu you just add an item and make its text a single dash. You handle the Click event of each menu item individually. To create a Click event handler for an item just double-click it (the item, not the menu) in the designer.
    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
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: ContextMenu - Popup

    But if your creating the menu items dynamically then you need to use the AddHandler/RemoveHandler methods.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  4. #4

    Thread Starter
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Re: ContextMenu - Popup

    Quote Originally Posted by RobDog888
    But if your creating the menu items dynamically then you need to use the AddHandler/RemoveHandler methods.

    Rob I found this example here:

    VB Code:
    1. Sub TestEvents()
    2.     Dim Obj As New Class1
    3.     ' Associate an event handler with an event.
    4.     AddHandler Obj.Ev_Event, AddressOf EventHandler
    5.     ' Call the method to raise the event.
    6.     Obj.CauseSomeEvent()
    7.     ' Stop handling events.
    8.     RemoveHandler Obj.Ev_Event, AddressOf EventHandler
    9.     ' This event will not be handled.
    10.     Obj.CauseSomeEvent()
    11. End Sub
    12.  
    13. Sub EventHandler()
    14.     ' Handle the event.
    15.     MsgBox("EventHandler caught event.")
    16. End Sub
    17.  
    18. Public Class Class1
    19.     ' Declare an event.
    20.     Public Event Ev_Event()
    21.     Sub CauseSomeEvent()
    22.         ' Raise an event.
    23.         RaiseEvent Ev_Event()
    24.     End Sub
    25. End Class

    But I can't figure out how to implement could you help me out?

    Thanks
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


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