双击 ListView 的一行时选择了哪一列 [英] Which Column was selected when DoubleClick a Row of a ListView

查看:31
本文介绍了双击 ListView 的一行时选择了哪一列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我不是在谈论单击列标题.我说的是选定行中的项目.
我有一个 ListView,想要双击所选行中的 username 列并将用户名复制到剪贴板.
如果我双击所选行的 password 列,将密码复制到剪贴板.

First of all, I'm not talking about clicking the column header. I'm talking about the item in a selected row.
I have a ListView and wanting to double click the username column in a selected row and copy username to clipboard.
If I double click the password column of a selected row, copy the password to clipboard.

显然我们可以知道选择了哪一行,但我不知道如何捕获在该行中双击哪一列.
这可能吗?

Obviously we can tell what row is selected, but I can't figure out how to capture what column was double clicked within that row.
Is that possible?

人们想要代码,所以在这里你去吧,记住只能选择单行所以是的,0 会工作:

People want code so here ya go, keep in mind only single row can be selected so yes, 0 will work:

ListViewItem lvi = this.contentListView.SelectedItems[0];
string pass = lvi.SubItems[5].Text;

推荐答案

您可以使用 ListView HitTest 方法.它返回一个 ListViewHitTestInfo 对象.
它的 SubItem 属性为您提供ListViewSubItem 对象被点击:

You can use the ListView HitTest method. It returns a ListViewHitTestInfo object.
Its SubItem property gives you the ListViewSubItem object clicked:

可以使用 ListViewItem.SubItems.IndexOf() 方法.ListViewItemSubItem 都在 ListViewHitTestInfo 对象中被引用.

The corresponding ColumnHeader object can be identified using the ListViewItem.SubItems.IndexOf() method. Both the ListViewItem and SubItem are referenced in the ListViewHitTestInfo object.

private void listView1_MouseDoubleClick(object sender, MouseEventArgs e)
{
    var hitInfo = listView1.HitTest(e.Location);
    if (hitInfo.SubItem == null || string.IsNullOrEmpty(hitInfo.SubItem.Text)) return;

    int subItemIndex = hitInfo.Item.SubItems.IndexOf(hitInfo.SubItem);
    var column = listView1.Columns[subItemIndex];

    // Do whatever you need to do with the SubItem text
    string result = ProcessSubItemText(hitInfo.SubItem.Text, column);
    Clipboard.SetText(result, TextDataFormat.UnicodeText);
}

这篇关于双击 ListView 的一行时选择了哪一列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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