在一个WinForms组合框颜色个别项目? [英] Colour Individual Items in a winforms ComboBox?

查看:134
本文介绍了在一个WinForms组合框颜色个别项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个两难选择,由此我有它包含了一些包含信息/选项组合框/项目可能是无效的表格/外的日期在某些情况下。

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.

我想在项目(可能是红色)着色,以指示和放大器;当他们是无效的。我不一定需要prevent用户从选择一个无效的选项,只是让他们直观地意识到,他们正在这样做。

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.

可以这样做?你能 - dyamically - 改变组合框项结肠(U)R

Can this be done? Can you - dyamically - change the colo(u)r of combobox items?

谢谢,

推荐答案

您可以尝试组合框的DrawItem事件。保持你的日期的名单上,它们与ID的比较和刷你的项目。

You may try DrawItem event of ComboBox. Keep your dates on a list and compare them with ID's and brush your items.

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)

To fire this event (thanks to @Bolu)

您需要更改ComboBox.DrawMode
  到OwnerDrawFixed / OwnerDrawVariable到
  火comboBox_DrawItem

You need to change ComboBox.DrawMode to OwnerDrawFixed/OwnerDrawVariable to fire the comboBox_DrawItem

这篇关于在一个WinForms组合框颜色个别项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆