Expand Windows Forms TreeView node without selecting it

When I expand the TreeView node by clicking on the plus sign on it, node is selected. How can i avoid this? I want to be able to expand nodes without changing the selected node (for example, in RegEdit.exe, for example), and only change the selection when I click on node.

(Forgive me for what seems to be the main question - I searched, but didn't find anything. Any pointers or links are welcome.)

+4
source share
2 answers

I believe there is a BeforeSelect event that you can connect to, which should allow you to deselect the node if the selected node has children.

private void MyTreeView_BeforeSelect(object sender, TreeViewCancelEventArgs e) { If (nodeWithChildren) e.Cancel = True } 
+1
source

A bit late for a party with this.

You can use the Hit test.

  private void myTreeView_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e) { TreeViewHitTestInfo info = myTreeView.HitTest(e.Location); if (info.Location == TreeViewHitTestLocations.Label) { TreeNode node = myTreeView.GetNodeAt(e.Location); //do something } } 

If you select a label, only node will be selected.

Hope this helps.

0
source

All Articles