如何更改组合框的背景颜色(不仅限于下拉列表部分) [英] How to change combobox background color (not just the drop down list part)

查看:101
本文介绍了如何更改组合框的背景颜色(不仅限于下拉列表部分)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Windows 7上运行的Winform应用程序中,我希望更改组合框的背景颜色以使其突出显示。
comboxbox具有DropDownList的DropDownStyle。



当我以编程方式将BackColor属性更改为Red时,只有实际下拉列表的背景更改为Red 。当未打开下拉列表时,显示所选值的组合框背景保持灰色。我该怎么办,它也会变成红色?



在Windows XP上运行应用程序时,设置BackColor属性可以正常工作



将combobox的DrawMode属性更改为OwnerDrawFixed,并处理DrawItem事件: p>

  private void comboBox1_DrawItem(object sender,DrawItemEventArgs e)
{
int index = e.Index> = 0? e.Index:0;
var brush = Brushes.Black;
e.DrawBackground();
e.Graphics.DrawString(comboBox1.Items [index] .ToString(),e.​​Font,brush,e.Bounds,StringFormat.GenericDefault);
e.DrawFocusRectangle();
}

背景颜色是正确的,但盒子的样式是平坦的,不是通常的3D样式。


In a winform application running on windows 7 I want the change the background color of a combobox to highlight it. The comboxbox has a DropDownStyle of DropDownList.

When I programmatically change the BackColor property to Red, only the background of the actual drop down list is changed to Red. When the drop down list is not opened, the combobox background displaying the selected value remains grey. What can I do so it becomes red too?

Setting the BackColor property works fine when app is run on Windows XP

解决方案

This should get you started.

Change the combobox DrawMode property to OwnerDrawFixed, and handle the DrawItem event:

private void comboBox1_DrawItem(object sender, DrawItemEventArgs e)
{
    int index = e.Index >= 0 ? e.Index : 0;
    var brush = Brushes.Black;
    e.DrawBackground();
    e.Graphics.DrawString(comboBox1.Items[index].ToString(), e.Font, brush, e.Bounds, StringFormat.GenericDefault);
    e.DrawFocusRectangle();
}

The background color will be right but the style of the box will be flat, not the usual 3D style.

这篇关于如何更改组合框的背景颜色(不仅限于下拉列表部分)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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