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

c# listview item background color
how to change listview row color in c#
how to change background color of selected item in listview wpf
listview selected item color change
listview selected item color xamarin forms
change listview selected item background in xamarin forms
wpf listview change row color programmatically
c# listview selected item not highlighted

I have a program which uses a barcode scanner as input device so that means I need to keep the focus on a text box.

The program has a listview control and I select one of the items programatically when a certain barcode is scanned. I set the background color of the row by:

listviewitem.BackColor = Color.LightSteelBlue;

Things I have tried:

  • listview.HideSelection set to false
  • call listview.Focus() after setting the color
  • listviewitem.Focused set to true
  • call listview.Invalidate
  • call listview.Update()
  • call listview.Refresh()
  • different combinations of the above

I've also did combinations above stuff in a timer so that they are called on a different thread but still no success.

Any ideas?

More info:

  • The key here is the control focus. The listview control does not have the focus when I select one of the items.
  • I select one item by doing:

    listView1.Items[index].Selected = true;
  • the Focus is always in the textbox.

  • the computer does not have keyboard or mouse, only a barcode reader.

I have this code to keep the focus on the textbox:

private void txtBarcode_Leave(object sender, EventArgs e)

You need to have a textbox add that code to simulate my problem.

What you describe works exactly as expected, assuming that you've set the HideSelection property of the ListView control to False. Here's a screenshot for demonstration purposes. I created a blank project, added a ListView control and a TextBox control to a form, added some sample items to the ListView, set its view to "Details" (although this works in any view), and set HideSelection to false. I handled the TextBox.Leave event just as you showed in the question, and added some simple logic to select the corresponding ListViewItem whenever its name was entered into the TextBox. Notice that "Test Item Six" is selected in the ListView:


Now, as I suspected initially, you're going to mess things up if you go monkeying around with setting the BackColor property yourself. I'm not sure why you would ever want to do this, as the control already uses the default selection colors to indicate selected items by default. If you want to use different colors, you should change your Windows theme, rather than trying to write code to do it.

In fact, if I add the line item.BackColor = Color.LightSteelBlue in addition to my existing code to select the ListViewItem corresponding to the name typed into the TextBox, I get exactly the same thing as shown above. The background color of the item doesn't change until you set focus to the control. That's the expected behavior, as selected items look different when they have the focus than they do when their parent control is unfocused. Selected items on focused controls are painted with the system highlight color; selected items on unfocused controls are painted with the system 3D color. Otherwise, it would be impossible to tell whether or not the ListView control had the focus. Moreover, any custom BackColor property is completely ignored by the operating system when the ListView control has the focus. The background gets painted in the default system highlight color.

Explicitly setting the focus to the ListView control, of course, causes the custom background color to be applied to the ListViewItem, and things render with a color that very much contrasts with the color scheme that I've selected on my computer (remember, not everyone uses the defaults). The problem, though, becomes immediately obvious: you can't set the focus to the ListView control because of the code you've written in the TextBox.Leave event handler method!

I can tell you right now that setting the focus in a focus-changing event is the wrong thing to do. It's a hard rule in Windows you're not allowed to do things like that, and the documentation even warns you explicitly not to do it. Presumably, your answer will be something along the lines of "I have to", but that's no excuse. If everything were working as expected, you wouldn't be asking this question in the first place.

So, what now? Your application's design is broken. I suggest fixing it. Don't try and monkey with setting the BackColor property yourself to indicate that an item is selected. It conflicts with the default way that Windows highlights selected items. Also, don't try and set the focus in a focus-changing event. Windows explicitly forbids this, and the documentation is clear that you're not supposed to do this. If the target computer doesn't have a mouse or keyboard, it's unclear how the user is going to set focus to anything else in the first place, unless you write code to do it, which you shouldn't be doing.

But I have surprisingly little faith that you'll want to fix your application. People who ignore warnings in the documentation tend to be the same people who don't listen to well-meaning advice on Q&A sites. So I'll throw you a bone and tell you how to get the effect you desire anyway. The key lies in not setting the ListViewItem's Selected property, which avoids the conflict between your custom BackColor and the system default highlight color. It also frees you from having to explicitly set the focus to the ListView control and back again (which, as we established above, isn't actually happening, given your Leave event handler method). Doing that produces the following result:


And here's the code—it's not very pretty, but this is just a proof of concept, not a sample of best practice:

public partial class Form1 : Form
   public Form1()
      listView1.View = View.Details;
      listView1.HideSelection = false;

   private void textBox1_TextChanged(object sender, EventArgs e)
      foreach (ListViewItem item in listView1.Items)
         if (item.Text == textBox1.Text)
            item.BackColor = Color.LightSteelBlue;

   private void textBox1_Leave(object sender, EventArgs e)

How to Highlight Selected Items in a ListView when NOT in Focus , the appearance of the item by setting a new ControlTemplate when the Item is selected. I think you are defining new Control template for the ListViewItem. Now you are changing the template on the IsSelected trigger right. I found another soluthen in the MSDN forum. BackColor = SystemColors. c# - How to change listview selected row backcolor even when focus on another control? c# - Change BackColor of button when focus in Window Form; c# - How to change the backcolor of a listview subitem using its own value; c# - How to auto scroll down in WinForms ListView control when update new item?

A standard ListView does not let you set the background color of a selected row. The background (and foreground) colors of a selected row are always controlled by the theme of the OS.

You have to owner draw your ListView to get around this OR you can use ObjectListView. ObjectListView is an open source wrapper around .NET WinForms ListView, which makes it much easier to use, as well as easily allowing things that are very difficult in a normal ListView -- like changed the colors of selected rows.

this.objectListView1.UseCustomSelectionColors = true;
this.objectListView1.HighlightBackgroundColor = Color.Lime;
this.objectListView1.UnfocusedHighlightBackgroundColor = Color.Lime;

This shows the ObjectListView when it does not have focus.

Keep ListViewItem highlighted after loosing focus, Exit focus mode This method adds two columns to the ListView, setting the Text // and TextAlign, and BackColor = System. When the user selects an item without pressing CTRL to perform a multiple selection, the control first clears the  For example - when the user selects a row in the listview I want that to be with Gold color. It does get Gold color but the user can see once they've the lost focus on the listview. so if the row is selected the highlight would be blue – Omer Jul 13 '14 at 14:20.

On SelectedIndexChanged:

    private void lBxDostepneOpcje_SelectedIndexChanged(object sender, EventArgs e)

        ListViewItem item = lBxDostepneOpcje.FocusedItem as ListViewItem;
        ListView.SelectedIndexCollection lista = lBxDostepneOpcje.SelectedIndices;
        foreach (Int32 i in lista)
            lBxDostepneOpcje.Items[i].BackColor = Color.White;
        if (item != null)
            item.Selected = false;
            if (item.Index == 0)
                lBxDostepneOpcje.Items[item.Index-1].BackColor = Color.White;
            if (lBxDostepneOpcje.Items[item.Index].Focused == true)
                lBxDostepneOpcje.Items[item.Index].BackColor = Color.LightGreen;
                if (item.Index < lBxDostepneOpcje.Items.Count-1)
                    lBxDostepneOpcje.Items[item.Index + 1].BackColor = Color.White;
            else if (lBxDostepneOpcje.Items[item.Index].Focused == false)
                lBxDostepneOpcje.Items[item.Index].BackColor = Color.Blue;


ListView.SelectedIndexChanged Event (System.Windows.Forms , In other words, when the Listview control no longer has the focus, I'd like the and it might even just come down to custom drawing the whole thing or the ListView from changing the highlight color of the selected item(s)  I want to set the highlight color for a ListViewItem to be the same color as if the ListView is currently in focus even though it isn't. After setting the HideSelection property to False, when I selected an item and then focusing on another view, the color is grayed out.

[RESOLVED] Listview non-focus selected backcolor-VBForums, Specifies whether the user can delete a ListView item from a ListView control by text stays selected (highlighted) even when the control does not have focus. event, causes the item to change color as the mouse moves over it (hot tracking), and Specifies whether items appear in a different color when the mouse moves​  I've tried this so far with no success: If lvw.FocusedItem Is Nothing Then If lvw.Items.Count > 0 Then lvw.Focus() lvw.HideSelection = False lvw.Items(0).Selected = True lvw.Items(0).Focused = True lvw.FocusedItem = lvw.Items(0) lvw.Select() End If End If. btw the form where the listview is has not called the ShowDialog method yet.

You cant set focus on listview control in this situation. txtBarcode_Leave method will prevent this. But if you are desire to be able select listview items by clicking on them, just add code below to MouseClick event handler of listview:

    private void listView1_MouseClick(object sender, MouseEventArgs e)
        ListView list = sender as ListView;

        for (int i = 0; i < list.Items.Count; i++)
            if (list.Items[i].Bounds.Contains(e.Location) == true)
                list.Items[i].BackColor = Color.Blue; // highlighted item
                list.Items[i].BackColor = SystemColors.Window; // normal item

ListView control, When I select an row (i.e. a ListViewItem) the selected row turns blue, as I'd By default, when you move your focus to another control, the listview will it will sometimes only change the color for the subitems other than 0, but  Explicitly setting the focus to the ListView control, of course, causes the custom background color to be applied to the ListViewItem, and things render with a color that very much contrasts with the color scheme that I've selected on my computer (remember, not everyone uses the defaults).

Selected ListViewItem is the wrong color (grey instead of blue , To display a more custom view for each item in your dataset, implement a ListAdapter. private class MyAdapter extends BaseAdapter { // override other abstract This approach gives you more direct control of the list view and adapter. android:divider, Drawable or color to draw between list items. RE: Selected ListViewItem is the wrong color (grey instead of blue) RiverGuy (Programmer) 24 Oct 05 13:25 By default, when you move your focus to another control, the listview will highlight in grey for the selected item when HideSelection = False.

ListView, But when you check another RadioButton, the databound value still remains true. When you remove an item from the list, the indexes change for subsequent items in the list. Go to Home and select the arrow next to Text Highlight Color. Also, if the control gets the focus, only the WPF C# Tutorial - Add and Remove​  Hi bpell, You can set the listview's ownerdraw property to true. Then add the code as below: Private Sub listView1_DrawItem(ByVal sender As Object, _ ByVal e As DrawListViewItemEventArgs) _ Handles ListView1.DrawItem If Not (e.State And ListViewItemStates.Selected) = 0 Then ' Draw the background for a selected item.

Remove highlight from button wpf, An item within a Cell may be represented by text or some other control such as a getCurrencyInstance().format(item)); // change the text fill based on whether it is If you wanted to set the color of selected ListView cells to be blue, you could Returns the initial focus traversable state of this control, for use by the JavaFX  Setting this to false keep the selected item highlighted even if the listview loses focus. ListView1.HideSelection = false; Gets or sets a value indicating whether the selected item in the control remains highlighted when the control loses focus. When this property is set to false, selected items in the ListView control remain highlighted in a different color than the current selection color specified by the operating system when the ListView control loses focus.

  • Cant understand the problem. Code listView1.Items[0].BackColor = Color.LightSteelBlue; works well. What are you trying to achive but cant?
  • My guess is that the default Windows selection highlight is overriding your custom background color. It's really impossible to say; your question is very unclear as to what the actual problem is.
  • I've hilighted the main key in the problem. Thanks guys for looking at my question.
  • Wow - you're a bit scathing in parts here! But overall it's a good answer ;)
  • @Vidar: Hmm, upon reflection, you're right. I'm not sure why I was so frustrated here. Perhaps there was an extended exchange in the comment thread that has now been deleted. Or I was just in a bad mood. I'm not always such a grump! But a frustrating amount of applications do get this wrong...
  • Thank you! Been fighting with this for a couple hours this morning.
  • Thinking outside the box.
  • great idea, but take note that it doesn't work for the "List" View mode; you have to switch over to "Details" View mode
  • A control or form shouldn't have to listen to their own events. Just override OnDrawItem, etc.
  • Hi Anton, unfortunately there is no mouse or keyboard.