Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/268.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# 如何在winforms中创建列表框的多色项?_C#_Winforms_Listbox_Listboxitem - Fatal编程技术网

C# 如何在winforms中创建列表框的多色项?

C# 如何在winforms中创建列表框的多色项?,c#,winforms,listbox,listboxitem,C#,Winforms,Listbox,Listboxitem,我正在用winforms开发一个软件,我被困在一个我已经完成的步骤中 List<KeyValuePair<string, string>>. 当您需要在ListBox控件中显示自定义结果时,您需要启用列表中项的自定义绘制,将ListBox属性设置为OwnerDrawVariable或OwnerDrawFixed(后者将所有项设置为相同高度)。 (注意→ 在这里,我将其设置为OwnerDrawVariable) 如果出现列表框,必须使用e.Graphics对象执行列表

我正在用winforms开发一个软件,我被困在一个我已经完成的步骤中

 List<KeyValuePair<string, string>>. 

当您需要在
ListBox
控件中显示自定义结果时,您需要启用列表中
项的自定义绘制,将
ListBox
属性设置为
OwnerDrawVariable
OwnerDrawFixed
(后者将所有项设置为相同高度)。
注意→ 在这里,我将其设置为
OwnerDrawVariable

如果出现
列表框
,必须使用
e.Graphics
对象执行列表
项目
绘制。这允许在
列表框
/
表单
需要重新绘制时正确刷新
项。

1-您不必将图形对象相乘。
2-也不需要使用
foreach
循环,因为在创建/修改
列表框
集合时,将绘制每个项。

注意→ 我正在以代码中显示的方式绘制
项目
背景,但视觉效果可能有点奇怪(这些颜色混合不好)。


首先,模拟您的
GetItac.FilterWorkOrders()
方法的结果,该方法将返回一个
List
,将那些
Value
项添加到列表中:

using System.Collections.Generic;

List<KeyValuePair<string, string>> workOrders;

workOrders = new List<KeyValuePair<string, string>>()
{
    new KeyValuePair<string, string>("S", "1200" ),
    new KeyValuePair<string, string>("S", "1300"),
    new KeyValuePair<string, string>("L", "1400")
};
//Select().ToList() extracts the Value string from the KeyValuePair elements
listBox1.DataSource = workOrders.Select(kv => kv.Value).ToList();
列表框
项目
集合被填充时,将引发
DrawItem
事件,以允许绘制
项目
内容。
您可能需要添加事件,以确保正确测量每个项目的高度(如果您计划修改
列表框
字体,则必须添加)

对于绘制的每个项目,通过测试
KeyValuePair
Key
选择文本颜色:如果其值为
“S”
,则文本颜色设置为
color.Red
,背景颜色设置为
color.Olive
。否则,它们将设置为
color.Blue
color.Aquamarine

private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{
    e.DrawBackground();
    Color textColor = (workOrders[e.Index].Key == "S") ? Color.Red : Color.Blue;
    Color backColor = (workOrders[e.Index].Key == "S") ? Color.Olive : Color.Aquamarine;
    using (SolidBrush backColorBrush = new SolidBrush(backColor))
    using (SolidBrush foreColorBrush = new SolidBrush(textColor))
    {
        e.Graphics.FillRectangle(backColorBrush, e.Bounds);
        e.Graphics.DrawString(workOrders[e.Index].Value, listBox1.Font, foreColorBrush, 
                              e.Bounds, StringFormat.GenericTypographic);
    }
}

private void listBox1_MeasureItem(object sender, MeasureItemEventArgs e)
{
    e.ItemHeight = listBox1.Font.Height;
}

这会有帮助。它也是一个副本。这些答案的可能副本不会告诉我如何根据键更改颜色。此外,我不想更改背景颜色,我需要更改项目的前景色。@Omar AlMadani,很高兴它有帮助:)
using System.Collections.Generic;

List<KeyValuePair<string, string>> workOrders;

workOrders = new List<KeyValuePair<string, string>>()
{
    new KeyValuePair<string, string>("S", "1200" ),
    new KeyValuePair<string, string>("S", "1300"),
    new KeyValuePair<string, string>("L", "1400")
};
//Select().ToList() extracts the Value string from the KeyValuePair elements
listBox1.DataSource = workOrders.Select(kv => kv.Value).ToList();
workOrders = GetItac.FilterWorkOrders();
listBox1.DataSource = workOrders.Select(kv => kv.Value).ToList();
//Or
workOrders = GetItac.FilterWorkOrders();
listBox1.Items.AddRange(workOrders.Select(kv => kv.Value).ToArray());
private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{
    e.DrawBackground();
    Color textColor = (workOrders[e.Index].Key == "S") ? Color.Red : Color.Blue;
    Color backColor = (workOrders[e.Index].Key == "S") ? Color.Olive : Color.Aquamarine;
    using (SolidBrush backColorBrush = new SolidBrush(backColor))
    using (SolidBrush foreColorBrush = new SolidBrush(textColor))
    {
        e.Graphics.FillRectangle(backColorBrush, e.Bounds);
        e.Graphics.DrawString(workOrders[e.Index].Value, listBox1.Font, foreColorBrush, 
                              e.Bounds, StringFormat.GenericTypographic);
    }
}

private void listBox1_MeasureItem(object sender, MeasureItemEventArgs e)
{
    e.ItemHeight = listBox1.Font.Height;
}