Treeview Question

R

Randall Hale

I'm trying to dynamically create values for a context menu (popup),
depending upon which node is highlighted in a Treeview control. This is not
a problem, if you select the node with the left mouse button and THEN
right-click (the context menu then displays an item IAW a Tag value
previously assigned to each node). If you only use the right mouse button,
however, it appears that the given node is selected but this is not the
case; it is only highlighted, and all you get is the old value for the
previously selected node, which had to have been selected via the left mouse
button first.

A one-click process seems more natural and intuitive to me; and it should be
possible to select a given node with either mouse button. Any suggestions
as to how to achieve this. Lots of hair pulling so far but no luck.

Regards,

Randall Hale
 
H

Herfried K. Wagner [MVP]

* "Randall Hale said:
I'm trying to dynamically create values for a context menu (popup),
depending upon which node is highlighted in a Treeview control. This is not
a problem, if you select the node with the left mouse button and THEN
right-click (the context menu then displays an item IAW a Tag value
previously assigned to each node). If you only use the right mouse button,
however, it appears that the given node is selected but this is not the
case; it is only highlighted, and all you get is the old value for the
previously selected node, which had to have been selected via the left mouse
button first.

That's a FAQ:

<http://www.google.com/groups?ie=UTF-8&q=Contextmenu+treeview+group:*dotnet*>

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>

Improve your quoting style:
<http://learn.to/quote>
<http://www.plig.net/nnq/nquote.html>
 
R

Randall Hale

Thanks, Herrfried. That did the trick (I've been out of the loop and hadn't
seen this question before).

For quick reference for others, this is what finally worked for my
situation:

Sub ContextMenu1_PopUp ( ByVal o As Object, ByVal e As System.EventArgs )
Handles ContextMenu1.Popup
Dim clickedNode As TreeNode = Treeview1.GetNodeAt (
Treeview1.PointToClient ( Cursor.Position ) )
With ContextMenu1.MenuItems
.Clear ( )
.Add ( clickedNode.Tag)
End With
End Sub

Pretty simple but sequence IS scored. I tried something similar during the
MouseUp event for the Treeview control; passing a Point derived from the
MouseEventArgs to the Treeview's GetNodeAt method, as the documentation
suggests. That doesn't help here, however, because the menu pops up before
the MouseUp is called. I knew I was missing something obvious. ;-)

Thanks All!

Randall hale
 

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