Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change combobox background color (not just the drop down list part)

Tags:

.net

winforms

In a winform application running on windows 7 I want the change the background color of a combobox to highlight it. The comboxbox has a DropDownStyle of DropDownList.

When I programmatically change the BackColor property to Red, only the background of the actual drop down list is changed to Red. When the drop down list is not opened, the combobox background displaying the selected value remains grey. What can I do so it becomes red too?

Setting the BackColor property works fine when app is run on Windows XP

like image 889
JBB Avatar asked Jun 24 '11 12:06

JBB


People also ask

How do I change the color of a ComboBox in WPF?

Right click on the ComboBox element in the design view in Visual Studio again and then select the “Edit Additional Templates” option followed by the “Edit Generated Item Container (ItemContainerStyle)” and “Edit a copy…” options.

What is the use of dropdown style property of the ComboBox control?

The DropDownStyle property specifies whether the list is always displayed or whether the list is displayed in a drop-down. The DropDownStyle property also specifies whether the text portion can be edited. See ComboBoxStyle for the available settings and their effects.


2 Answers

This should get you started.

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

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

The background color will be right but the style of the box will be flat, not the usual 3D style.

like image 187
Igby Largeman Avatar answered Sep 20 '22 17:09

Igby Largeman


Since you lose the 3D effects anyway with Igby Largeman's solution you're better off changing the FlatStyle property to Flat. The background color seems to be obeyed even in Windows 7 that way, and without re-implementing any low-level events.

I would consider this a bug on Microsoft's part...

like image 36
bambams Avatar answered Sep 20 '22 17:09

bambams