Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 根据类中的值高亮显示列表框项_C#_Winforms_Loops_Class_Listbox - Fatal编程技术网

C# 根据类中的值高亮显示列表框项

C# 根据类中的值高亮显示列表框项,c#,winforms,loops,class,listbox,C#,Winforms,Loops,Class,Listbox,是否可以通过检查类中的值来循环列表框中的项并以某种方式突出显示或指示项不可用 基本上,我得到了一个游戏类,在存储的信息中,游戏是否可用,所以我需要在列表框项目中循环时检查这个类,并以某种方式在列表框上指示GameAvailable=false 到了这一步,不确定如何继续: private void HighlightUnavailable() { foreach(string item in listbox_consoles.Items) {

是否可以通过检查类中的值来循环列表框中的项并以某种方式突出显示或指示项不可用

基本上,我得到了一个游戏类,在存储的信息中,游戏是否可用,所以我需要在列表框项目中循环时检查这个类,并以某种方式在列表框上指示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)
                    {

                    }
                }
            }
        }
    }

是的,这是可能的,因为:

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

  • 将设置为
    DrawMode.OwnerDrawFixed
    并处理事件以根据项目的
    GameAvailable
    属性绘制项目

假设控件名称为
Form1
listBox1
,则添加
Form1
构造函数:

public Form1()
{
初始化组件();
//...
listBox1.DrawMode=DrawMode.OwnerDrawFixed;
listBox1.DrawItem+=(s,e)=>OnListBoxDrawItem(s,e);
listBox1.DataSource=GameService.AllGames();
}
假设您希望以绿色显示可用的游戏,其余的以红色前景色显示

private void OnListBoxDrawItem(对象发送方,DrawItemEventArgs e)
{
//如果不需要显示所选项目,请进行注释。。。
e、 牵引杆接地();
如果(e.Index==-1)返回;
var game=listBox1.项目[e.索引]作为游戏;
var foreColor=game.GameAvailable?颜色。绿色:颜色。红色;
//传递listBox1.BackColor而不是e.BackColor
//如果您不需要显示所选内容。。。
TextRenderer.DrawText(如Graphics、game.GameName、e.Font、,
e、 边界,前景色,例如背景色,
TextFormatFlags.Left | TextFormatFlags.VerticalCenter);
}
。。。或使用不同的背景颜色:

private void OnListBoxDrawItem(对象发送方,DrawItemEventArgs e)
{
如果(e.Index==-1)返回;
var game=listBox1.项目[e.索引]作为游戏;
var backColor=e.State.HasFlag(DrawItemState.Selected)
?e.背景色
:game.GameAvailable
?颜色:浅绿色
:listBox1.BackColor;
//或者,如果您不需要显示选择。。。
//var backColor=game.GameAvailable
//?颜色:浅绿色
//:listBox1.BackColor;
使用(var br=新的SolidBrush(背景色))
e、 图形填充矩形(br,e.Bounds);
TextRenderer.DrawText(如Graphics、game.GameName、e.Font、,
e、 边界,颜色。黑色,背景色,
TextFormatFlags.Left | TextFormatFlags.VerticalCenter);
}
。。。或者从您的资源中选择几张“是”和“否”图片:

Bitmap-YesImage,NoImage;
公共表格1()
{
初始化组件();
//...
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();};
}
私有void OnListBoxDrawItem(对象发送方,DrawItemEventArgs e)
{
如果(e.Index==-1)返回;
var game=listBox1.项目[e.索引]作为游戏;
var backColor=e.State.HasFlag(DrawItemState.Selected)
?e.背景色
:listBox1.BackColor;
var bmp=game.GameAvailable?YesImage:NoImage;
var rectImage=新矩形(
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、 界限,高度
);
使用(var br=新的SolidBrush(背景色))
e、 图形填充矩形(br,e.Bounds);
e、 Graphics.DrawImage(bmp、rectImage);
TextRenderer.DrawText(如Graphics、game.GameName、e.Font、,
RECTXT,颜色。黑色,背景色,
TextFormatFlags.Left | TextFormatFlags.VerticalCenter);
}

为什么不删除不需要的项目(使用
GameAvailable==false
)?@DmitryBychenko its用于一个表单,用户可以显示所有可用的游戏,但需要在列表上注明哪些产品可用。这是什么?WinForms、WPF……等等?@JQSOFT抱歉,我应该首先提到这一点——这是WinForms。谢谢,这非常有效!我用了是/否的图片,我认为这看起来最好。我可以问一下你从哪里得到的红色和绿色图标吗?我买的没有你预览版上的好看。谢谢@帕维尔在这里,伙计