【问题标题】:Background color of a ListBox item (Windows Forms)ListBox 项的背景颜色(Windows 窗体)
【发布时间】:2010-09-10 15:25:18
【问题描述】:

如何设置System.Windows.Forms.ListBox 中特定项目的背景颜色

如果可能的话,我希望能够设置多个。

【问题讨论】:

    标签: c# winforms listbox colors


    【解决方案1】:

    感谢answer by Grad van Horck。它引导我朝着正确的方向前进。

    为了支持文本(不仅仅是背景颜色),这是我的完整代码:

    //global brushes with ordinary/selected colors
    private SolidBrush reportsForegroundBrushSelected = new SolidBrush(Color.White);
    private SolidBrush reportsForegroundBrush = new SolidBrush(Color.Black);
    private SolidBrush reportsBackgroundBrushSelected = new SolidBrush(Color.FromKnownColor(KnownColor.Highlight));
    private SolidBrush reportsBackgroundBrush1 = new SolidBrush(Color.White);
    private SolidBrush reportsBackgroundBrush2 = new SolidBrush(Color.Gray);
    
    //custom method to draw the items, don't forget to set DrawMode of the ListBox to OwnerDrawFixed
    private void lbReports_DrawItem(object sender, DrawItemEventArgs e)
    {
        e.DrawBackground();
        bool selected = ((e.State & DrawItemState.Selected) == DrawItemState.Selected);
    
        int index = e.Index;
        if (index >= 0 && index < lbReports.Items.Count)
        {
            string text = lbReports.Items[index].ToString();
            Graphics g = e.Graphics;
    
            //background:
            SolidBrush backgroundBrush;
            if (selected)
                backgroundBrush = reportsBackgroundBrushSelected;
            else if ((index % 2) == 0)
                backgroundBrush = reportsBackgroundBrush1;
            else
                backgroundBrush = reportsBackgroundBrush2;
            g.FillRectangle(backgroundBrush, e.Bounds);
    
            //text:
            SolidBrush foregroundBrush = (selected) ? reportsForegroundBrushSelected : reportsForegroundBrush;
            g.DrawString(text, e.Font, foregroundBrush, lbReports.GetItemRectangle(index).Location);
        }
    
        e.DrawFocusRectangle();
    }
    

    以上代码添加到给定的代码中,将显示正确的文本并突出显示选定的项目。

    【讨论】:

    • 好极了,选中的位很有用。
    • 什么是reportsForegroundBrushSelected:reportsForegroundBrush ??
    • reportsForegroundBrushSelected :reportsForegroundBrush 给了我错误,他们应该被声明但如何?
    • @PrakashVishwakarma 完成,请参阅我的编辑。感谢您的提醒,我也提高了整体效率,每次调用错误的方法时,我都会创建新的 Brush 实例。
    • @ShadowWizardIsVaccinatedV3 希望为当前/未来的用户注意。我现在将不得不使用 ListView,我只是很好奇,因为我还没有见过这样的列表框解决方案。将来我可能会长出一头狂野的头发并尝试将其运行-如果这样做,我会发布-但现在快速运行的东西比“另一个有趣的项目”要好。尤其是 OwnerDraw,我之前没有经验,也预见到不需要。 ;-)
    【解决方案2】:
    // Set the background to a predefined colour
    MyListBox.BackColor = Color.Red;
    // OR: Set parts of a color.
    MyListBox.BackColor.R = 255;
    MyListBox.BackColor.G = 0;
    MyListBox.BackColor.B = 0;
    

    如果您设置多种背景颜色的意思是为每个项目设置不同的背景颜色,这对于 ListBox 是不可能的,但它 对于 ListView 来说是这样的:

    // Set the background of the first item in the list
    MyListView.Items[0].BackColor = Color.Red;
    

    【讨论】:

    • 使用 ListBox 是可能的。见stackoverflow.com/questions/91747/…
    • s/可能/容易/。那好吧。 C# 1,新手 0。我之前没有在重载绘画方法方面做过很多工作。
    • BackColor 不是ListBox.ObjectCollection item 的属性
    【解决方案3】:

    可能实现这一点的唯一方法是自己绘制项目。

    DrawMode 设置为OwnerDrawFixed 并在DrawItem 事件上编写如下代码:

    private void listBox_DrawItem(object sender, DrawItemEventArgs e)
    {
        e.DrawBackground();
        Graphics g = e.Graphics;
    
        g.FillRectangle(new SolidBrush(Color.Silver), e.Bounds);
    
        // Print text
    
        e.DrawFocusRectangle();
    }
    

    第二种选择是使用 ListView,尽管它们有另一种实现方式(不是真正的数据绑定,但列方式更灵活)。

    【讨论】:

      【解决方案4】:
      public MainForm()
      {
          InitializeComponent();
          this.listbox1.DrawItem += new DrawItemEventHandler(this.listbox1_DrawItem);
      }
      
      private void listbox1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
      {
          e.DrawBackground();
          Brush myBrush = Brushes.Black;
          var item = listbox1.Items[e.Index];
          if(e.Index % 2 == 0)
          {
              e.Graphics.FillRectangle(new SolidBrush(Color.Gold), e.Bounds);
          }
              e.Graphics.DrawString(((ListBox)sender).Items[e.Index].ToString(), 
                  e.Font, myBrush,e.Bounds, StringFormat.GenericDefault);
              e.DrawFocusRectangle();
          }
      }
      

      【讨论】:

      • 解释一下。例如,想法/要点是什么?请通过editing (changing) your answer 回复,而不是在 cmets 中(without "Edit:"、"Update:" 或类似的 - 答案应该看起来像是今天写的)。
      • 同意@PeterMortensen。根本无法遵循这个逻辑 - 似乎非常严重地错过了这个问题。懒惰。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-03
      • 1970-01-01
      • 2018-03-30
      • 1970-01-01
      • 2011-10-26
      • 1970-01-01
      相关资源
      最近更新 更多