代码之家  ›  专栏  ›  技术社区  ›  Mike Christiansen

C ListView详细信息,突出显示单个单元格

  •  10
  • Mike Christiansen  · 技术社区  · 16 年前

    我正在使用C中的ListView创建网格。我想找出一种方法,能够突出显示一个特定的细胞,编程。我只需要突出显示一个单元格。

    我已经尝试过所有者绘制的子项,但是使用下面的代码,我会得到突出显示的单元格,但没有文本!有什么办法可以让这个工作吗?谢谢你的帮助。

    //m_PC.Location is the X,Y coordinates of the highlighted cell.
    
    
    void listView1_DrawSubItem(object sender, DrawListViewSubItemEventArgs e)
    {
        if ((e.ItemIndex == m_PC.Location.Y) && (e.Item.SubItems.IndexOf(e.SubItem) == m_PC.Location.X))
            e.SubItem.BackColor = Color.Blue;
        else
            e.SubItem.BackColor = Color.White;
        e.DrawBackground();
        e.DrawText();
    }
    
    3 回复  |  直到 12 年前
        1
  •  14
  •   Charlie    16 年前

    您可以在没有所有者绘制列表的情况下执行此操作:

    // create a new list item with a subitem that has white text on a blue background
    ListViewItem lvi = new ListViewItem( "item text" );
    lvi.UseItemStyleForSubItems = false;
    lvi.SubItems.Add( new ListViewItem.ListViewSubItem( lvi,
        "subitem", Color.White, Color.Blue, lvi.Font ) );
    

    ListViewSubItem构造函数的颜色参数控制子项的前景色和背景色。这里最关键的事情就是 UseItemStyleForSubItems 若要在列表项中设置为false,否则将忽略您的颜色更改。

    我认为你的主人画的解决方案也会奏效,但你必须记住改变文本(前景)的颜色,当你改变背景为蓝色,否则文本将很难看到。

        2
  •  2
  •   Mike Christiansen    16 年前

    明白了。下面是用于切换特定子项的突出显示的代码。

    listView1.Items[1].UseItemStyleForSubItems = false;
    if (listView1.Items[1].SubItems[10].BackColor == Color.DarkBlue)
    {
        listView1.Items[1].SubItems[10].BackColor = Color.White;
        listView1.Items[1].SubItems[10].ForeColor = Color.Black;
    }
    else
    {
        listView1.Items[1].SubItems[10].BackColor = Color.DarkBlue;
        listView1.Items[1].SubItems[10].ForeColor = Color.White;
    }
    
        3
  •  1
  •   kiritsuku    12 年前

    在我的例子中,我希望突出显示特定的行,包括所有字段。因此,在我的ListView中,第一列中带有“Medicare”的每一行都会突出显示整个行:

    public void HighLightListViewRows(ListView xLst)
            {
                for (int i = 0; i < xLst.Items.Count; i++)
                {
                    if (xLst.Items[i].SubItems[0].Text.ToString() == "Medicare")
                    {
                        for (int x = 0; x < xLst.Items[i].SubItems.Count; x++)
                        {
                            xLst.Items[i].SubItems[x].BackColor = Color.Yellow;
                        }
                    }
                }
            }