Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# : changing listbox row color?

Tags:

c#

listbox

I am trying to change the background color of some rows in a ListBox. I have two lists that one has names and is displayed in a ListBox. The second list has some similar values as the first List. When clicking a button, I want to search the ListBox and the second List, and change the color of the ListBox for those values that appear in the List. My search in the ListBox is as follows:

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 use in order to change the appearance of a ListBox row. Can anybody help me?

**EDIT: **

HI I wrote my code as follows:

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 in the ListBox, but when I click the button first, it shows in red only the students that are in the List and when I click on the ListBox it draws all the elements. I want that it will show all the elements , and when I click the button it will show all the elements and the element found in the List in red. Where is my mistake?

like image 780
Ercan Avatar asked Mar 31 '10 16:03

Ercan


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.


1 Answers

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;             }         }     } } 
like image 170
Ercan Avatar answered Sep 21 '22 20:09

Ercan