简体   繁体   English

如何在列表视图的子项中添加颜色? C#

[英]how to add color to sub item in listview? c#

I tried to add text color to any sub item in the listview by the index that the method get from the array 我试图通过方法从数组中获取的索引向列表视图中的任何子项添加文本颜色

for (int i = 0; i < sizes.Length; ++i)
            {
                if (sizes[i] == 1)
                {
                    Item.SubItems.Add("In Stock");
                }
                else if (sizes[i] == 0)
                {
                    Item.SubItems.Add("Out Of Stock");
                }
                else if (sizes[i] == 2)
                {
                    Item.SubItems.Add("Less Than 3");
                }
                else if (sizes[i] == 5)
                {
                    Item.SubItems.Add("Less Than 5");
                }
                else if (sizes[i] == 10)
                {
                    Item.SubItems.Add("Less Than 10");
                }
            }

            ProductListView.Items.Add(Item);
        }

if the size in stock the subitem color change to green and if the size is out of stock the subitem color will change to red 如果库存尺寸,子项目颜色变为绿色,如果尺寸不足,子项目颜色将变为红色

thanks 谢谢

The Add method returns the subItem, Add方法返回subItem,
So you can change the subItem color like this: 因此,您可以像下面这样更改subItem的颜色:

var subItem = Item.SubItems.Add("In Stock");
subItem.ForeColor = Color.Green;
// subItem.BackColor = Color.Red;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM