如何更改列表框项目的背景和字体颜色?

时间:2019-01-17 11:20:15

标签: c# winforms listbox

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

  • 如果文本包含- Success ,则背景颜色为 Green ,ForeGround颜色为 Black < / strong>;
  • 如果文本包含- Error ”彩色背景 Red ,则前景为 Black ;
  • 其他情况:背景颜色为 White ,前景色为 Black

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

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

  • 背景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();
}

enter image description here

2 个答案:

答案 0 :(得分:1)

问题按您的if/else顺序。

像这样设置:

if (selected)
{
    backgroundBrush = reportsBackgroundBrushSelected;
}
else
{
    if (text.Contains("Success"))
    {
        backgroundBrush = reportsBackgroundBrush1;
    }
    else if(text.Contains("Error"))
    {
        backgroundBrush = reportsBackgroundBrush2;
    }
    else
    {
        backgroundBrush = reportsBackgroundBrush3;            
    }
}

答案 1 :(得分:1)

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

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

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

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

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

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

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

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 类:

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