Pab*_*buc 42
您可以尝试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);
}
Run Code Online (Sandbox Code Playgroud)
解雇此事件(感谢@Bolu)
您需要将ComboBox.DrawMode更改为OwnerDrawFixed/OwnerDrawVariable以触发comboBox_DrawItem
小智 5
///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));
Run Code Online (Sandbox Code Playgroud)
源网址页面:http: //www.tyrodeveloper.com/2012/04/color-in-combobox-item.html