根据 class 中的值突出显示列表框项目
Highlight ListBox items based on value from class
是否可以循环遍历 ListBox 中的项目并通过检查 class 的值以某种方式突出显示或指示项目不可用?
基本上,得到了一个游戏 class 并且在存储的信息中游戏是否可用所以我需要在循环遍历列表框项目时检查这个 class 并以某种方式在列表框上指示 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)
{
}
}
}
}
}
是的,可以通过以下方式实现:
将 ListBox 绑定到 GameService.AllGames()
,我认为 returns 是 Game
对象的列表或数组。
设置 ListBox.DrawMode to DrawMode.OwnerDrawFixed
and handle the ListBox.DrawItem 事件以根据项目的 GameAvailable
属性绘制项目。
假设控件名称为Form1
和listBox1
,在Form1
构造函数中添加:
public Form1()
{
InitializeComponent();
//...
listBox1.DrawMode = DrawMode.OwnerDrawFixed;
listBox1.DrawItem += (s, e) => OnListBoxDrawItem(s, e);
listBox1.DataSource = GameService.AllGames();
}
假设你想用绿色显示可用的游戏,其余的用红色前景色显示。
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);
}
... 或者从您的资源中选择一些是 and no 图片:
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);
}
是否可以循环遍历 ListBox 中的项目并通过检查 class 的值以某种方式突出显示或指示项目不可用?
基本上,得到了一个游戏 class 并且在存储的信息中游戏是否可用所以我需要在循环遍历列表框项目时检查这个 class 并以某种方式在列表框上指示 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)
{
}
}
}
}
}
是的,可以通过以下方式实现:
将 ListBox 绑定到
GameService.AllGames()
,我认为 returns 是Game
对象的列表或数组。设置 ListBox.DrawMode to
DrawMode.OwnerDrawFixed
and handle the ListBox.DrawItem 事件以根据项目的GameAvailable
属性绘制项目。
假设控件名称为Form1
和listBox1
,在Form1
构造函数中添加:
public Form1()
{
InitializeComponent();
//...
listBox1.DrawMode = DrawMode.OwnerDrawFixed;
listBox1.DrawItem += (s, e) => OnListBoxDrawItem(s, e);
listBox1.DataSource = GameService.AllGames();
}
假设你想用绿色显示可用的游戏,其余的用红色前景色显示。
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);
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);
}