如何更改组合框的选定项的前景色? [英] How to change ForeColor of ComboBox's Selected Item?

查看:204
本文介绍了如何更改组合框的选定项的前景色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能选择改变外观(而不是在下拉!)项目?

combobox.ForeColor正在改变文字颜色只为所有项目到下拉列表中。

编辑: 变种beelow,我们为

 公共静态无效CBoxDrawItem(对象发件人,DrawItemEventArgs参数)
    {
        VAR箱=发件人为组合框;
        如果(盒== NULL || args.Index℃的|| args.Index> = box.Items.Count)
            返回;

        e.DrawBackground();
        VAR数据= box.Tag作为ControlData;
        VAR颜色=(args.State&安培; DrawItemState.ComboBoxEdit)== 0 ||数据== NULL || !data.IsInDefaultState
            ? e.ForeColor:GetDefaultColor(e.ForeColor);
        使用(VAR刷=新SolidBrush(彩色))
        {
            args.Graphics.DrawString(box.Items [args.Index]的ToString(),args.Font,刷,args.Bounds.X,args.Bounds.Y);
        }
        args.DrawFocusRectangle();
    }
 

解决方案

您不必更改的FlatStyle 来弹出或平,使这项工作。你可能不希望这样做,摆在首位,因为这些样式往往相比,你的应用程序界面的其余部分看起来真难看。本机Windows控件使用3D风格的外观;扁平和弹出式风格设计的Web或Windows Mobile应用程序,他们是更好的选择。

我认为你问这个问题,因为你已经写code改变下拉列表的文本的前景色,但已经注意到,它无法在Windows Vista或更高版本的工作。这是因为当组合框的的DropDownList 的风格改变看起来更像是在这些版本的Windows中的一个按钮,它也失去了自定义文字颜色的支持。相反,选定的文本始终显示在标准的窗口文本的颜色。比较的DropDownList 风格到正规下拉风格组合框:

     

从外观上看,这两个组合框看起来一样在早期版本的Windows,而不是在Vista和更高版本。 的关键,让你自定义的前景色出现正在改变着<一href="http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.dropdownstyle.aspx"><$c$c>DropDownStyle您的组合框控件的属性以下拉 (这实际上是默认值)。

我也喜欢设置<一个href="http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.flatstyle.aspx"><$c$c>FlatStyle物业以系统,让你得到所有的漂亮的淡入和淡出的本机Windows控件提供的效果。该标准风格试图效仿的管理code的作用,但它只是没有很合适的感觉。我关心的小事。

然后你可以用下面的code(阿德里安的回答原来的建议):

 公共Form1中()
{
   的InitializeComponent();

   //设置自定义组合框样式
   comboBox1.DropDownStyle = ComboBoxStyle.DropDown;
   comboBox1.FlatStyle = FlatStyle.System;

   //附上相关的事件处理方法
   comboBox1.DropDown + =新的EventHandler(comboBox1_DropDown);
   comboBox1.DropDownClosed + =新的EventHandler(comboBox1_DropDownClosed);
}

无效comboBox1_DropDown(对象发件人,EventArgs的)
{
   //可选,恢复颜色恢复为默认
   //当组合框掉落下来
   //
   //(请​​注意,我们使用的是实际的默认颜色在这里,
   //而非硬编码黑)
   comboBox1.ForeColor = SystemColors.WindowText;
}

无效comboBox1_DropDownClosed(对象发件人,EventArgs的)
{
   //更改所选文本的颜色在组合框中
   //你的自定义颜色
   comboBox1.ForeColor = Color.Red;
}
 

要产生以下影响:

&NBSP;&NBSP;&NBSP;

Is it possible to change appearance for selected (not in drop down!) item?

combobox.ForeColor is changing the text color only for all items into drop-down list.

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

解决方案

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.

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:

     

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).

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.

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

To produce the following effect:

   

这篇关于如何更改组合框的选定项的前景色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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