Brad
Brad

Reputation: 21210

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

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);
    }
}

Upvotes: 7

Views: 17227

Answers (5)

Ploy
Ploy

Reputation: 1

I use this code; it works for me.

design: Mytreeview.HideSelection = True you will manual highlight the lose focus selected node.

Private Sub MyTreeview_Leave(sender As Object, e As EventArgs) Handles MyTreeview.Leave
    MyTreeview.SelectedNode.BackColor = Color.LemonChiffon
End Sub

Private Sub MyTreeview_BeforeSelect(sender As Object, e As TreeViewCancelEventArgs) Handles MyTreeview.BeforeSelect
    If MyTreeview.SelectedNode IsNot Nothing Then 
        MyTreeview.SelectedNode.BackColor = Color.White
End Sub

Upvotes: 0

dtyrant
dtyrant

Reputation: 1

I like the HideSelection = false; answer, because:

  1. It's easy

  2. I have a search function that cycles through the nodes and marks the relevant ones by changing it's background to yellow, when the user clicks on the node a textbox fills with the relevant info attached to that node, before I used this method, if the user clicked on the textbox to scroll through it, it would unhighlight the node and make it hard to keep track of which node was selected, this way it is still highlighted in a light gray colour showing it is not in focus, opposed to the blue highlight which is used when it is in focus. I could have 'painted' the node but with the yellow background for search results would have made life more complicated than it needed to be.

  3. Did I mention it was easy?

Upvotes: -2

scottm
scottm

Reputation: 28699

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.

Upvotes: 12

Rob Kennedy
Rob Kennedy

Reputation: 163357

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.

Upvotes: 1

Schmuli
Schmuli

Reputation: 743

If I were doing it, I would simply have an extra Label alongside the ListView, above the classification controls being selected, that would indicate which process event has been selected. You can also use said Label to add extra details about the event (if any).

This way, you are sticking to standard UI conventions and making it that much clearer to the user what their current selection is.

Upvotes: 0

Related Questions