Search code examples
c#winformslistviewbackcolor

C# finding text by listview and giving it a backcolor


I want to find a certain string (text) that is entered in the textBox1 field in the listView1. So the current problem is, the code below works however right next to the item which has its backcolor changed, the other item would have the same backcolor when hovered over. And when the row is selected then hovered over, there is change in colors (the dark blue goes a bit light when hovered).The listView is kept inside a tabControl. Any ideas why the item next to it gets the backcolor? Any solutions would be great.

foreach (ColumnHeader sColumnHeader in listView1.Columns)
{
    foreach (ListViewItem items in listView1.Items)
    {
        if (items.SubItems[sColumnHeader.Index].Text == textBox1.Text)
        {
            listView1.Items[items.Index].UseItemStyleForSubItems = false;
            listView1.Items[items.Index].SubItems[sColumnHeader.Index]
                                        .BackColor = Color.LightBlue;
         }
    }
}

Here is a link to the problem screenshot: https://i.sstatic.net/A7H7E.png


Solution

  • The problem was due to the activation on the listView being on "OneClick".

    Resetting it back to Standard fixed this problem.

    Sorry and thanks to everyone who has helped.