基于类的值的高亮列表框项目

问题描述 投票:0回答:1

是否有可能通过检查类的值来循环遍历ListBox中的项目并以某种方式突出显示或指示项目不可用?

[基本上,有一个Game类,并且在存储的信息中是否有可用的Game,所以我在遍历ListBox项时需要检查此类,并以某种方式在ListBox上指示GameAvailable = false。

到此为止,不确定如何进行:

private void HighlightUnavailable()
    {
        foreach(string item in listbox_consoles.Items)
        {
            foreach (Products.Game game in GameService.AllGames())
            {
                if (item == game.GameName.ToString())
                {
                    if (game.GameAvailable)
                    {

                    }
                }
            }
        }
    }
c# winforms loops class listbox
1个回答
0
投票

是的,可以这样:

  • 将列表框绑定到GameService.AllGames(),它返回Game对象的列表或数组。

  • ListBox.DrawMode设置为DrawMode.OwnerDrawFixed并处理ListBox.DrawItem事件以根据其GameAvailable属性绘制项目。

假设控件名称是Form1listBox1,请添加Form1构造函数:

public Form1()
{
    InitializeComponent();
    //...

    listBox1.DrawMode = DrawMode.OwnerDrawFixed;
    listBox1.DrawItem += (s, e) => OnListBoxDrawItem(s, e); 
}

假设您要以绿色显示可用的游戏,其余以红色前景色显示。

private void OnListBoxDrawItem(object sender, DrawItemEventArgs e)
{
    //Comment if you don't need to show the selected item(s)...
    e.DrawBackground();

    if (e.Index == -1) return;

    var game = listBox1.Items[e.Index] as Game;
    var foreColor = game.GameAvailable ? Color.Green : Color.Red;

    //Pass the listBox1.BackColor instead of the e.BackColor 
    //if you don't need to show the selection...
    TextRenderer.DrawText(e.Graphics, game.GameName, e.Font,
        e.Bounds, foreColor, e.BackColor,
        TextFormatFlags.Left | TextFormatFlags.VerticalCenter);
}

...或具有不同的背景色:

private void OnListBoxDrawItem(object sender, DrawItemEventArgs e)
{
    if (e.Index == -1) return;

    var game = listBox1.Items[e.Index] as Game;          

    var backColor = e.State.HasFlag(DrawItemState.Selected)
        ? e.BackColor
        : game.GameAvailable
        ? Color.LightGreen
        : listBox1.BackColor;

    //Or this if you don't need to show the selection ...
    //var backColor = game.GameAvailable
    //  ? Color.LightGreen
    //  : listBox1.BackColor;

    using (var br = new SolidBrush(backColor))
        e.Graphics.FillRectangle(br, e.Bounds);

    TextRenderer.DrawText(e.Graphics, game.GameName, e.Font,
        e.Bounds, Color.Black, backColor,
        TextFormatFlags.Left | TextFormatFlags.VerticalCenter);
}

...或带有来自您资源的一些是/否图像:

Bitmap YesImage, NoImage;

public Form1()
{
    InitializeComponent();
    //...

    YesImage = Properties.Resources.YesImage;
    NoImage = Properties.Resources.NoImage;

    listBox1.DrawMode = DrawMode.OwnerDrawFixed;
    listBox1.DrawItem += (s, e) => OnListBoxDrawItem(s, e);
    this.FormClosed += (s, e) => { YesImage.Dispose(); NoImage.Dispose(); };
}

private void OnListBoxDrawItem(object sender, DrawItemEventArgs e)
{
    if (e.Index == -1) return;

    var game = listBox1.Items[e.Index] as Game;
    var backColor = e.State.HasFlag(DrawItemState.Selected)
        ? e.BackColor
        : listBox1.BackColor;
    var bmp = game.GameAvailable ? YesImage : NoImage;
    var rectImage = new Rectangle(
        3, e.Bounds.Y + ((e.Bounds.Height - bmp.Height) / 2),
        bmp.Width, bmp.Height
        );
    var rectTxt = new Rectangle(
        rectImage.Right + 3, e.Bounds.Y,
        e.Bounds.Right - rectImage.Right - 3,
        e.Bounds.Height
        );

    using (var br = new SolidBrush(backColor))
        e.Graphics.FillRectangle(br, e.Bounds);

    e.Graphics.DrawImage(bmp, rectImage);

    TextRenderer.DrawText(e.Graphics, game.GameName, e.Font,
            rectTxt, Color.Black, backColor,
            TextFormatFlags.Left | TextFormatFlags.VerticalCenter);
}

SOQ61607771

© www.soinside.com 2019 - 2024. All rights reserved.