2

Quite a simple question but I think its going to prove much harder than it sounds

I'd like to keep the selection of a listview item there when the focus leaves the list view, at the moment I've set the hideselection property to false and that's fine.. it does cause a VERY light gray selection to stay after the list view loses focus, so my question is, how can I properly show that that item is still selected so that the user will recognize that, something like changing the rows text colour or background colour? or just keeping it highlighted as when first selected the whole row turns blue?

I've had a look through intelisense and can't seem to find anything for a row or item or selected item's individual colour property?

It must exist though because selected items have their own background colour, where would I be able to change that?

Oh and the list view does need to stay in details view, which means I can't use the only method that I've been able to find whilst googling

thanks

Alex
  • 718
  • 3
  • 11
  • 26
  • possible duplicate of [How to change listview selected row backcolor even when focus on another control?](http://stackoverflow.com/questions/5179664/how-to-change-listview-selected-row-backcolor-even-when-focus-on-another-control) – George Stocker Jun 06 '12 at 19:19
  • yeah, I flagged the question for mods because I'm still new to the site and unsure of what to do – Alex Jun 07 '12 at 10:51
  • Too broad, you have to tag it with appropriate technology (e.g. winforms, wpf, etc.). – Sinatr Aug 03 '15 at 12:43

2 Answers2

4

Here's a solution for a ListView that does not allow multiple selections and does not have images (e.g. checkboxes).

  1. Set event handlers for the ListView (in this example it's named listView1):
    • DrawItem
    • Leave (invoked when the ListView's focus is lost)
  2. Declare a global int variable (i.e. a member of the Form that contains the ListView, in this example it's named gListView1LostFocusItem) and assign it the value -1
    • int gListView1LostFocusItem = -1;
  3. Implement the event handlers as follows:

    private void listView1_Leave(object sender, EventArgs e)
    {
        // Set the global int variable (gListView1LostFocusItem) to
        // the index of the selected item that just lost focus
        gListView1LostFocusItem = listView1.FocusedItem.Index;
    }
    
    private void listView1_DrawItem(object sender, DrawListViewItemEventArgs e)
    {
        // If this item is the selected item
        if (e.Item.Selected)
        {
            // If the selected item just lost the focus
            if (gListView1LostFocusItem == e.Item.Index)
            {
                // Set the colors to whatever you want (I would suggest
                // something less intense than the colors used for the
                // selected item when it has focus)
                e.Item.ForeColor = Color.Black;
                e.Item.BackColor = Color.LightBlue;
    
               // Indicate that this action does not need to be performed
               // again (until the next time the selected item loses focus)
                gListView1LostFocusItem = -1;
            }
            else if (listView1.Focused)  // If the selected item has focus
            {
                // Set the colors to the normal colors for a selected item
                e.Item.ForeColor = SystemColors.HighlightText;
                e.Item.BackColor = SystemColors.Highlight;
            }
        }
        else
        {
            // Set the normal colors for items that are not selected
            e.Item.ForeColor = listView1.ForeColor;
            e.Item.BackColor = listView1.BackColor;
        }
    
        e.DrawBackground();
        e.DrawText();
    }
    

Note: This solution can result in some flicker. A fix for this involves subclassing the ListView control so you can change the protected property DoubleBuffered to true.

public class ListViewEx : ListView
{
    public ListViewEx() : base()
    {
        this.DoubleBuffered = true;
    }
}

I created a class library of the above class so that I could add it to the toolbox.

D Walker
  • 167
  • 1
  • 6
1

A possible solution might be this answer to an another question:

How to change listview selected row backcolor even when focus on another control?

Community
  • 1
  • 1
  • Seems wrong to give you the correct answer but yes that's exactly what I was looking for lol! – Alex Jun 06 '12 at 14:21
  • Well there are a few things that that answer doesn't cover but should definitely be a good starting point – Alex Jun 06 '12 at 14:24