曲线救国的办法,重写OnEnabledChanged方法,Enabled=false时,将DropDownStyle属性修改为DropDownList,Enabled=true时恢复原来的DropDownStyle属性,这样在应用时就可以通过设置BackColor来改变下拉框的背景色了。
protected string style = "DropDown";
protected Color color = Color.Black;
public MyComboBox()
{
InitializeComponent();
this.DrawMode = DrawMode.OwnerDrawVariable;
this.MaxDropDownItems = 20;
if (this.DropDownStyle == ComboBoxStyle.Simple)
style = "Simple";
else
if (this.DropDownStyle == ComboBoxStyle.DropDown)
style = "DropDown";
else
if (this.DropDownStyle == ComboBoxStyle.DropDownList)
style = "DropDownList";
color = this.ForeColor;
}
protected override void OnEnabledChanged(EventArgs e)
{
base.OnEnabledChanged(e);
if (!this.Enabled)
{
this.DropDownStyle = ComboBoxStyle.DropDownList;
this.ForeColor = Color.FromArgb(109, 109, 109);
}
else
{
switch (style)
{
case "simple": this.DropDownStyle = ComboBoxStyle.DropDownList; break;
case "DropDown": this.DropDownStyle = ComboBoxStyle.DropDown; break;
case "DropDownList": this.DropDownStyle = ComboBoxStyle.DropDownList; break;
}
this.ForeColor = color;
}
}