简体   繁体   English

为 winforms ComboBox 中的单个项目着色?

[英]Colour Individual Items in a winforms ComboBox?

I have a dilemma whereby I have a form which contains a number of comboboxes that contain information/options/items that may be invalid/out-of-date in certain circumstances.我有一个困境,我有一个表单,其中包含许多组合框,其中包含在某些情况下可能无效/过时的信息/选项/项目。

I can't simply remove the out-of-date information from the items, but I do want to give the user a visual-clue when options are invalid.我不能简单地从项目中删除过时的信息,但我确实希望在选项无效时为用户提供视觉线索。

I was thinking of colouring in the Items (probably red) to indicate if & when they are invalid.我正在考虑在项目中着色(可能是红色)以指示它们是否和何时无效。 I don't necessarily need to prevent a user from selecting an invalid option, just make them visually aware that they are doing so.我不一定需要阻止用户选择无效选项,只需让他们在视觉上意识到他们正在这样做。

Can this be done?这能做到吗? Can you - dyamically - change the colo(u)r of combobox items?你能 - 动态地 - 改变组合框项目的颜色吗?

Thanks,谢谢,

You may try DrawItem event of ComboBox.你可以试试 ComboBox 的 DrawItem 事件。 Keep your dates on a list and compare them with ID's and brush your items.将您的日期列在清单上,并将它们与 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);
}

To fire this event (thanks to @Bolu)触发此事件(感谢@Bolu)

You need to change ComboBox.DrawMode to OwnerDrawFixed/OwnerDrawVariable to fire the comboBox_DrawItem您需要将 ComboBox.DrawMode 更改为 OwnerDrawFixed/OwnerDrawVariable 以触发 comboBox_DrawItem

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

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

Changing color of combobox back color更改组合框背景颜色的颜色

You can use this codes for changing back color of combobox.您可以使用此代码更改组合框的背景颜色。

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

This should work perfectly这应该完美地工作

private void comboBox1_DrawItem(object sender, DrawItemEventArgs e)
    {
        e.DrawBackground();
        Color color;
        //Text of your ComboBox element
        string text = ((ComboBox)sender).Items[e.Index].ToString();
        //Check for something
        if (text == "I am red")
        {
            color = Color.Red;
        }
        else
        {
            color = Color.White;
        }
        e.Graphics.FillRectangle(new SolidBrush(color), e.Bounds);
        e.Graphics.DrawString(text, e.Font, new SolidBrush(((ComboBox)sender).ForeColor), new Point(e.Bounds.X, e.Bounds.Y));
        e.DrawFocusRectangle();
    }

Use this as DrawItem Event and set DrawMode to OwnerDrawFixed or OwnerDrawVariable将此用作 DrawItem 事件并将 DrawMode 设置为 OwnerDrawFixed 或 OwnerDrawVariable

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM