【问题标题】:C# ListView Detail, Highlight a single cellC# ListView 详细信息,突出显示单个单元格
【发布时间】:2010-09-17 22:22:53
【问题描述】:

我在 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();
}

【问题讨论】:

    标签: c# listview highlight ownerdrawn


    【解决方案1】:

    您可以在不绘制列表的情况下执行此操作:

    // 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 构造函数的 Color 参数控制子项的前景色和背景色。这里要做的关键是在列表项上将UseItemStyleForSubItems 设置为 False,否则您的颜色更改将被忽略。

    我认为您的所有者绘制解决方案也可以,但是您必须记住在将背景更改为蓝色时更改文本(前景)颜色,否则将很难看到文本。

    【讨论】:

      【解决方案2】:

      想通了。这是切换特定子项的突出显示的代码。

      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】:

        就我而言,我想突出显示特定行,包括所有字段。因此,我的列表视图中第一列中带有“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;
                            }
                        }
                    }
                }
        

        【讨论】:

          猜你喜欢
          • 2021-01-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-07-31
          • 1970-01-01
          • 2020-06-28
          • 2018-10-25
          • 1970-01-01
          相关资源
          最近更新 更多