
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?