Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Right click select on .Net TreeNode

I am trying to show a popup menu on my treeview when users right click - allowing them to choose context sensitive actions to apply against the selected node.

At the moment the user has to left click node and then right click to choose.

Is it possible to make a right click on a node select that node - and if so what is the best method to do this.

like image 635
Martin Avatar asked Jan 24 '11 16:01

Martin


People also ask

How do I know if TreeView node is selected?

Solution 1Use TreeView. SelectedNode[^] property, which get or set selected node for currently selected Treeview. If no TreeNode is currently selected, the SelectedNode property is a null reference (Nothing in Visual Basic).

What is TreeNode C#?

The TreeNode label is set by setting the Text property explicitly. The alternative is to create the tree node using one of the TreeNode constructors that has a string parameter that represents the Text property. The label is displayed next to the TreeNode image, if one is displayed.

What is TreeNode in VB net?

Advertisements. The TreeView control is used to display hierarchical representations of items similar to the ways the files and folders are displayed in the left pane of the Windows Explorer. Each node may contain one or more child nodes.

How do you edit TreeView?

The TreeView allows you to edit nodes by setting the allowEditing property to true. To directly edit the nodes in place, double click the TreeView node or select the node and press F2 key. When editing is completed by focus out or by pressing the Enter key, the modified node's text saves automatically.


1 Answers

Both left and right clicks fire a click event and cause the selection to change. However, in certain circumstances (that I haven't yet bothered to trace down) the selection will change from the node that was right clicked to the originally selected node.

In order to make sure that the right click changes the selection, you can forcibly change the selected node by using the MouseDown event:

treeView.MouseDown += (sender, args) =>     treeView.SelectedNode = treeView.GetNodeAt(args.X, args.Y); 

A little better, as one of the other posters pointed out, is to use the NodeMouseClick event:

treeView.NodeMouseClick += (sender, args) => treeView.SelectedNode = args.Node; 
like image 195
Kaleb Pederson Avatar answered Sep 23 '22 12:09

Kaleb Pederson