【问题标题】:How do I change background and font colors of the Items of a ListBox?如何更改列表框项目的背景和字体颜色?
【发布时间】:2019-01-17 11:20:15
【问题描述】:

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

  • 如果文本中包含-Success,则背景色为Green,前景色为Black
  • 如果文本中包含-Error"彩色背景Red,前景为Black
  • 其他情况:背景色为White,前景色为Black

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

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

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

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

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

【问题讨论】:

  • 控件错误,使用 ListView 和 View=List。

标签: c# winforms graphics listbox gdi+


【解决方案1】:

尽管没有明确要求,但我建议使用另一种管理画笔的方法,我认为这可能会简化 ForeColor/BackColor 开关并允许更好地自定义 ListBox 演示。

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

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

这个类很简单。
它有一个重载的构造函数,允许指定默认值或特定的颜色属性以分配给 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)
{
    var 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);
        }
    }
}

【讨论】:

    【解决方案2】:

    问题在于您的if/else

    这样设置:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-09
      • 1970-01-01
      • 1970-01-01
      • 2012-10-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多