tags:

views:

826

answers:

5

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?

+1  A: 

I think you have to draw the listitems yourself to achieve this.

Here's a post with the same kind of question.

Rhapsody
+1  A: 

Set the DrawMode property to OwnerDrawFixed, and handle the DrawItem event :

void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{
    if (/* condition */)
    {
        e.BackColor = Color.Red;
    }
}
Thomas Levesque
A: 

You will need to draw the item yourself. Change the DrawMode to OwnerDrawFixed and handle the DrawItem event.

/// <summary>
/// Handles the DrawItem event of the listBox1 control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.Windows.Forms.DrawItemEventArgs"/> instance containing the event data.</param>
private void listBox1_DrawItem( object sender, DrawItemEventArgs e )
{
   e.DrawBackground();
   Graphics g = e.Graphics;

    // draw the background color you want
    // mine is set to olive, change it to whatever you want
    g.FillRectangle( new SolidBrush( Color.Olive), e.Bounds );

    // draw the text of the list item, not doing this will only show
    // the background color
    // you will need to get the text of item to display
    g.DrawString( THE_LIST_ITEM_TEXT , e.Font, new SolidBrush( e.ForeColor ), new PointF( e.Bounds.X, e.Bounds.Y) );

    e.DrawFocusRectangle();
}
Justin
thanks for comment but how will I use this function?or where? I changed in properties of listbox to Drawmode = OwnerDrawFixed and in InitializeComponent() am I have to correct some think? or simply I will add this function to my Form1.cs function and it will call it?
Meko
I'm a VS designer fan. So in the form editor where your listbox is go into the event list and create a new handler for the DrawItem event. Then use the above code in your new event handler.
Justin
A: 

Hi again. I find solution that instead of using ListBox I used ListView.It allows to change list items BackColor.

  private void listView1_Refresh()
    {

        for (int i = 0; i < listView1.Items.Count; i++)
        {
            listView1.Items[i].BackColor = Color.Red;
            for (int j = 0; j < existingStudents.Count; j++)
                if (listView1.Items[i].ToString().Contains(existingStudents[j]))
                {

                    listView1.Items[i].BackColor = Color.Green;
                }

        }
Meko
A: 

First use this Namespace:

using System.Drawing;

Add this anywhere on your form:

listBox.DrawMode = DrawMode.OwnerDrawFixed;
listBox.DrawItem += listBox_DrawItem;

Here is the Event Handler:

private void listBox_DrawItem(object sender, DrawItemEventArgs e)
{
     e.DrawBackground();

     Graphics g = e.Graphics;
     g.FillRectangle(new SolidBrush(Color.White), e.Bounds);
     ListBox lb = (ListBox)sender;
     g.DrawString(lb.Items[e.Index].ToString(), e.Font, new SolidBrush(Color.Black), new PointF(e.Bounds.X, e.Bounds.Y));

     e.DrawFocusRectangle();
}
Anadrol