Right Click Treeview Doesnt Select Node 
Author Message
 Right Click Treeview Doesnt Select Node

What Im trying to do is allow a user to right click on a node so a context
menu is displayed. They then can select the action to perform on that node.
What im finding is that the node is highlighted and the menu pops up but the
selected node is the last node to be left clicked.

Does anyone know how to get round this?



Fri, 29 Apr 2005 23:08:55 GMT  
 Right Click Treeview Doesnt Select Node
Agreed.

Now, here's how to fix it:

The MouseDown will get fired first.

So, we're going to determine whether or not it was a right mouse click. But
before we do that, we're going to create a boolean value that is decalred
just above the formload:

Dim isrightclicked as Boolean
Private Sub TreeView1_MouseDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles TreeView1.MouseDown

If e.Button = MouseButtons.Right then

isrightclicked = True

Else

isrightclicked=false

End If

End Sub

Private Sub TreeView1_AfterSelect(ByVal sender As System.Object, ByVal e As
System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect

If isrightclicked =true then

MyNodeValue = TreeView1.SelectedNode.Text.ToString()

End If

End Sub

HTH


Quote:
> What Im trying to do is allow a user to right click on a node so a context
> menu is displayed. They then can select the action to perform on that
node.
> What im finding is that the node is highlighted and the menu pops up but
the
> selected node is the last node to be left clicked.

> Does anyone know how to get round this?



Fri, 29 Apr 2005 23:47:15 GMT  
 Right Click Treeview Doesnt Select Node
You can actually determine (and cache for using later) which node was
actually clicked during the MouseDownEvent. Here's the code to do it:

    Private MyNode As TreeNode

    Private Sub TreeView1_MouseDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles TreeView1.MouseDown
        If (e.Button = MouseButtons.Right) Then
            MyNode = TreeView1.GetNodeAt(e.X, e.Y)
        Else
            MyNode = Nothing
        End If
    End Sub

Afterwards you can use that information in the context menu click handler:

    Private Sub MenuItem1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MenuItem1.Click
        If Not MyNode Is Nothing Then
            Debug.WriteLine(MyNode.ToString)
        End If
    End Sub

Hope this helps,
Abel
VB .Net Team

--
This posting is provided "AS IS" with no warranties, and confers no rights.



Quote:
> Agreed.

> Now, here's how to fix it:

> The MouseDown will get fired first.

> So, we're going to determine whether or not it was a right mouse click.
But
> before we do that, we're going to create a boolean value that is decalred
> just above the formload:

> Dim isrightclicked as Boolean
> Private Sub TreeView1_MouseDown(ByVal sender As Object, ByVal e As
> System.Windows.Forms.MouseEventArgs) Handles TreeView1.MouseDown

> If e.Button = MouseButtons.Right then

> isrightclicked = True

> Else

> isrightclicked=false

> End If

> End Sub

> Private Sub TreeView1_AfterSelect(ByVal sender As System.Object, ByVal e
As
> System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect

> If isrightclicked =true then

> MyNodeValue = TreeView1.SelectedNode.Text.ToString()

> End If

> End Sub

> HTH



> > What Im trying to do is allow a user to right click on a node so a
context
> > menu is displayed. They then can select the action to perform on that
> node.
> > What im finding is that the node is highlighted and the menu pops up but
> the
> > selected node is the last node to be left clicked.

> > Does anyone know how to get round this?



Sat, 30 Apr 2005 05:05:30 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Right mouse click on Treeview Node

2. Right mouse click on Treeview Node

3. HOW TO: Detect right click on a treeview node

4. Right Click on a TreeView Node?

5. TreeView-Control, activate Node with right mouse click

6. Help: TreeView (Right Mouse Click on node to get popup menu)

7. VB 6 TreeView Clicking empty space to the right of a node

8. Select node with right click

9. TREEVIEW Ctrl :detecting if a node has been selected/clicked

10. treeview right-click to select

11. Right-click select in treeview control

12. Treeview: Right Click ----> Selected

 

 
Powered by phpBB® Forum Software