【发布时间】:2018-06-08 11:19:17
【问题描述】:
我正在用 winforms 开发一个软件,我被困在我有的一步
List<KeyValuePair<string, string>>.
还有一些示例数据:
List <KeyValuePair<"S", "1200">>
List <KeyValuePair<"S", "1300">>
List <KeyValuePair<"L", "1400">>
我想在 ListBox 中显示密钥对的值,根据该对的密钥,ListBox 上的 Item 具有不同的颜色,例如,如果 Key 是 S,则 Item 应该是红色并且如果 Key 是 L,则 Item 应该是蓝色的。
希望你能帮我解决这个问题。
这是我做的代码,但它没有达到预期的效果:
e.DrawBackground();
Graphics g = e.Graphics;
Graphics x = e.Graphics;
g.FillRectangle(new SolidBrush(Color.Olive), e.Bounds);
x.FillRectangle(new SolidBrush(Color.Aquamarine), e.Bounds);
foreach (var workOrders in GetItac.FilterWorkOrders())
{
if (workOrders.Key == "S")
{
g.DrawString(workOrders.Value, e.Font, new SolidBrush(e.ForeColor), new PointF(e.Bounds.X, e.Bounds.Y));
}
else
{
x.DrawString(workOrders.Value, e.Font, new SolidBrush(e.ForeColor), new PointF(e.Bounds.X, e.Bounds.Y));
}
}
【问题讨论】:
-
这会有所帮助。 stackoverflow.com/questions/2554609/… 也是重复的。
-
那些答案没有告诉我如何根据键更改颜色。另外我不想改变背景颜色,我需要改变项目的前景色。
标签: c# winforms graphics listbox listboxitem