21

如何处理单击 ListView 的子项(详细模式)?即我需要检测究竟点击了什么列。

4

3 回答 3

16

您需要通过其位置来确定列:

private void listView_Click(object sender, EventArgs e)
{
    Point mousePos = listView.PointToClient(Control.MousePosition);
    ListViewHitTestInfo hitTest = listView.HitTest(mousePos);
    int columnIndex = hitTest.Item.SubItems.IndexOf(hitTest.SubItem);
}
于 2013-07-29T05:07:40.747 回答
13

这对我来说效果很好:

    private void listView_MouseDown(object sender, MouseEventArgs e)
    {
        var info = listView.HitTest(e.X, e.Y);
        var row = info.Item.Index;
        var col = info.Item.SubItems.IndexOf(info.SubItem);
        var value = info.Item.SubItems[col].Text;
        MessageBox.Show(string.Format("R{0}:C{1} val '{2}'", row, col, value));
    }
于 2014-09-11T20:08:14.203 回答
5

您可以ListView.MouseClick按如下方式使用该事件:

private void listView_MouseClick(object sender, MouseEventArgs e)
{
    // Hittestinfo of the clicked ListView location
    ListViewHitTestInfo listViewHitTestInfo = listView.HitTest(e.X, e.Y);

    // Index of the clicked ListView column
    int columnIndex = listViewHitTestInfo.Item.SubItems.IndexOf(listViewHitTestInfo.SubItem);

    ...
}
于 2016-05-09T10:49:35.087 回答