【问题标题】:How can i color each item in another color in a listView?如何在 listView 中为每个项目着色为另一种颜色?
【发布时间】:2017-03-14 00:40:17
【问题描述】:

首先这种处理listView在构造函数中添加项目的方式是正确的吗?其次,如何将所有文本“Ready”涂成红色?只有“准备好”?

        listView1.View = View.Details;
        listView1.GridLines = true;
        listView1.FullRowSelect = true;

        //Add column header
        listView1.Columns.Add("Status", 70);
        listView1.Columns.Add("Country", 70);
        listView1.Columns.Add("Link", 399);

        string[] arr = new string[countriesCodes.Length];
        ListViewItem itm;
        for (int i = 0; i < countriesCodes.Length; i++)
        {
            arr[0] = "Ready";
            arr[1] = countriesCodes[i];
            arr[2] = lines[i];
            itm = new ListViewItem(arr);
            listView1.Items.Add(itm);
        }

运行 prgoram 时列表视图的屏幕截图: 我只想将状态列行着色为红色,我的意思是仅将文本“就绪”着色为红色,其余不着色。

如果我要添加这个方法:

private void colorReady()
        {
            foreach (ListViewItem li in listView1.Items)
            {
                if (li.Text == "Ready")
                {
                    li.ForeColor = Color.Red;
                }
            }
        }

然后调用 colorReady();在设置完所有 listView 后的构造函数中,它将为 listView 中的所有项目着色,就像它在屏幕截图中显示的那样。

【问题讨论】:

标签: c# .net winforms


【解决方案1】:

做一个空并命名它,我将它命名为colorReady

    private void colorReady()
    {
        foreach (ListViewItem li in listView1.Items)
        {
            if(li.Text == "Ready")
            {
                li.SubItems.Add("Color");
                li.SubItems[0].ForeColor = Color.Red;
                li.UseItemStyleForSubItems = false;
            }
        }
    }

然后在你上面的代码之后调用它。

        listView1.View = View.Details;
        listView1.GridLines = true;
        listView1.FullRowSelect = true;

        //Add column header
        listView1.Columns.Add("Status", 70);
        listView1.Columns.Add("Country", 70);
        listView1.Columns.Add("Link", 399);
        string[] countriesCodes = new string[] { "test1", "test2", "test3" };
        string[] arr = new string[countriesCodes.Length];
        ListViewItem itm;
        for (int i = 0; i < countriesCodes.Length; i++)
        {
            arr[0] = "Ready";
            arr[1] = countriesCodes[i];
            itm = new ListViewItem(arr);
            listView1.Items.Add(itm);
        }

        colorReady();

添加一个未“准备好”的项目来测试结果。

【讨论】:

  • 我用截图编辑了我的问题。它不仅为所有项目着色“就绪”
  • 你的意思是你只希望'Ready'所在的单元格是红色的而不是整行?甚至不是“就绪”的行也是红色的。
  • 是的,我的意思是只有状态下的单元格是红色的。稍后我还想更改此单元格文本,例如从“就绪”更改为“已下载”,但是是的,我的意思是状态列下的所有单元格。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-05
  • 1970-01-01
  • 1970-01-01
  • 2013-08-15
  • 2014-10-17
  • 1970-01-01
相关资源
最近更新 更多