如何更改ComboBox中单个项目的ForeColor? (C#Winforms) [英] How to change the ForeColor of individual items in a ComboBox? (C# Winforms)

查看:217
本文介绍了如何更改ComboBox中单个项目的ForeColor? (C#Winforms)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我可以这样改变ComboBox的ForeColor:

I know I can change the ForeColor of the ComboBox like this:

comboBox1.ForeColor = Color.Red;

但是这使得所有的颜色项目。

But that makes all the items that color. When you drop down the ComboBox every single item is then red.

我想对项目进行单独着色,以便第一个项目总是 黑色,第二始终红色,第三始终蓝色等。这是可能的吗?

I want to individually color items so that the first item is always black, the second always red, the third always blue, et cetera. Is this possible?

此外,我不认为我可以创建一个UserControl,因为我使用的ComboBox是Toolstrips。

Also, I don't think I can create a UserControl for this because the ComboBox I am using is the one for Toolstrips.

推荐答案

您可以使用 DrawItem事件


事件由自绘ComboBox使用。您可以使用此事件
执行在ComboBox中绘制项目所需的任务。如果你有
一个可变大小的项目(当DrawMode属性设置为
DrawMode.OwnerDrawVariable),在绘制项目之前,MeasureItem
事件被引发。您可以为MeasureItem
事件创建一个事件处理程序,以指定要在
中为DrawItem事件的事件处理程序绘制的项目的大小。

This event is used by an owner-drawn ComboBox. You can use this event to perform the tasks needed to draw items in the ComboBox. If you have a variable sized item (when the DrawMode property set to DrawMode.OwnerDrawVariable), before drawing an item, the MeasureItem event is raised. You can create an event handler for the MeasureItem event to specify the size for the item that you are going to draw in your event handler for the DrawItem event.

MSDN示例:

// You must handle the DrawItem event for owner-drawn combo boxes.  
// This event handler changes the color, size and font of an 
// item based on its position in the array.
protected void ComboBox1_DrawItem(object sender, 
            System.Windows.Forms.DrawItemEventArgs e)
{

    float size = 0;
    System.Drawing.Font myFont;
    FontFamily family = null;

    System.Drawing.Color animalColor = new System.Drawing.Color();
    switch(e.Index)
    {
        case 0:
            size = 30;
            animalColor = System.Drawing.Color.Gray;
            family = FontFamily.GenericSansSerif;
            break;
        case 1:
            size = 10;
            animalColor = System.Drawing.Color.LawnGreen;
            family = FontFamily.GenericMonospace;
            break;
        case 2:
            size = 15;
            animalColor = System.Drawing.Color.Tan;
            family = FontFamily.GenericSansSerif;
            break;
    }

    // Draw the background of the item.
    e.DrawBackground();

    // Create a square filled with the animals color. Vary the size
    // of the rectangle based on the length of the animals name.
    Rectangle rectangle = new Rectangle(2, e.Bounds.Top+2, 
            e.Bounds.Height, e.Bounds.Height-4);
    e.Graphics.FillRectangle(new SolidBrush(animalColor), rectangle);

    // Draw each string in the array, using a different size, color,
    // and font for each item.
    myFont = new Font(family, size, FontStyle.Bold);
    e.Graphics.DrawString(animals[e.Index], myFont, System.Drawing.Brushes.Black, new RectangleF(e.Bounds.X+rectangle.Width, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height));

    // Draw the focus rectangle if the mouse hovers over an item.
    e.DrawFocusRectangle();
}

编辑:
只是发现了一个相似主题

这篇关于如何更改ComboBox中单个项目的ForeColor? (C#Winforms)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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