2011-01-12 54 views
20

我有一個困境,我有一個表單,其中包含一些組合框,其中包含可能在某些情況下無效/過時的信息/選項/項目。在winforms組合框中顏色個別項目?

我不能簡單地從項目中刪除過時的信息,但我確實希望在選項無效時給用戶一個可視的提示。

我正在考慮着色項目(可能是紅色),以表明&何時無效。我不一定需要阻止用戶選擇無效選項,只需讓他們直觀地意識到他們正在這樣做。

可以這樣做嗎?你可以 - dyamically - 改變組合框項目的顏色(u)r?

謝謝,

+0

該死的,我不會重寫默認的項目繪製行爲。必須採取除了ComboBox以外的其他東西。 – Bitterblue 2013-10-30 09:36:24

回答

37

您可以嘗試ComboBox的DrawItem事件。將日期保留在列表中,並將其與身份證件進行比較並刷新項目。

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

+0

感謝您的編輯。 '//根據項目是否被選擇來確定前景顏色'正在拋棄我一點,但似乎沒有下面的代碼來支持選擇或不......我不認爲我真的需要這個功能,只是想知道這是一個C&P錯誤還是我需要注意的事情? – Mike 2011-01-12 10:26:17

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

的源url頁面: http://www.tyrodeveloper.com/2012/04/color-in-combobox-item.html

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