winforms ComboBox中的单个项目颜色?

时间:2011-01-12 10:10:22

标签: c# vb.net winforms combobox

我有一个困境,我有一个表格,其中包含许多组合框,其中包含在某些情况下可能无效/过期的信息/选项/项目。

我不能简单地从项目中删除过时的信息,但我确实希望在选项无效时给用户一个直观的线索。

我想在项目中着色(可能是红色)以表明是否&当它们无效时。我不一定需要阻止用户选择无效选项,只是让他们在视觉上意识到他们正在这样做。

可以这样做吗?你能 - 动态地 - 改变组合框的colo(u)r吗?

谢谢,

3 个答案:

答案 0 :(得分:40)

您可以尝试使用ComboBox的DrawItem事件。将您的日期保存在列表中,并将其与ID进行比较并刷新您的项目。

private void comboBox1_DrawItem(object sender, DrawItemEventArgs e)
{    
    // Draw the background 
    e.DrawBackground();        

    // Get the item text    
    string text = ((ComboBox)sender).Items[e.Index].ToString();

    // Determine the forecolor based on whether or not the item is selected    
    Brush brush;
    if (YourListOfDates[e.Index] < DateTime.Now)// compare  date with your list.  
    {
        brush = Brushes.Red;
    }
    else
    {
        brush = Brushes.Green;
    }

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

发起此事件(感谢@Bolu)

  

您需要更改ComboBox.DrawMode   到OwnerDrawFixed / OwnerDrawVariable来   触发comboBox_DrawItem

答案 1 :(得分:4)

///The ComboBoxCustom Control:

using System;
using System.Windows.Forms;
using System.Drawing;
namespace TyroDeveloper
{
    public class ComboBoxCustom:ComboBox
    {
        public ComboBoxCustom() { 
            this.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed; 
        }
        protected override void OnDrawItem(DrawItemEventArgs e)
        {
            base.OnDrawItem(e);
            e.DrawBackground();
            ComboBoxItem item = (ComboBoxItem)this.Items[e.Index];
            Brush brush = new SolidBrush(item.ForeColor);
            if ((e.State & DrawItemState.Selected) == DrawItemState.Selected) 
            { brush = Brushes.Yellow; }
            e.Graphics.DrawString(item.Text, this.Font, brush, e.Bounds.X, e.Bounds.Y);
        }
    }
    public class ComboBoxItem
    {
        public ComboBoxItem() { }

        public ComboBoxItem(string pText, object pValue)
        {
            text = pText; val = pValue;
        }

        public ComboBoxItem(string pText, object pValue, Color pColor)
        {
            text = pText; val = pValue; foreColor = pColor;
        }

        string text = "";
        public string Text { 
            get { return text; } set { text = value; } 
        }

        object val;
        public object Value { 
            get { return val; } set { val = value; } 
        }

        Color foreColor = Color.Black;
        public Color ForeColor { 
            get { return foreColor; } set { foreColor = value; } 
        }

        public override string ToString()
        {
            return text;
        }
    }
}

//To add the items:

comboBoxCustom1.Items.Add(new ComboBoxItem("México","0",Color.Green));
comboBoxCustom1.Items.Add(new ComboBoxItem("USA", "1", Color.Blue));
comboBoxCustom1.Items.Add(new ComboBoxItem("China", "2", Color.Red));

源网址页面: http://www.tyrodeveloper.com/2012/04/color-in-combobox-item.html

答案 2 :(得分:1)

Changing color of combobox back color

您可以使用此代码更改组合框的背面颜色。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Comboboxrenklendir
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }



    private void cmbrenkli_DrawItem(object sender, DrawItemEventArgs e)
    {




        Color HighlightColor = Color.Red;

        if (e.Index >=0)
        {

            //int sayi = cmbrenkli.Items.Count;

            string deger = cmbrenkli.Items[e.Index].ToString();

            if (deger == "30"|| deger == "50")
            {
                e.Graphics.FillRectangle(new SolidBrush(HighlightColor), e.Bounds);
            }



            e.Graphics.DrawString(cmbrenkli.Items[e.Index].ToString(), e.Font, new SolidBrush(cmbrenkli.ForeColor), new Point(e.Bounds.X, e.Bounds.Y));

            e.DrawFocusRectangle();

        }
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        cmbrenkli.Items.Add("10");
        cmbrenkli.Items.Add("20");
        cmbrenkli.Items.Add("30");
        cmbrenkli.Items.Add("40");
        cmbrenkli.Items.Add("50");
    }
}
}