views:

29

answers:

2

It seems to use default color from Windows settings which is blue by default. Let's say I want to change it to red permanently. I'm using Winforms.

Thanks in advance.

A: 

The below code does exactly what you are saying:

In the InitializeComponent method:

this.listBox1.DrawMode = DrawMode.OwnerDrawFixed;
this.listBox1.DrawItem += new System.Windows.Forms.DrawItemEventHandler(listBox1_DrawItem);
this.listBox1.SelectedIndexChanged += new System.EventHandler(listBox1_SelectedIndexChanged);

And the event handlers:

void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
    this.listBox1.Invalidate();
}

void listBox1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
{
    int index = e.Index;
    Graphics g = e.Graphics;
    foreach (int selectedIndex in this.listBox1.SelectedIndices)
    {
        if (index == selectedIndex)
        {
            // Draw the new background colour
            e.DrawBackground();
            g.FillRectangle(new SolidBrush(Color.Red), e.Bounds);
        }
    }

    // Get the item details
    Font font = listBox1.Font;
    Color colour = listBox1.ForeColor;
    string text = listBox1.Items[index].ToString();

    // Print the text
    g.DrawString(text, font, new SolidBrush(Color.Black), (float)e.Bounds.X, (float)e.Bounds.Y);
    e.DrawFocusRectangle();
}

Code is taken from:

http://www.weask.us/entry/change-listbox-rsquo-selected-item-backcolor-net

Musa Hafalır
I'm getting a problem with this. The selection changes to red but it also stays on all items that I previously selected, defeating the purpose of having a "visual" one selected item. What could it be?
Queops
+2  A: 

You must override the Drawitem event and set the DrawMode property to DrawMode.OwnerDrawFixed

check this sample

private 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();
}

alt text

RRUZ
I managed to apply this code but the items on the listbox have white color on the background and foreground. I can't see why it would do that?
Queops
do you set the property `DrawMode` to `DrawMode.OwnerDrawFixed`?
RRUZ
Yes, otherwise if at Normal it is the default windows color.
Queops
Do you assign the event handler DrawItem like this `this.listBox1.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.listBox1_DrawItem);`?
RRUZ
Oops I forgot to override. Sorry for your trouble! It works as intended now. Thanks!
Queops