ForeColor in Combobox control
-
Is there a way to set the ForeColor for only the displayed text in a ComboBox control? In other words, can I set the text of the selected item to, say, Color.Red, while keeping the other items in the list as Color.Black (or any other color I desire)? Thanks.
-
Is there a way to set the ForeColor for only the displayed text in a ComboBox control? In other words, can I set the text of the selected item to, say, Color.Red, while keeping the other items in the list as Color.Black (or any other color I desire)? Thanks.
Hello, Basically you could inherit your own ComboBox from System.Windows.Forms.ComboBox:
public class SpecialComboBox : System.Windows.Forms.ComboBox {
In the classes constructor you have to change the "DrawMode" property:public SpecialComboBox() { this.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed; }
Then you have to override "OnDrawItem":protected override void OnDrawItem(System.Windows.Forms.DrawItemEventArgs e) { using(System.Drawing.SolidBrush sb = new System.Drawing.SolidBrush(System.Drawing.Color.Red)) { e.Graphics.DrawString(this.GetItemText(this.Items[e.Index]), e.Font, sb, e.Bounds); } base.OnDrawItem (e); }
Note: I only tested it with "DropDownStyle" set to "System.Windows.Forms.ComboBoxStyle.DropDown", and I think behaviour changes then. I think this article[^] covers it up better. Hope it helps!All the best, Martin