是否能够将禁用的组合框BackColor更改为错误或功能?

时间:2011-05-24 15:59:24

标签: c# winforms

我找到了以下解决方案: 如果我加入设计师:

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

我可以更改comboBox1的颜色 - 它并不总是灰色。

它应该是DropDownList,并且BackColor也应该放在设计器中。

是错误还是功能?

4 个答案:

答案 0 :(得分:3)

制作自定义组合框,然后在WndProc设置BackColor以禁用控件。

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;
        }
    }
}

答案 1 :(得分:2)

DropDownList允许更改BackColor,无需在Designer中设置颜色,只需在属性窗格中将comboBox属性设置为DropDownList。

答案 2 :(得分:2)

我也遇到了这个问题,但感谢DropDownStyle属性,它收到了ComboBoxStyle枚举来设置样式。

yourCombo.DropDownStyle = ComboBoxStyle.DropDownList;

答案 3 :(得分:1)

已禁用的控件具有默认 BackColor = Color.Grey。它有意改变。

修改:

我相信它只是'那么简单'。是的,当您开始自定义颜色时,必须提供代码以在所有状态下设置控件的属性。可以这样想:.Net假设如果要定制属性,则负责始终设置属性。

源自Control classcomboBox1公开Control.EnabledChanged事件。这是您需要实现逻辑的地方,以便为启用和禁用状态设置您自己的默认值;例如:

private void radioButton1_EnabledChanged(object sender, EventArgs e)
{
    if (((ComboBox)sender).Enabled)
    {
        // set BackColor for enabled state
    }
    else
    {
        // set BackColor for disabled state
    }
}