C#ListView的详细信息,选中一个单细胞 [英] C# ListView Detail, Highlight a single cell

查看:189
本文介绍了C#ListView的详细信息,选中一个单细胞的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用C#一个ListView做一个网格。我想找到一种方法能够突出一个特定的细胞,编程。我只需要突出一个单元格。

我已经尝试了所有者绘制子项,但使用低于code,我得到凸显细胞,但没有文字!是否有关于如何得到这个工作什么想法?感谢您的帮助。

  // m_PC.Location是X,突出显示的单元的Y坐标。
无效listView1_DrawSubItem(对象发件人,DrawListViewSubItemEventArgs E)
{
    如果((e.ItemIndex == m_PC.Location.Y)及及(e.Item.SubItems.IndexOf(e.SubItem)== m_PC.Location.X))
        e.SubItem.BackColor = Color.Blue;
    其他
        e.SubItem.BackColor = Color.White;
    e.DrawBackground();
    e.DrawText();
}


解决方案

可以做到这一点没有所有者绘制列表:

  //创建一个具有蓝色背景上的白色文字子项目新列表项
ListViewItem的LVI =新的ListViewItem(项目文本);
lvi.UseItemStyleForSubItems = FALSE;
lvi.SubItems.Add(新ListViewItem.ListViewSubItem(LVI,
子项目,Color.White,Color.Blue,lvi.Font));

颜色参数的构造函数ListViewSubItem正在控制的子项的前景色和背景色。在这里做的关键的设置 UseItemStyleForSubItems 为False的列表项,否则你的颜色的变化将被忽略。

我觉得你的所有者绘制的解决方案也同样是工作,但你要记住,当你改变背景为蓝色更改的文本(前景)颜色,否则该文本将很难看到。

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

解决方案

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.

这篇关于C#ListView的详细信息,选中一个单细胞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆