Reputation:
I have made a combobox
with DropDownStyle
property to DropDownList
and I am trying to disable the first option of the dropdown (Read only) as this should be something like "choose an option".
How can I do it? The equivalent code in HTML should be something like this:
<option selected disabled>Select an option</option>
This is just a demo in html of what i actually want to achieve in c#.
By the way I am using visual C# Windows Forms App (.NET Framework)
Upvotes: 0
Views: 1293
Reputation: 39956
What about this:
private void comboBox1_DrawItem(object sender, DrawItemEventArgs e)
{
var yourFont = new Font("Microsoft Sans Serif", 9, FontStyle.Regular);
if (e.Index == 0)
{
e.Graphics.DrawString(comboBox1.Items[e.Index].ToString(), yourFont, Brushes.LightGray, e.Bounds);
}
else
{
e.DrawBackground();
e.Graphics.DrawString(comboBox1.Items[e.Index].ToString(), yourFont, Brushes.Black, e.Bounds);
e.DrawFocusRectangle();
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox1.SelectedIndex == 0)
comboBox1.SelectedIndex = -1;
}
You need to set the DrawMode
property of the comboBox
to OwnerDrawFixed
also.
Upvotes: 3