根据类中的值突出显示列表框项 [英] Highlight ListBox items based on value from class

查看:95
本文介绍了根据类中的值突出显示列表框项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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



到目前为止并且不确定如何进行:

  private void HighlightUnavailable()
{
foreach(string item在listbox_consoles.Items中)
{
foreach(GameService.AllGames()中的Products.Game游戏)
{
if(item == game.GameName.ToString())
{
if(game.GameAvailable)
{

}
}
}
}
}


解决方案

是的,可能的方式如下:




  • 将ListBox绑定到 GameService.AllGames()返回我相信 Game 对象的列表或数组。


  • 设置且没有来自您资源的图像:

     位图YesImage,NoImage; 

    公共Form1()
    {
    InitializeComponent();
    // ...

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

    listBox1.DrawMode = DrawMode.OwnerDrawFixed;
    listBox1.DrawItem + =(s,e)=> OnListBoxDrawItem(s,e);
    listBox1.DataSource = GameService.AllGames();
    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 =新矩形(
    rectImage.Right + 3,e.Bounds.Y,
    e.Bounds.Right-rectImage.Right-3,
    e.Bounds。高度
    );

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


    Is it possible to loop through items in a ListBox and highlight or indicate item unavailability somehow by checking a class for a value?

    Basically, got a Game class and within stored info whether Game is Available so I need to check this class when looping through the ListBox Items and somehow indicate on the ListBox if GameAvailable = false.

    Got to this point and not sure how to carry on:

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

    解决方案

    Yes that's possible in such a way as:

    • Bind the ListBox to the GameService.AllGames() which returns I believe a list or an array of the Game objects.

    • Set the ListBox.DrawMode to DrawMode.OwnerDrawFixed and handle the ListBox.DrawItem event to draw the items according to their GameAvailable properties.

    Assuming the controls names are Form1 and listBox1, add in Form1 constructor:

    public Form1()
    {
        InitializeComponent();
        //...
    
        listBox1.DrawMode = DrawMode.OwnerDrawFixed;
        listBox1.DrawItem += (s, e) => OnListBoxDrawItem(s, e); 
        listBox1.DataSource = GameService.AllGames();
    }
    

    Say you want to display the available games with green and the rest with red foreground colors.

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

    ... or with different background colors:

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

    ... or with a couple of yes and no images from your resources:

    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);
        listBox1.DataSource = GameService.AllGames();
        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);
    }
    

    这篇关于根据类中的值突出显示列表框项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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