如何更改列表框项目的背景和字体颜色? [英] How do I change background and font colors of the Items of a ListBox?

查看:62
本文介绍了如何更改列表框项目的背景和字体颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个格式化条件:

I created a formatting condition:

  • 如果文本包含- 成功 ,则背景颜色为 绿色 ,ForeGround颜色为黑色
  • 如果文本包含- 错误 "彩色背景 红色 ,则前景为 黑色 ;
  • 其他情况:背景颜色为 白色 ,前景色为 黑色

问题.
如何更改我选择满足条件1 的ListBox项的背景/前景颜色?

Question.
How do I change background/foreground colors of the ListBox Item that I choose to fulfill Condition 1?

条件1 .所选项目的条件:

Condition 1. Condition for the selected Item:

  • 背景 Color.Blue
  • 前景 Color.White (或 Color.Black );

问题:
我的代码没有绘制ListBox项,它是 Color.White .

private SolidBrush reportsForegroundBrushSelected = new SolidBrush(Color.Black);
private SolidBrush reportsBackgroundBrushSelected = new SolidBrush(Color.FromKnownColor(KnownColor.Highlight));

private SolidBrush reportsForegroundBrush = new SolidBrush(Color.Black);
private SolidBrush reportsBackgroundBrush1 = new SolidBrush(Color.LimeGreen);
private SolidBrush reportsBackgroundBrush2 = new SolidBrush(Color.Salmon);
private SolidBrush reportsBackgroundBrush3 = new SolidBrush(Color.White);

private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{
    e.DrawBackground();
    bool selected = ((e.State & DrawItemState.Selected) == DrawItemState.Selected);

    int index = e.Index;
    if (index >= 0 && index < listBox1.Items.Count)
    {
        string text = listBox1.Items[index].ToString();
        Graphics g = e.Graphics;

        //background:
        SolidBrush backgroundBrush;
        if (selected)
        {
            backgroundBrush = reportsBackgroundBrushSelected;
        }
        else
        {
            if (text.Contains("Success"))
            {
                backgroundBrush = reportsBackgroundBrush1;
            }
            else
            {
                backgroundBrush = reportsBackgroundBrush2;
            }
        }

        if (!text.Contains("Success") && !text.Contains("Error"))
        {
            backgroundBrush = reportsBackgroundBrush3;
        }

        g.FillRectangle(backgroundBrush, e.Bounds);

        //text:
        SolidBrush foregroundBrush = (selected) ? reportsForegroundBrushSelected : reportsForegroundBrush;
        g.DrawString(text, e.Font, foregroundBrush, listBox1.GetItemRectangle(index).Location);
    }
    e.DrawFocusRectangle();
}

推荐答案

即使没有明确要求,我还是建议另一种管理笔刷的方法,我认为这可以简化ForeColor/BackColor开关并允许更好地自定义笔刷.ListBox演示文稿.

Even though not explicitly requested, I suggest another way to manage the brushes that may, I think, simplify the ForeColor/BackColor switch and allows a better customization of the ListBox presentation.

创建一个包含所有画笔选择/预选的类对象,并在需要时公开公共属性以修改这些值.
此外,此类还提供了一种返回正确笔刷组合的方法,并根据以下几种条件选择了正确的笔刷:

Create a class object that contains all the brushes selection/pre-selections and exposes public propeties to modify these values if/when needed.
Also, this class provides a method to return the correct combination of brushes, selecting the right ones based on a couple of conditions:

  • 当前商品的文本内容
  • 其选择状态( Selected/Focused NotAccelerator/NotFocusedRect )

该类非常简单.
它具有重载的构造函数,该构造函数允许指定默认值或特定的Color属性,以符合ListBox控件的标准外观.

The class is quite simple.
It has an overloaded constructor that allows to specify default values or specific Color attributes to assing to the standard appearance of the ListBox control.

使用默认(空)构造函数时,ForeGround/BackGround颜色的默认组合设置为 Color.Black Color.White :

When using the default (empty) constructor, the default combination of ForeGround/BackGround Colors are set to Color.Black and Color.White:

public ReportsBrushes() : this(Color.White, Color.Black) { }

否则,它接受2个参数来设置特定值:

otherwise, it accepts 2 parameters, to set specific values:

public ReportsBrushes(Color ItemBackColor, Color ItemForeColor)
{
    this.StandardForeground = new SolidBrush(ItemForeColor);
    this.StandardBackground = new SolidBrush(ItemBackColor);
}

这简化了 ListBox.DrawItem 方法:

private ReportsBrushes reportsBrushes = new ReportsBrushes();

private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{
    ListBox ctl = sender as ListBox;
    e.Graphics.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
    e.DrawFocusRectangle();

    var itemColors = reportsBrushes.GetItemBrushes(ctl.Items[e.Index].ToString(), e.State.HasFlag(DrawItemState.Selected));

    using (StringFormat format = new StringFormat())
    {
        format.LineAlignment = StringAlignment.Center;
        e.Graphics.DrawString(ctl.Items[e.Index].ToString(), ctl.Font, itemColors.ForeColor, e.Bounds, format);
    }
}

private void listBox1_MeasureItem(object sender, MeasureItemEventArgs e)
{
    e.ItemHeight = listBox1.Font.Height + 4;
}

ReportsBrushes 类:

The ReportsBrushes class:

internal class ReportsBrushes
{
    public ReportsBrushes() : this(Color.White, Color.Black) { }
    public ReportsBrushes(Color ItemBackColor, Color ItemForeColor)
    {
        this.StandardForeground = new SolidBrush(ItemForeColor);
        this.StandardBackground = new SolidBrush(ItemBackColor);
    }
    public SolidBrush StandardForeground { get; set; }
    public SolidBrush StandardBackground { get; set; }

    public SolidBrush SelectedForeground { get ; set ; } = 
        new SolidBrush(Color.FromKnownColor(KnownColor.HighlightText));
    public SolidBrush SelectedBackground { get; set; } = 
        new SolidBrush(Color.FromKnownColor(KnownColor.Highlight));
    public SolidBrush SuccessBackground { get; set; } = 
        new SolidBrush(Color.LimeGreen);
    public SolidBrush ErrorBackground { get; set; } = 
        new SolidBrush(Color.OrangeRed);

    public (SolidBrush ForeColor, SolidBrush BackColor) GetItemBrushes(string ItemText, bool ItemSelected)
    {
        if (ItemSelected)
            return (this.SelectedForeground, this.SelectedBackground);
        else
        {
            if (ItemText.Contains("Success"))
                return (this.StandardForeground, this.SuccessBackground);
            if (ItemText.Contains("Error"))
                return (this.StandardForeground, this.ErrorBackground);
            return (this.StandardForeground, this.StandardBackground);
        }
    }
}

这篇关于如何更改列表框项目的背景和字体颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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