Results 1 to 2 of 2

Thread: [2005] Tree view nodes and context menu

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2007
    Posts
    23

    [2005] Tree view nodes and context menu

    I have a tree view with items which each have a ContextMenuStrip. How do i know on which tree node the user activated the context menu when handling events like clicks on items in the context menu?

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

    Re: [2005] Tree view nodes and context menu

    I was intrigued by your question so I had a wee look. The SourceControl property of the menu is no use because that refers to the TreeView containing the node. I don't know if there's a better way but this is what I came up with:
    vb Code:
    1. Private Sub TreeView1_MouseDown(ByVal sender As Object, _
    2.                                 ByVal e As System.Windows.Forms.MouseEventArgs) Handles TreeView1.MouseDown
    3.     If e.Button = Windows.Forms.MouseButtons.Right Then
    4.         Me.ContextMenuStrip1.Tag = Me.TreeView1.GetNodeAt(e.Location)
    5.     End If
    6. End Sub
    7.  
    8. Private Sub Item1ToolStripMenuItem_Click(ByVal sender As System.Object, _
    9.                                          ByVal e As System.EventArgs) Handles Item1ToolStripMenuItem.Click
    10.     Dim sourceNode As TreeNode = TryCast(Me.ContextMenuStrip1.Tag, TreeNode)
    11.  
    12.     If sourceNode IsNot Nothing Then
    13.         MessageBox.Show("Source node: " & sourceNode.Text)
    14.     End If
    15. End Sub
    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