且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

双击 ListView 的一行时选择了哪一列

更新时间:2023-02-13 20:37:02

您可以使用 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);
}