Spark Spark - 1 year ago 282
C# Question

TreeNode Selected BackColor while TreeView not focused

Is there an easy way to have a selected TreeNode retain its SystemColors.Highlight BackColor while the TreeView doesn't have focus? Because even with HideSelection set to false, the selected BackColor is near impossible to see.

Selected TreeNode while TreeView has focus:


Selected TreeNode while TreeView does not have focus:


Thanks in advance.

EDIT: I'm aware i could set DrawMode to OwnerDrawAll and then add a custom DrawNode event. I did attempt this previously, the problem i have is i don't know how to go about drawing the TreeNode's corresponding ImageKey properly.

Answer Source

The solution, works like a charm:

public TreeNode previousSelectedNode = null;
private void treeView1_Validating(object sender, System.ComponentModel.CancelEventArgs e)
    treeView1.SelectedNode.BackColor = SystemColors.Highlight;
    treeView1.SelectedNode.ForeColor = Color.White;
    previousSelectedNode = treeView1.SelectedNode;

private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
    if(previousSelectedNode != null)
        previousSelectedNode.BackColor = treeView1.BackColor;
        previousSelectedNode.ForeColor = treeView1.ForeColor;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download