0

I have a little problem with a combobox.

I need to put the background color of a combobox in red when there is a value in it.

I'm using the following code :

if (!string.IsNullOrEmpty(ComboTransmis.Text))
    ComboTransmis.BackColor = Color.OrangeRed;
else
    ComboTransmis.BackColor = Color.White;

But the result is this :

enter image description here

There is only text which have the backcolor, I need all element have it and I have no idea how to do it.

If anyone have an idea ?

Thank you in advance

4
  • I think if i understand the question correctly you need to write a custom control to do this. P.s. in order to iterate through each combobox on your form you will need to specify all subsequent containers and loop through each of their items. Oct 12, 2017 at 8:55
  • @MasterYoda It's just for this one Oct 12, 2017 at 8:57
  • have a look at this question: stackoverflow.com/questions/6468024/…. You need to modify the combobox yourself to achieve this however you lose the 3d style and gain flat. Oct 12, 2017 at 8:59
  • 1
    @MasterYoda Your last comment help me ! It just need to put the FlatStyle propertie to Flat and the BackColor apply on all element. Thanks for your light ! May the force be with you ! Oct 12, 2017 at 9:04

1 Answer 1

1

You need to modify the combobox yourself to achieve this however you lose the 3d style and gain flat.

Based on this answer:

Change the combobox DrawMode property to OwnerDrawFixed, and handle the DrawItem event:

private void ComboTransmis_DrawItem(object sender, DrawItemEventArgs e)
{
   int index = e.Index >= 0 ? e.Index : 0;
   var brush = Brushes.Black;
   e.DrawBackground();
   e.Graphics.DrawString(ComboTransmis.Items[index].ToString(), e.Font, brush, e.Bounds, StringFormat.GenericDefault);
   e.DrawFocusRectangle();
}
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.