Is it possible to change appearance for selected (not in drop down!) item?
combobox.ForeColor is changing the text color only for all items into drop-down list.
Edit:
Variants are beelow, ours is
public static void CBoxDrawItem(object sender, DrawItemEventArgs args)
{
var box = sender as ComboBox;
if (box == null || args.Index < 0 || args.Index >= box.Items.Count)
return;
e.DrawBackground();
var data = box.Tag as ControlData;
var color = (args.State & DrawItemState.ComboBoxEdit) == 0 || data == null || !data.IsInDefaultState
? e.ForeColor : GetDefaultColor(e.ForeColor);
using (var brush = new SolidBrush(color))
{
args.Graphics.DrawString(box.Items[args.Index].ToString(), args.Font, brush, args.Bounds.X, args.Bounds.Y);
}
args.DrawFocusRectangle();
}
You don’t have to change the
FlatStyleto Popup or Flat to make this work. And you probably don’t want to do that in the first place, because those styles tend to look really ugly when compared to the rest of your application’s interface. Native Windows controls use a 3D-style appearance; the Flat and Popup styles are designed for Web or Windows Mobile applications, where they are a better fit.I assume that you’re asking this question because you have already written code to change the foreground color of the text displayed in the combobox, but have noticed that it isn’t working under Windows Vista or later. That’s because when the
DropDownListstyle of combobox changed to look more like a button in those versions of Windows, it also lost support for custom text color. Instead, the selected text is always displayed in the standard “Window Text” color. Compare theDropDownListstyle to the regularDropDownstyle combobox:Visually, the two comboboxes look the same in earlier versions of Windows, but not under Vista and later. The key to getting your custom foreground color to appear is changing the
DropDownStyleproperty of your combobox control toDropDown(which is actually the default).I also like to set the
FlatStyleproperty toSystemso that you get all the nifty fade-in and fade-out effects offered by the native Windows controls. TheStandardstyle attempts to emulate those effects in managed code, but it just doesn’t have quite the right feel. I care about the little things.Then you can use the following code (as originally suggested in Adrian’s answer):
To produce the following effect: