Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# how do I ensure the selected node remains highlighted when focus lost

Tags:

c#

treeview

I have changed the Treeview.HideSelection = false; But how do I insure that when focus is lost that the selected item remains the original selected color?

EDIT:

I have a listview on a form that holds a list of process events. Alongside the Treeview on the same form is a series of selections that the user completes to classify the event in the listview. However, when the user makes a selection on one of the classification controls the blue highlighted selected Treeview item turns to a grey color. I was hoping to find the property that defines this color to make it the same color blue.

Any suggestions.

Update:

 public partial class myTreeView : TreeView
{
    TreeNode tn = null;
    public myTreeView()
    {
        InitializeComponent();
    }

    protected override void OnAfterSelect(TreeViewEventArgs e)
    {
        if (tn != null)
        {
            tn.BackColor = this.BackColor;
            tn.ForeColor = this.ForeColor;
        }
        tn = e.Node;
        base.OnAfterSelect(e);
    }
    protected override void OnBeforeSelect(TreeViewCancelEventArgs e)
    {

        e.Node.BackColor = Color.Green;
        e.Node.ForeColor = Color.White;
        base.OnBeforeSelect(e);
    }
    protected override void OnGotFocus(System.EventArgs e)
    {

        base.OnGotFocus(e);
    }

    protected override void OnLostFocus(System.EventArgs e)
    {

        if (tn != null)
        {
            tn.BackColor = Color.Green;
            tn.ForeColor = Color.White;
        }
        // tn.BackColor = Color.Red;

        base.OnLostFocus(e);
    }
}
like image 782
Brad Avatar asked Jan 19 '09 00:01

Brad


2 Answers

Setting ListView.HideSelection to true means that when focus is lost, it will hide the selection. By setting HideSelection to false, the selected item will still have the color indicator showing which item is selected.

like image 192
scottm Avatar answered Sep 20 '22 12:09

scottm


Generally, you don't. The change in color is one of the visual cues that indicate which control has the focus. Don't confuse your customers by getting rid of that.

If you want to buck the convention, then you can make your control owner-drawn, and then you can paint the items whatever color you want.

Another option, in your case, is to use a drop-down combo box instead of a list box. Then the current selection is always clear, no matter whether the control has the focus. Or, you could consider using a grid, where each event has all its settings given separately, and then "selection" doesn't matter at all.

like image 41
Rob Kennedy Avatar answered Sep 18 '22 12:09

Rob Kennedy