Treeview context menus don't seem to work right.

J

Jonathan Miller

I'm using vb.net and the treeview control. I have a created a context menu with a few items on it and set the context menu property of my treeview control to my context menu. Here is the problem: If I right-click on a node that is not currently the selectednode the following happens: The right-clicked node becomes highlighted. (correct) The context menu appears on top of the right-clicked node. (correct) The selectedNode property of the treeview is last selected node, not the right-clicked node! (not good!). So basically, how the heck to I figure our which node was right-clicked on to bring up the context menu? If the selectedNode property is not accurate, what do I use? Any help on this one would be appreciated!
 
G

Guest

Hi Jonathan

You can select the node that you right clicked on prior to the context menu being displayed. This way, when you display the context menu the treeView.SelectedNode will be the one you want. To do this, add a MouseDown event handler to your tree view as below

private void treeView_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e

if ( e.Button == MouseButtons.Right

TreeNode node = treeView.GetNodeAt( e.X, e.Y )

if ( node != null

treeView.SelectedNode = node




HT

RS
 
H

Herfried K. Wagner [MVP]

* "Jonathan Miller said:
I'm using vb.net and the treeview control. I have a created a context
menu with a few items on it and set the context menu property of my
treeview control to my context menu. Here is the problem: If I
right-click on a node that is not currently the selectednode the
following happens: The right-clicked node becomes
highlighted. (correct) The context menu appears on top of the
right-clicked node. (correct) The selectedNode property of the treeview
is last selected node, not the right-clicked node! (not good!).

Quick and (very) Dirty:

\\\
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
///
 
G

Guest

I have a similar problem, but I haven't started working
on fixing it yet.

One idea comes to mind. Use the treeview click event and
test for the right mouse button. Use e.X and e.Y
properties with GetNodeAt to return the correct node.
Then display the context menu programatically and
continue from there.

Basically, take manual control of the process.

Jason

-----Original Message-----
I'm using vb.net and the treeview control. I have a
created a context menu with a few items on it and set the
context menu property of my treeview control to my
context menu. Here is the problem: If I right-click on a
node that is not currently the selectednode the following
happens: The right-clicked node becomes highlighted.
(correct) The context menu appears on top of the right-
clicked node. (correct) The selectedNode property of the
treeview is last selected node, not the right-clicked
node! (not good!). So basically, how the heck to I
figure our which node was right-clicked on to bring up
the context menu? If the selectedNode property is not
accurate, what do I use? Any help on this one would be
appreciated!
 

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