TreeView and Context Menu Problem

  • 15 years ago

    I have a context menu set on my treeview. The problem is that the user needs to left click the node first to select it and then right click to bring up the context menu. Is there anyway i can make the right click select the node as well as bring up the context menu?

  • 15 years ago
    Did a bit of digging, and it looks like you need to capture the value of the right mouse click, and select the node at that point. See this code snippet (Grabbed from tek-tips.com)


    Code:

    Private Sub TreeView1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TreeView1.MouseDown
       Dim dp As System.Drawing.Point

       If e.Button = MouseButtons.Right Then

           'select node that is being right-clicked
           TreeView1.SelectedNode = TreeView1.GetNodeAt(e.X, e.Y)

           'Get the nesting level of the selected node
           NodeLevel = GetNodeNestingLevel(TreeView1.SelectedNode)

           'only display context menu on lowest-level nodes
           If NodeLevel = 3 Then
               TreeView1.ContextMenu = TVMenu
           Else
               TreeView1.ContextMenu = Nothing
           End If
       End If

       NodeLevel = -1
    End Sub


    Hope that helps. Check the link above for more information if you need it.
  • 15 years ago
    Thanks heaps, that is what i was after. I only needed one bit but it works like a charm. I couldn't find anything when i looked the other day.

    Code:
    If e.Button = MouseButtons.Right Then
       tvwNSD.SelectedNode = tvwNSD.GetNodeAt(e.X, e.Y)
    End If


    Thanks again for your time.
  • 15 years ago

    This question was asked at the site Bigbeanpole said: tek-tips.com. If you go there it only has this part of the code that they have shown. Maybe you can contact the person that posted the code at the other forum.

  • 15 years ago

    Quote:
    [1]Posted by jonorossi on 30 Jun 2005 05:54 AM[/1]
    This question was asked at the site Bigbeanpole said: tek-tips.com. If you go there it only has this part of the code that they have shown. Maybe you can contact the person that posted the code at the other forum.

    i just saw that and i removed my question.

  • 15 years ago
    Sorry, but i am usually pretty quick to reply

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.” - Rich Cook