【问题标题】:c# coloring individual box in combobox that have different textc#在组合框中为具有不同文本的单个框着色
【发布时间】:2016-10-16 10:07:13
【问题描述】:

我想为组合框中的每个 不是文本项)着色,它们有不同的项目,例如:山有它的框红色,大海有它的框蓝色等。我已经尝试过,但我尝试过的颜色真的很混乱

我需要突出显示每个组合框项目以显示其预期颜色,当单击该组合框时它没有显示其预期颜色

代码如下

        private void cmbRegion_DrawItem(object sender, DrawItemEventArgs e)
    {
        e.DrawBackground();

        string text = ((ComboBox)sender).Items[e.Index].ToString();

        Brush brush;
        if (text.Equals("Mountain")) 
        {
            brush = Brushes.Black;
            cmbRegion.BackColor = Color.Red;
        }
        else if (text.Equals("Sea"))
        {
            brush = Brushes.Black;
            cmbRegion.BackColor = Color.Blue;
        }
        else
        {
            brush = Brushes.Black;
            cmbRegion.BackColor = Color.White;
        }

        // Draw the text    
        e.Graphics.DrawString(text, ((Control)sender).Font, brush, e.Bounds.X, e.Bounds.Y);
    }

可以吗..?

【问题讨论】:

  • 如果cmbRegion 是你的ComboBox 那么cmbRuang 是什么?
  • ups,我的错误。现已编辑。
  • 你确实设置了DrawMode??您在调试器中尝试了什么?
  • e.DrawBackground(); 仅绘制具有白色或高亮背景的项目。你需要自己画背景。

标签: c# winforms colors combobox


【解决方案1】:

修改MSDN example 以符合您的要求。这里的重要属性是DrawMode

private void Form1_Load(object sender, EventArgs e)
{
    InitializeComboBox();
}

internal ComboBox ComboBox1;
private string[] items;

// This method initializes the owner-drawn combo box.
// The drop-down width is set much wider than the size of the combo box
// to accomodate the large items in the list.  The drop-down style is set to 
// ComboBox.DropDown, which requires the user to click on the arrow to 
// see the list.
private void InitializeComboBox()
{
    this.ComboBox1 = new ComboBox();
    this.ComboBox1.DrawMode =
        System.Windows.Forms.DrawMode.OwnerDrawVariable;
    this.ComboBox1.BackColor = Color.Red;
    this.ComboBox1.Location = new System.Drawing.Point(10, 20);
    this.ComboBox1.Name = "ComboBox1";
    this.ComboBox1.Size = new System.Drawing.Size(100, 120);
    this.ComboBox1.DropDownWidth = 250;
    this.ComboBox1.TabIndex = 0;
    this.ComboBox1.DropDownStyle = ComboBoxStyle.DropDown;
    items = new string[] { "Mountain", "Sea", "Other" };
    ComboBox1.DataSource = items;
    this.Controls.Add(this.ComboBox1);

    // Hook up the MeasureItem and DrawItem events
    this.ComboBox1.DrawItem +=
        new DrawItemEventHandler(ComboBox1_DrawItem);
    this.ComboBox1.MeasureItem +=
        new MeasureItemEventHandler(ComboBox1_MeasureItem);
    this.ComboBox1.SelectedIndexChanged += ComboBox1_SelectedIndexChanged;
}

private void ComboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    Color itemBackgroundColor = new Color();

    switch (ComboBox1.SelectedIndex)
    {
        case 0:
            itemBackgroundColor = Color.Red;
            break;
        case 1:
            itemBackgroundColor = Color.Blue;
            break;
        case 2:
            itemBackgroundColor = Color.Green;
            break;
    }

    ComboBox1.BackColor = itemBackgroundColor;
}

// If you set the Draw property to DrawMode.OwnerDrawVariable, 
// you must handle the MeasureItem event. This event handler 
private void ComboBox1_MeasureItem(object sender, MeasureItemEventArgs e)
{
    // No change to item height.
    e.ItemHeight = e.ItemHeight;
}

// 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.
private void ComboBox1_DrawItem(object sender, DrawItemEventArgs e)
{
    string text = ((ComboBox)sender).Items[e.Index].ToString();
    Color itemBackgroundColor = new Color();

    switch (e.Index)
    {
        case 0:
            itemBackgroundColor = Color.Red;
            break;
        case 1:
            itemBackgroundColor = Color.Blue;
            break;
        case 2:
            itemBackgroundColor = Color.Green;
            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.
    e.Graphics.FillRectangle(new SolidBrush(itemBackgroundColor), e.Bounds);

    // Draw each string in the array, using a different size, color,
    // and font for each item.

    e.Graphics.DrawString(items[e.Index], e.Font, Brushes.Black, e.Bounds);
    // Draw the focus rectangle if the mouse hovers over an item.

    e.DrawFocusRectangle();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-17
    • 2012-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多