【问题标题】:Changing the Color of ComboBox highlighting更改 ComboBox 突出显示的颜色
【发布时间】:2012-10-24 02:42:01
【问题描述】:

我正在尝试在 C# Windows Forms 应用程序的 ComboBox 下拉列表中更改突出显示的颜色。 我已经在整个网络上搜索了答案,到目前为止我发现的最佳选择是在绘制所选项目时绘制所需颜色的矩形。

Class Search
{
    Public Search()
    {
    }

    private void addFilter()
    {
        ComboBox field = new ComboBox();

        field.Items.AddRange(new string[] { "Item1", "item2" });
        field.Text = "Item1";
        field.DropDownStyle = ComboBoxStyle.DropDownList;
        field.FlatStyle = FlatStyle.Flat;
        field.BackColor = Color.FromArgb(235, 235, 235);
        field.DrawMode = DrawMode.OwnerDrawFixed;
        field.DrawItem += field_DrawItem;
    }

    private void field_DrawItem(object sender, DrawItemEventArgs e)
    {
        if (e.Index >= 0)
        {
            ComboBox combo = sender as ComboBox;

            if (e.Index == combo.SelectedIndex)
                e.Graphics.FillRectangle(new SolidBrush(Color.Gray),
                                         e.Bounds
                                        );
            else
                e.Graphics.FillRectangle(new SolidBrush(combo.BackColor),
                                         e.Bounds
                                        );

            e.Graphics.DrawString(combo.Items[e.Index].ToString(), e.Font,
                                  new SolidBrush(combo.ForeColor),
                                  new Point(e.Bounds.X, e.Bounds.Y)
                                 );
        }
    }
}

此代码的问题在于,一旦选择了下拉列表中的另一个项目,我绘制的另一个矩形仍然具有我想要突出显示的颜色。 然后我尝试保存最后绘制的项目并重新绘制它:

Class Search
{
    private DrawItemEventArgs lastDrawn;

    Public Search()
    {
        lastDrawn = null;
    }

    private void addFilter()
    {
        ComboBox field = new ComboBox();

        field.Items.AddRange(new string[] { "Item1", "item2" });
        field.Text = "Item1";
        field.DropDownStyle = ComboBoxStyle.DropDownList;
        field.FlatStyle = FlatStyle.Flat;
        field.BackColor = Color.FromArgb(235, 235, 235);
        field.DrawMode = DrawMode.OwnerDrawFixed;
        field.DrawItem += field_DrawItem;
    }

    private void field_DrawItem(object sender, DrawItemEventArgs e)
    {
        if (e.Index >= 0)
        {
            ComboBox combo = sender as ComboBox;
            if (e.Index == combo.SelectedIndex)
            {
                e.Graphics.FillRectangle(new SolidBrush(Color.Gray), e.Bounds);
                if(lastDrawn != null)
                    lastDrawn.Graphics.FillRectangle(new SolidBrush(combo.BackColor),
                                                 lastDrawn.Bounds
                                                );
                lastDrawn = e;
            }
            else
                e.Graphics.FillRectangle(new SolidBrush(combo.BackColor),
                                         e.Bounds
                                        );

            e.Graphics.DrawString(combo.Items[e.Index].ToString(), e.Font,
                                  new SolidBrush(combo.ForeColor),
                                  new Point(e.Bounds.X, e.Bounds.Y)
                                 );
        }
    }
}

由于lastDrawn.Bounds(不兼容类型),此行返回错误

lastDrawn.Graphics.FillRectangle(new SolidBrush(combo.BackColor),
                                                 lastDrawn.Bounds
                                                );

我觉得改变下拉菜单的突出显示颜色是不可能的。 提前致谢!

【问题讨论】:

    标签: c# winforms combobox highlighting


    【解决方案1】:

    如果您在项目中的多个地方使用ComboBox,则一遍又一遍地为DrawItem 事件重复相同的代码是没有意义的。只需将此类添加到您的项目中,您将拥有一个新的 ComboBox 控件,该控件具有 HightlightColor 属性,这将使在整个项目中使用该控件变得更加容易:

    class AdvancedComboBox : ComboBox
    {
        new public System.Windows.Forms.DrawMode DrawMode { get; set; }
        public Color HighlightColor { get; set; }
    
        public AdvancedComboBox()
        {
            base.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed;
            this.HighlightColor = Color.Gray;
            this.DrawItem += new DrawItemEventHandler(AdvancedComboBox_DrawItem);
        }
    
        void AdvancedComboBox_DrawItem(object sender, DrawItemEventArgs e)
        {
            if (e.Index < 0)
                return;
    
            ComboBox combo = sender as ComboBox;
            if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
                e.Graphics.FillRectangle(new SolidBrush(HighlightColor),
                                         e.Bounds);
            else
                e.Graphics.FillRectangle(new SolidBrush(combo.BackColor),
                                         e.Bounds);
    
            e.Graphics.DrawString(combo.Items[e.Index].ToString(), e.Font,
                                  new SolidBrush(combo.ForeColor),
                                  new Point(e.Bounds.X, e.Bounds.Y));
    
            e.DrawFocusRectangle();
        }
    }
    

    【讨论】:

    • 我们可以将它用于 DataGridViewComboBoxColumn 吗?
    猜你喜欢
    • 2010-12-12
    • 1970-01-01
    • 2013-02-02
    • 2023-03-19
    • 2011-06-19
    • 2016-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多