<
>
  • winform中Combobox属性为Enabled=false时背景色如何改变
  • 2015/4/27 18:06:29  浏览次数:2457
  • 曲线救国的办法,重写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;
        }
    }

分享:0
合作伙伴:车三百中国联通 太平洋电脑网