Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the ability to change disabled combo-box BackColor a bug or a feature?

Tags:

c#

winforms

I have found the following solution: If I put in the designer:

this.comboBox1.BackColor = System.Drawing.Color.White;  //or any other color
this.comboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; //it has to be that style

I'm able to change the color of comboBox1 - it won't always be grey.

It should be DropDownList and also BackColor should be placed in the designer.

Is it a bug or a feature?

like image 858
alex gil Avatar asked Nov 19 '25 08:11

alex gil


2 Answers

Make custom combobox, and then in WndProc set BackColor for disabled control.

public class ComboBoxCustom : ComboBox {
    [DllImport("gdi32.dll")]
    internal static extern IntPtr CreateSolidBrush(int color);

    [DllImport("gdi32.dll")]
    internal static extern int SetBkColor(IntPtr hdc, int color);

    protected override void WndProc(ref Message m){
        base.WndProc(ref m);
        IntPtr brush;
        switch (m.Msg){

            case (int)312:
                SetBkColor(m.WParam, ColorTranslator.ToWin32(this.BackColor));
                brush = CreateSolidBrush(ColorTranslator.ToWin32(this.BackColor));
                m.Result = brush;
                break;
            default:
                break;
        }
    }
}
like image 64
Milos Avatar answered Nov 21 '25 22:11

Milos


The DropDownList is allowed the change of BackColor, and no need to set the colour in the Designer, just set the comboBox property to DropDownList in the properties pane.


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!