Click here to Skip to main content
15,911,132 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
C#
c#  winforms  change selection colour in a listbox in after data binding


What I have tried:

c# winforms change selection colour in a listbox in after data binding
Posted
Updated 21-Apr-16 5:48am
Comments
Richard Deeming 21-Apr-16 11:48am    
Three copies of the same sentence, and none of them tell us what you want to do, what you've tried, or what problem you're having.

Click "Improve question" and update your question with a proper description of the problem. Include the relevant parts of your code, and the full details of any exceptions thrown.

1 solution

C#
private void Form1_Load(object sender, EventArgs e)
       {
           listBox1.DataSource = new string[] { "one", "two", "three" };
           listBox1.DrawItem += listBox1_DrawItem;
           listBox1.DrawMode = DrawMode.OwnerDrawVariable;
       }

       void listBox1_DrawItem(object sender, DrawItemEventArgs e)
       {
           if (e.Index < 0) return;
           //if the item state is selected them change the back color
           if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
               e = new DrawItemEventArgs(e.Graphics,
                                         e.Font,
                                         e.Bounds,
                                         e.Index,
                                         e.State ^ DrawItemState.Selected,
                                         e.ForeColor,
                                         Color.Yellow);//Choose the color

           // Draw the background of the ListBox control for each item.
           e.DrawBackground();
           // Draw the current item text
           e.Graphics.DrawString(listBox1.Items[e.Index].ToString(), e.Font, Brushes.Black, e.Bounds, StringFormat.GenericDefault);
           // If the ListBox has focus, draw a focus rectangle around the selected item.
           e.DrawFocusRectangle();
       }


reference : change ListBox selection background color [^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900