ListBox 项的背景颜色(Windows 窗体) [英] Background color of a ListBox item (Windows Forms)

查看:32
本文介绍了ListBox 项的背景颜色(Windows 窗体)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How can I set the background color of a specific item in a System.Windows.Forms.ListBox?

如果可能,我希望能够设置多个.

I would like to be able to set multiple ones if possible.

推荐答案

可能实现这一目标的唯一方法就是自己绘制项目.

Probably the only way to accomplish that is to draw the items yourself.

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

Set the DrawMode to OwnerDrawFixed and code something like this on the DrawItem event:

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,尽管它们有另一种实现方式(不是真正的数据绑定,但在列的方式上更灵活).

The second option would be using a ListView, although they have an other way of implementations (not really data bound, but more flexible in way of columns).

这篇关于ListBox 项的背景颜色(Windows 窗体)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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