更改的WinForms ComboBox的选择颜色 [英] Change the Selection Color of a WinForms ComboBox

查看:1601
本文介绍了更改的WinForms ComboBox的选择颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所有的,我已经有一个深入的外观,但似乎无法找到我所期待的。我想OT更改ComboBoc控制的选择的颜色(最好无需子类的控制)。我虽然做了以下的工作,但这一事件不会甚至解雇

All, I have had an indepth look but can't seem to find what I am looking for. I want ot change the selection color of a ComboBoc control (ideally without having to sub-class the control). I though doing the following would work, but this event is not even firing

private void comboBoxDb_DrawItem(object sender, DrawItemEventArgs e) 
{
    ComboBox combo = sender as ComboBox;
    e.Graphics.FillRectangle(new SolidBrush(combo.BackColor), e.Bounds);
    string strSelectionColor = @"#99D4FC";
    Color selectionColor = 
        System.Drawing.ColorTranslator.FromHtml(strSelectionColor);
    e.Graphics.DrawString(combo.Items[e.Index].ToString(), 
                          e.Font, 
                          new SolidBrush(selectionColor), 
                          new Point(e.Bounds.X, e.Bounds.Y));
}



但这一事件不会甚至解雇。我在做什么错在这里?

but this event is not even firing. What am I doing wrong here?

感谢您的时间。

编辑。虽然非烧结是通过不通过设置正确@Teppic指出ComboBox的DrawMode属性造成的,这仍然不是做什么我需要。我想设置选择的颜色,上面我所做过没有(我在这里勾勒出名字)

而我想改变蓝色突出离子控制,如下所示

推荐答案

设置ComboBox控件要么OwnerDrawFixed的DrawMode属性(如果每个项目的高度将是相同的)或OwnerDrawVariable (如果每个项目的高度可以不同)。

Set the DrawMode property of the ComboBox control to either OwnerDrawFixed (if the height of each item will be the same) or OwnerDrawVariable (if the height of each item may vary).

然后修改DrawItem事件,以类似下面(替代自己的颜色明显):

Then modify your DrawItem event to something like the following (substitute your own colours in obviously):

private void comboBoxDb_DrawItem(object sender, DrawItemEventArgs e) 
{
    var combo = sender as ComboBox;

    if((e.State & DrawItemState.Selected) == DrawItemState.Selected)
    {
        e.Graphics.FillRectangle(new SolidBrush(Color.BlueViolet), e.Bounds);
    }
    else
    {
        e.Graphics.FillRectangle(new SolidBrush(SystemColors.Window), e.Bounds);
    }

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

这篇关于更改的WinForms ComboBox的选择颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆