Treeview: right click doesn't select item?

T

tmaster

I can detect a right click on my treeview, but SelectedNode.Index points to
the last node that was left-clicked. Is there a way for a right-click event
of a treeview to update the SelectedNode.Index?

Private Sub tvwTopics_Mouseup(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles tvwTopics.MouseUp
If e.Button = MouseButtons.Right Then
MsgBox("Right click " &
tvwTopics.Nodes(tvwTopics.SelectedNode.Index).Text)
End If
End Sub

Thanks
 
H

Herfried K. Wagner [MVP]

* "tmaster said:
I can detect a right click on my treeview, but SelectedNode.Index points to
the last node that was left-clicked. Is there a way for a right-click event
of a treeview to update the SelectedNode.Index?

Private Sub tvwTopics_Mouseup(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles tvwTopics.MouseUp
If e.Button = MouseButtons.Right Then
MsgBox("Right click " &
tvwTopics.Nodes(tvwTopics.SelectedNode.Index).Text)
End If
End Sub

\\\
Private Sub TreeView1_MouseUp( _
ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs _
) Handles TreeView1.MouseUp
If e.Button = MouseButtons.Right Then
Dim n As TreeNode = Me.TreeView1.GetNodeAt(e.X, e.Y)
If Not n Is Nothing Then
Me.TreeView1.SelectedNode = n
Me.MenuItem1.Text = n.Text
Else
Me.MenuItem1.Text = "(no item selected)"
End If
Me.ContextMenu1.Show(Me.TreeView1, New Point(e.X, e.Y))
End If
End Sub
///
 
C

Captain Chaos

Here is a workaround for this Problem:

Private Sub ctl_TreeView_MouseUp(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles ctl_TreeView.MouseUp

Dim node As Windows.Forms.TreeNode

node = ctl_TreeView.GetNodeAt(e.X, e.Y)

If node Is Nothing Then Exit Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top