all groups > vb.net controls > june 2006 >
You're in the

vb.net controls

group:

ContextMenu in Treeview control


ContextMenu in Treeview control Fred Brandli
6/20/2006 6:36:34 PM
vb.net controls:
Using VB.Net 2005

I can display a contextmenu when I right-click on a node and even get the
nodelevel of the node right clcked on.
But, what I can't seem to do is make the contextmenu do anything once click.
I just can't seem to find any reference to the correct event to use.

Any hints, helps or direction to clear, concise but complete (so far as this
is concerned) doc is appreciated.

Thanx, following is my code:

Private Sub treeView1_Mousedown(ByVal sender As Object, ByVal e As
MouseEventArgs) Handles TreeView1.MouseDown

'force selection of right-clicked node

If e.Button = System.Windows.Forms.MouseButtons.Right Then

TreeView1.SelectedNode = TreeView1.GetNodeAt(e.X, e.Y)

End If

End Sub

Private Sub treeView1_MouseUp(ByVal sender As Object, ByVal e As
MouseEventArgs) Handles TreeView1.MouseUp

' If the right mouse button was clicked and released,

' display the context menu assigned to the TreeView.

Dim ctxCT1 As New ContextMenu

If e.Button = System.Windows.Forms.MouseButtons.Right Then

ctxCT1.MenuItems.Clear()

ctxCT1.MenuItems.Add("Item1")

ctxCT1.MenuItems.Add("NodeLevel : " &
TreeView1.SelectedNode.Level.ToString)

ctxCT1.Show(TreeView1, New Point(e.X, e.Y))

End If

Re: ContextMenu in Treeview control GhostInAK
6/21/2006 10:38:53 PM
Hello Fred,

A context menu has a collection of MenuItems.. each MenuItem has a click
event...

-Boo

[quoted text, click to view]

Re: ContextMenu in Treeview control ssta
6/22/2006 8:23:50 PM
The ContextMenu is a property of the TreeView control. You have to
point to an instance of context menu using the ContextMenu property of
the TreeView control.

Since you are using 2005 I would use the ContextMenuStrip instead since
it has more functionality. I also recommend using the IDE to rough-out
your menu. You can then use the Popup event to handle any dynamic
items.

For you dynamic items you will have to use AddHandler to add a handler
for the click event (etc.) to the dynamic menu items..


[quoted text, click to view]
AddThis Social Bookmark Button