C# HideSelection = False is worthless (windows forms).

By | May 24, 2013

C# is a wonderful language. It’s one of the few things you can honestly compliment Microsoft for creating without feeling like a fan boy.  However, as with all things (particularly MS things) they still go one step short or do the odd thing here and there.

One case in point is persistent highlighting of a selected item in a treeview. It’s not designed to operate like the rest of the OS. In fact, HideSelection, which is described as “Gets or sets a value indicating whether the selected item remains highlighted even when the control has lost the focus”; is turned OFF by default on C# Forms. In Windows itself, the behavior is ON… such as on Explorer, listbox selection items, etc.

No problem then, just find the HideSelection property on the control and set it to False (meaning the selection isn’t hidden when focus is lost), right? Wrong. In nearly all of the system styles by MS, the control background is white and the selecteditem when focus is lost is LIGHT GREY. Many people think HideSelection is just broken, but it in fact works.. it’s just that the highlight color is so close to white that it does not seem to work (this becomes obvious if you change the background color on the treeview to black). This problem is exacerbated by the fact that you cannot set this color property directly.

While searching across the internet for a easy workaround, I was stumped. There are several posts, but they are all needlessly complex or have 20 wrong responses and 1 half -right one. That’s not really a common thing for c# as it is such a popular language you can usually find a solution via google in a second or two for nearly anything. So, since it was so frustrating.. I thought I’d post my example.

First add a treeview to your form. For this example, use default naming for everything. Just create a new Forms project, and add a treeview to the form. Figure out how to port it to your project afterwards, it’ll just require some renaming to match your control names.

Make sure that HideSelection is set to TRUE. We already established HideSelection is worthless, so we’re going to do it ourselves.

In your code, define a public TreeNode labeled previousNode:

namespace WindowsFormsApplication1
    public partial class Form1 : Form
        TreeNode previousNode;

        public Form1()

Second, add the overload methods for handling treeview’s AfterSelect and Click events.

public Form1()

            this.treeView1.AfterSelect += this.treeView1_AfterSelect;
            this.treeView1.Click += new System.EventHandler(this.treeView1_Click);

Finally, define your treeView1_Click and treeView1_AfterSelect methods. We will use treeView1_Click to keep track of the previousNode, and we will use treeView1_AfterSelect to simultaneously set the color of the currently selected item and restore the previous item back to it’s original color.

       private void treeView1_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)
            TreeNode node = treeView1.SelectedNode;
            node.BackColor = Color.DodgerBlue;
            node.ForeColor = Color.White;
            previousNode.BackColor = Color.White;
            previousNode.ForeColor = Color.Black;


        private void treeView1_Click(object sender, System.EventArgs e)
            TreeNode node = treeView1.SelectedNode;
            previousNode = treeView1.SelectedNode;

That’s IT! Done. Now when the treeview loses focus, the last selected item will remain highlighted.

2 thoughts on “C# HideSelection = False is worthless (windows forms).

  1. Ragesh

    Better to check the previous node before assigning color. I’m getting selected node as null in that click event.

Leave a Reply

Your email address will not be published. Required fields are marked *