Click here to Skip to main content
15,909,242 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am adding new nodes to the already existing nodes(this is parent) of treeview. The newly added nodes will be under the parent. I want to set the focus on the newly added node, but not on that parent.

Can anyone assist me.

Thanks in advance
Posted

You might be confused because you mix up two different concepts: focusing and selection.

"Focusing" means getting keyboard focus, nothing else; showing of a visual clue of the focus is just one aspect of it. Normally the whole control is focused (I don't consider composite controls here, they can have children controls and only one of the "elementary" controls can be focused).

The TreeView control is the control which can be focused only as a whole control. Its nodes can be selected. Note that the nodes remain selected when the control is focused or not. You might argue: "What happens during in-place editing of the node text? It looks like the node is focused." No, node is never focused. Actually, when an in-place edition happens, whole tree view looses its focus, because a separate in-place text editor is created at the location of a tree node and temporarily grabs the focus.

You can get or set selected node using the property System.Windows.Forms.TreeView.SelectedNode, see http://msdn.microsoft.com/en-us/library/system.windows.forms.treeview.aspx[^].

See also the property affecting selection and presentation of selection:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.canselect.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.treeview.fullrowselect.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.treeview.hideselection.aspx[^].

Most likely you will also need events raised on change in selection:
http://msdn.microsoft.com/en-us/library/system.windows.forms.treeview.onafterselect.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.treeview.onbeforeselect.aspx[^].

Basically, that's all what involved.

—SA
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900