Jepe d Hepe
Jepe d Hepe

Reputation: 909

Change Listbox's selected item Backcolor in C#.net

i need to change the backcolor of the selected item in listbox. Can you give me some code example? i've tried adding DrawItem event but it didnt work for me.

Upvotes: 1

Views: 2449

Answers (1)

djdd87
djdd87

Reputation: 68476

Set the listbox's DrawMode to OwnerDrawFixed.

Then assign these events:

private void listBox1_DrawItem(object sender, 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();

}

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

Upvotes: 1

Related Questions