如何更改ListView控件中单个子项的ForeColor?

loading...


21

我有一个Windows窗体应用程序,我有一个 ListView 控件 . 我想改变一个子项目的前景色 . 虽然我已经设法改变整行或列的颜色,但我还没有找到一种方法来为一个子项目做这个 . 有谁知道这样做的方法?

loading...

4回答

  • 4

    解决方案相当简单,并在ListViewSubItem.ForeColor property的文档的备注部分中指出:

    如果拥有子项的ListViewItem的UseItemStyleForSubItems属性设置为true,则设置此属性不起作用 .

    默认设置旨在为 ListView 控件中的单个项目拥有的所有子项目保持一致的外观,以便您只需在一个位置更改属性 . To change the default behavior, you need to set the UseItemStyleForSubItems property of a particular ListViewItem to "False".

    完成后,您可以调整单个子项的 ForeColor (或任何其他)属性 . 例如:

    myListView.Items[0].UseItemStyleForSubItems = false;
    myListView.Items[0].SubItems[1].ForeColor = Color.Red;
    

  • 1

    此示例将为每个具有值“Monday”的子项着色

    public static void colorListcolor(ListView lsvMain)
        {           
    
            foreach (ListViewItem lvw in lsvMain.Items)
            {
                lvw.UseItemStyleForSubItems = false;
    
                for (int i = 0; i < lsvMain.Columns.Count; i++)
                {
                    if (lvw.SubItems[i].Text.ToString() == "Monday")
                    {
                        lvw.SubItems[i].BackColor = Color.Red;
                        lvw.SubItems[i].ForeColor = Color.White;
                    }
                    else {
                        lvw.SubItems[i].BackColor = Color.White;
                        lvw.SubItems[i].ForeColor = Color.Black; 
                    }
                }
    
            }      
        }[![Screenshot of the result from the code above][1]][1]
    

  • 3

    这是我如何显示多客户端SCCM推送/删除的结果 . 传递的CSV是(字符串ClientName,字符串PackageName,字符串EventType,bool结果),但是我想显示最长的字段,最后是Package Name,并且让结果更容易看到成功/失败的颜色 .

    private void MultiClientResultForm_Load(object sender, EventArgs e)
        {
            foreach (string token in Main.ClientListResults)
            {
                string[] ResultRecord = token.Split(new string[] { "," }, StringSplitOptions.None);
                if (ResultRecord[0] != "")
                {
                    ListViewItem row = new ListViewItem(ResultRecord[0]);
                    row.SubItems.Add(new ListViewItem.ListViewSubItem(row, ResultRecord[2]));
                    if (ResultRecord[3] == "true")
                    {
                        row.SubItems.Add(new ListViewItem.ListViewSubItem(row, "Success"));
                        row.SubItems[2].ForeColor = Color.ForestGreen;
                    }
                    else 
                    {
                        row.SubItems.Add(new ListViewItem.ListViewSubItem(row, "Fail"));
                        row.SubItems[2].ForeColor = Color.DarkRed;
                    }
                    row.SubItems.Add(new ListViewItem.ListViewSubItem(row, ResultRecord[3]));
                    row.SubItems.Add(new ListViewItem.ListViewSubItem(row, ResultRecord[1]));
                    listViewResults.Items.Add(row);
                }
            }
        }
    

  • 51

    一种方法是设置所有子项的颜色,并根据需要更改 .

    示例代码:

    private void btn_Add_Click(object sender, EventArgs e)
        {
    
            ListViewItem lvi = new ListViewItem();
            ListViewItem.ListViewSubItem lvsi1 = new ListViewItem.ListViewSubItem();
            ListViewItem.ListViewSubItem lvsi2 = new ListViewItem.ListViewSubItem();
    
            lvi.Text = tb_Main.Text;
            lvsi1.Text = tb_Sub1.Text;
            lvsi2.Text = tb_Sub2.Text;
    
            lvi.UseItemStyleForSubItems = false;
    
            lv_List.ForeColor = Color.Black;
    
            if (lvsi1.Text == tb_Different.Text)
            {
                lvsi1.ForeColor = Color.Red;
            }
    
            if (lvsi2.Text == tb_Different.Text)
            {
                lvsi2![enter image description here][2].ForeColor = Color.Red;
            }
    
            lv_List.Items.Add(lvi);
            lvi.SubItems.Add(lvsi1);
            lvi.SubItems.Add(lvsi2);
    
        }
    

    enter image description here

评论

暂时没有评论!