Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WinForms TreeView - how to manually "highlight" node (like it was clicked)

I would need to know how to let the programatically selected node make graphically in the state "selected" like the user clicked on it. SelectedNode only makes this one internally selected. Thank you very much!

like image 204
Petr Avatar asked Dec 03 '09 09:12

Petr


People also ask

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.

What is TreeView node?

The Nodes property holds a collection of TreeNode objects, each of which has a Nodes property that can contain its own TreeNodeCollection. This nesting of tree nodes can make it difficult to navigate a tree structure, but the FullPath property makes it easier to determine your location within the tree structure.


2 Answers

The reason it does not show as highlighted is due to the tree view not having focus. This is in a button click event on my test form:

TreeView1.SelectedNode = TreeView1.Nodes(2); TreeView1.Focus(); 

Which highlights the node properly. if you remove the Focus(); call it doesn't highlight until you click into the tree view (anywhere in the tree view, not necessarily on to the node that you want to be selected).

like image 128
Pondidum Avatar answered Sep 27 '22 20:09

Pondidum


TreeView1.SelectedNode.BackColor = SystemColors.HighlightText; // This will work

Above solutions will only set the focus on it but will not change the highlight view of it.

like image 26
Mohit Agarwal Avatar answered Sep 27 '22 22:09

Mohit Agarwal