问题描述
|
我发现以下解决方案:
如果我请设计师:
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应该放置在设计器中。
它是错误还是功能?
解决方法
制作自定义组合框,然后在
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;
}
}
}
,DropDownList允许更改BackColor,而无需在Designer中设置颜色,只需在属性窗格中将comboBox属性设置为DropDownList。
,我也遇到了这个问题,但是要感谢DropDownStyle
属性,它获得了ComboBoxStyle
枚举来设置样式。
yourCombo.DropDownStyle = ComboBoxStyle.DropDownList;
,禁用的控件的默认值为BackColor = Color.Grey
。打算进行更改。
编辑:
我相信这只是“那么简单”。是的,当您开始自定义颜色时,必须提供代码以在所有状态下设置控件的属性。这样想:.Net假设您要自定义属性,则您有责任始终设置该属性。
源自“ 9”类,“ 1”暴露了“ 11”事件。这是需要实现逻辑以为启用和禁用状态设置自己的默认值的地方。例如:
private void radioButton1_EnabledChanged(object sender,EventArgs e)
{
if (((ComboBox)sender).Enabled)
{
// set BackColor for enabled state
}
else
{
// set BackColor for disabled state
}
}