spunit
spunit

Reputation: 565

First Item in ComboBox in other color

I want my first Item in my ComboBox to have a red font. I also want the ComboBox to look like this, and not like this. How can I achieve this?

enter image description here

Upvotes: 0

Views: 1246

Answers (2)

Furkan Ekinci
Furkan Ekinci

Reputation: 2632

If you want to change combobox's appearance you can set DropDownStyle property as DropDownList (it causes to change combobox's behavior; text field is locked and you can't write in it)

And here is how to change first item's color;

Firstly set combobox's DrawMode property as OwnerDrawFixed (unfortunately, this changes causes combobox's appearance turn back old look) and use DrawItem event;

private void cmb_DrawItem(object sender, DrawItemEventArgs e)
{
    if (e.Index > -1)
    {
        e.DrawBackground();

        Brush brush = Brushes.Black;

        if (e.Index == 0)
        {
            brush = Brushes.Red;
        }

        e.Graphics.DrawString(((ComboBox)sender).Items[e.Index].ToString(), ((Control)sender).Font, brush, e.Bounds.X, e.Bounds.Y);
    }
}

Upvotes: 1

Bhavin
Bhavin

Reputation: 260

// Property named "DropDwonStyle" set it to DropDownList for Like this

Upvotes: 0

Related Questions