如何更改组合框backgound颜色(不只是下拉列表中的一部分) [英] How to change combobox backgound color (not just the drop down list part)

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

问题描述

在一个运行在Windows 7的winform应用程序,我想改变组合框的背景色以突出显示它。 该comboxbox拥有的DropDownList的DropDownStyle。

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?

当应用程序运行在Windows XP中设置背景色属性正常工作

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

推荐答案

这应该让你开始。

更改组合框DrawMode属性为OwnerDrawFixed,并处理DrawItem事件:

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();
}

的背景颜色将是正确的,但盒子的风格将是平的,而不是通常的3D风格。

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

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

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