如何更改ForeColor ComboBox的所选项目? [英] How to change ForeColor of ComboBox's Selected Item?
问题描述
是否可以更改所选(不是下拉!)项目的外观?
Is it possible to change appearance for selected (not in drop down!) item?
combobox.ForeColor只会将所有项目的文字颜色改成下拉式清单。
combobox.ForeColor is changing the text color only for all items into drop-down list.
Edit:
变体是beelow,我们的是
Variants are beelow, ours is
public static void CBoxDrawItem(object sender, DrawItemEventArgs args)
{
var box = sender as ComboBox;
if (box == null || args.Index < 0 || args.Index >= box.Items.Count)
return;
e.DrawBackground();
var data = box.Tag as ControlData;
var color = (args.State & DrawItemState.ComboBoxEdit) == 0 || data == null || !data.IsInDefaultState
? e.ForeColor : GetDefaultColor(e.ForeColor);
using (var brush = new SolidBrush(color))
{
args.Graphics.DrawString(box.Items[args.Index].ToString(), args.Font, brush, args.Bounds.X, args.Bounds.Y);
}
args.DrawFocusRectangle();
}
推荐答案
将 FlatStyle
更改为Popup或Flat以使此工作。你可能不想首先这样做,因为与应用程序接口的其他部分相比,这些样式往往看起来很丑陋。本机Windows控件使用3D风格的外观; Flat和Popup样式是为Web或Windows Mobile应用程序设计的,它们是更好的选择。
You don't have to change the FlatStyle
to Popup or Flat to make this work. And you probably don't want to do that in the first place, because those styles tend to look really ugly when compared to the rest of your application's interface. Native Windows controls use a 3D-style appearance; the Flat and Popup styles are designed for Web or Windows Mobile applications, where they are a better fit.
我假设你问这个问题,因为你已经写代码更改显示在组合框中的文本的前景色,但是注意到它不能在Windows Vista或更高版本中工作。这是因为当组合框的 DropDownList
样式更改为在这些版本的Windows中看起来更像一个按钮时,它也失去了对自定义文本颜色的支持。相反,所选文本总是以标准窗口文本颜色显示。将 DropDownList
样式与常规 DropDown
样式组合框比较:
I assume that you're asking this question because you have already written code to change the foreground color of the text displayed in the combobox, but have noticed that it isn't working under Windows Vista or later. That's because when the DropDownList
style of combobox changed to look more like a button in those versions of Windows, it also lost support for custom text color. Instead, the selected text is always displayed in the standard "Window Text" color. Compare the DropDownList
style to the regular DropDown
style combobox:
在视觉上,两个组合框在早期版本的Windows中看起来一样,但在Vista和更高版本中不一样。 获取自定义前景颜色的关键是更改 DropDownStyle
属性到 DropDown
实际上是默认值)。
Visually, the two comboboxes look the same in earlier versions of Windows, but not under Vista and later. The key to getting your custom foreground color to appear is changing the DropDownStyle
property of your combobox control to DropDown
(which is actually the default).
我也想设置 FlatStyle
属性到系统
这样你就可以得到所有由本地Windows控件提供的漂亮的淡入和淡出效果。 标准
样式试图在托管代码中模拟这些效果,但它只是没有相当正确的感觉。
I also like to set the FlatStyle
property to System
so that you get all the nifty fade-in and fade-out effects offered by the native Windows controls. The Standard
style attempts to emulate those effects in managed code, but it just doesn't have quite the right feel. I care about the little things.
然后,您可以使用以下代码(如Adrian的回答中最初建议的那样):
Then you can use the following code (as originally suggested in Adrian's answer):
public Form1()
{
InitializeComponent();
// Set custom combobox styles
comboBox1.DropDownStyle = ComboBoxStyle.DropDown;
comboBox1.FlatStyle = FlatStyle.System;
// Attach relevant event handler methods
comboBox1.DropDown += new EventHandler(comboBox1_DropDown);
comboBox1.DropDownClosed += new EventHandler(comboBox1_DropDownClosed);
}
void comboBox1_DropDown(object sender, EventArgs e)
{
// Optionally, revert the color back to the default
// when the combobox is dropped-down
//
// (Note that we're using the ACTUAL default color here,
// rather than hard-coding black)
comboBox1.ForeColor = SystemColors.WindowText;
}
void comboBox1_DropDownClosed(object sender, EventArgs e)
{
// Change the color of the selected text in the combobox
// to your custom color
comboBox1.ForeColor = Color.Red;
}
要产生以下效果:
这篇关于如何更改ForeColor ComboBox的所选项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!