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

发布于 2024-07-07 04:03:23 字数 579 浏览 20 评论 0原文

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

I'm using a ListView in C# to make a grid. I would like to find out a way to be able to highlight a specific cell, programatically. I only need to highlight one cell.

I've experimented with Owner Drawn subitems, but using the below code, I get highlighted cells, but no text! Are there any ideas on how to get this working? Thanks for your help.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

青巷忧颜 2024-07-14 04:03:23

您无需所有者绘制列表即可执行此操作:

// 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,否则您的颜色更改将被忽略。

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

You can do this without owner-drawing the list:

// 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 ) );

The Color arguments to the ListViewSubItem constructor are controlling the foreground and background color of the subitem. The critical thing to do here is set UseItemStyleForSubItems to False on the list item, otherwise your color changes will be ignored.

I think your owner-draw solution would have worked as well, but you have to remember to change the text (foreground) color when you change the background to blue, otherwise the text will be hard to see.

两人的回忆 2024-07-14 04:03:23

弄清楚了。 下面的代码用于切换特定子项的突出显示。

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;
}

Figured it out. Here's code to toggle the highlight of a specific subitem.

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;
}
晨光如昨 2024-07-14 04:03:23

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

In my case, I wanted to highlight specific rows, including all the fields. So every row in my listview with "Medicare" in the first column gets the entire row highlighted:

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;
                    }
                }
            }
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文