【问题标题】:How to change the ForeColor of individual items in a ComboBox? (C# Winforms)如何更改组合框中单个项目的前景色? (C# Winforms)
【发布时间】:2011-09-07 23:02:22
【问题描述】:

我知道我可以像这样更改 ComboBox 的前景色:

comboBox1.ForeColor = Color.Red;

但这会让所有的物品都有颜色。当您下拉 ComboBox 时,每个项目都是红色的。

我想单独为项目着色,以便第一个项目是 always 黑色,第二个 always 红色,第三个 always 蓝色等等等。这可能吗?

另外,我认为我不能为此创建 UserControl,因为我使用的 ComboBox 是用于工具条的。

【问题讨论】:

  • 检查这个问题接受的答案:http://stackoverflow.com/questions/4667532/colour-individual-items-in-a-winforms-combobox
  • ToolStripComboBox 是高难度要求,不支持所有者绘制。您需要使用 ToolStripControlHost 并将所有者绘制的常规 ComboBox 嵌入其中。
  • 呃.. 这听起来比它的价值更麻烦。我想我会尝试另一种方式将当前选定项目的颜色传达给用户。

标签: c# winforms combobox


【解决方案1】:

您可以使用DrawItem event

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

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

编辑: 刚找到similar thread

【讨论】:

    【解决方案2】:

    对于从 ToolStripControlHost 派生的 ToolStripComboBox。

    //Declare a class that inherits from ToolStripControlHost.
    public class ToolStripCustomCombo : ToolStripControlHost
    {
        // Call the base constructor passing in a MonthCalendar instance.
        public ToolStripCustomCombo() : base(new ComboBox()) { }
    
        public ComboBox ComboBox
        {
            get
            {
                return Control as ComboBox;
            }
        }
    }
    

    然后假设您有一个名为 m_tsMain 的工具条。以下是添加新控件的方法。

    ToolStripCustomCombo customCombo = new ToolStripCustomCombo();
    ComboBox c = customCombo.ComboBox;
    c.Items.Add("Hello World!!!");
    c.Items.Add("Goodbye cruel world!!!");
    m_tsMain.Items.Add(customCombo);
    

    您应该能够为 DrawItem 添加一个事件处理程序到 c。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-01
      • 2010-10-20
      • 1970-01-01
      • 1970-01-01
      • 2014-02-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多