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!
-
sorry but i needed to comment on answers 1 and 6 should be node[2] not node (2) answer #4 put node[2] the reason i'm saying this is because i can't comment on the existing answer as i lack rep points. More importantly, i think we all check the first answer as it has the most points. when we try the code and it doesn't work we move on to the lower answers or another post. This is a 10 year old question. It's not like I'm going to get any points here :D I just hope you'll fix that error in those two answers and delete my post so that whoever decides to try their code, won't face such issues. tha – user1242840 Mar 13 '20 at 18:38
9 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).

- 11,457
- 8
- 50
- 69
-
14Also the property HideSelection can be used. If set to false the highlight will be shown regardless of focus. – Goran Dec 09 '09 at 20:09
-
There are drawbacks to this. We have been using similar approach however find that if the user cancels the drag the wrong node is selected. We tried tracking last node selected this and reselecting in dragleave but then the list scrolls to the original item if you drag out. – Adam Butler Nov 08 '11 at 01:38
-
@Goran: This is what I was looking for! Why is this `true` by default?? – Tobias Knauss Feb 08 '21 at 17:05
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.

- 150,114
- 66
- 286
- 303

- 51
- 1
- 1
-
1This actually worked for me, Thanks! I changed the code a little bit: `TreeView1.SelectedNode.BackColor = SystemColors.Highlight;` `TreeView1..SelectedNode.ForeColor = SystemColors.HighlightText;` – gridtrak Mar 02 '16 at 16:54
-
This is nice because it works even when a programmatically selected node shows `TreeView1.SelectedNode.IsSelected == false` – gridtrak Mar 02 '16 at 17:11
This works for me for .net 3.5: Set the treeview component's DrawMode property to: OwnerDrawAll Then in the DrawNode event write the following:
if (((e.State & TreeNodeStates.Selected) != 0) && (!MyTreeView.Focused))
e.Node.ForeColor = Color.Blue;
else
e.DrawDefault = true;
And in the BeforeSelect event have:
if (MyTreeView.SelectedNode != null)
MyTreeView.SelectedNode.ForeColor = Color.Black;
e.Node.ForeColor = Color.Blue;
Here is what I got to work:
void myProcedure()
{
// Hookup a DrawMode Event Handler
this.myTV.DrawNode += myTV_DrawNode;
// Set DrawMode and HideSelection
this.myTV.DrawMode = TreeViewDrawMode.OwnerDrawText;
this.myTV.HideSelection = false;
// Make sure the TreeView has Focus
this.myTV.Focus();
// Make sure the TreeView is Selected
this.myTV.Select();
// If the TreeView has a Node, I want to select the first Node to demonstrate.
if (this.myTV.Nodes.Count > 0)
{
// Make sure the node is visible
this.myTV.Nodes[0].EnsureVisible();
// Make sure the Node is Selected
this.myTV.SelectedNode = myTV.Nodes[0];
}
// Make sure the SelectedNode IS the Node that we programmatically want to select.
textBox1.Text = this.myTV.SelectedNode.Text;
// if we display sanityCheck1 string, it actually is the correct node.text
// Make sure .NET runtime knows the Node is selected
textBox1.Text += " is Selected = " + this.myTV.SelectedNode.IsSelected.ToString();
}
Following up: laalto answered the How to HighLight the TreeView.Node. The following code in the DrawNode Event Handler, from samball's answer, properly highlights the TreeView.Node based on its Selected State.
private void myTV_DrawNode(object sender, DrawTreeNodeEventArgs e)
{
// first, let .NET draw the Node with its defaults
e.DrawDefault = true;
// Now update the highlighting or not
if (e.State == TreeNodeStates.Selected)
{
e.Node.BackColor = SystemColors.Highlight;
e.Node.ForeColor = SystemColors.HighlightText;
}
else
{
e.Node.BackColor = ((TreeView)sender).BackColor;
e.Node.ForeColor = ((TreeView)sender).ForeColor;
}
}
Platform = C# .NET 4.5 in Windows 10, Visual Studio 2015

- 731
- 7
- 20
TreeView1.SelectedNode = TreeView1.Nodes(2);
this.ActiveControl = TreeView1;
This works for me (.net 4.7)

- 317
- 4
- 17
I had an similar issue and wanted to have a TreeView
node selected (highlighted) on form load.
Maybe someone has the same problem, too.
I first tried Pondidum's solution. Without success.
But then I found the solution in another thread: Simply set the TabIndex
of the TreeView
to 0.
In that case you don't need to set the focus. Just choose the node that should be selected by using SelectedNode
and set the TabIndex
. That's it.

- 3,741
- 12
- 49
- 72
The underlying Win32 control supports this (think it's TVIS_DROPHILITED
), but I can't see the same functionality exposed through the TreeView
control.
As theraneman says, you could fake it with the TreeNode.ForeColor
and BackColor
properties...

- 53,480
- 10
- 121
- 138
Not sure, but can you not change the background color of that node?

- 1,620
- 4
- 18
- 32
-
That would be problematical, I would need to ensure its set back when other node is selected etc. – Petr Dec 03 '09 at 09:55
-
Trying this I find that I get a lot of flicker of the tree when dragging. – Adam Butler Nov 08 '11 at 02:40