C# : changing listbox row color?

Posted by Meko on Stack Overflow See other posts from Stack Overflow or by Meko
Published on 2010-03-31T16:18:36Z Indexed on 2010/04/04 21:33 UTC
Read the original article Hit count: 370

Filed under:
|

HI. I am trying to changing backround colorof some rows on listbox.I have 2 list that one has all names and it shown on listbox.And second list has some same value with first list.I want to show that when clicking button it will search in listbox and in second list then will change color where found value. I may search in list box like

  for (int i = 0; i < listBox1.Items.Count; i++)
        {
            for (int j = 0; j < students.Count; j++)

                if (listBox1.Items[i].ToString().Contains(students[j].ToString()))
                {




                }
        }

But I don't know which method to change appearances of row.Any help?

*EDIT: *

HI I made my code like

       private void ListBox1_DrawItem(object sender, DrawItemEventArgs e)
    {
        e.DrawBackground();
        Graphics g = e.Graphics;
        Brush myBrush = Brushes.Black;
        Brush myBrush2 = Brushes.Red;
        g.FillRectangle(new SolidBrush(Color.Silver), e.Bounds);
        e.Graphics.DrawString(listBox1.Items[e.Index].ToString(),
            e.Font, myBrush, e.Bounds, StringFormat.GenericDefault);
        for (int i = 0; i < listBox1.Items.Count; i++)
        {
            for (int j = 0; j < existingStudents.Count; j++)
                if (listBox1.Items[i].ToString().Contains(existingStudents[j]))
                {

                    e.Graphics.DrawString(listBox1.Items[i].ToString(),
              e.Font, myBrush2, e.Bounds, StringFormat.GenericDefault);
                }
        }
        e.DrawFocusRectangle();
    }

Now it draws my list on listbox.But when I click button first it shows only student that in list with red color but when I click on listbox it again draws all elements.I want that it will show all element ,when I click button it will show all elements and founded element in list with red color.Whre is my mistake?

© Stack Overflow or respective owner

Related posts about c#

Related posts about listbox