处理单击ListView的子项

时间:2013-07-29 04:41:57

标签: c# .net windows winforms user-interface

如何处理单击ListView的子项(详细模式)?即我需要检测单击列的确切内容。

3 个答案:

答案 0 :(得分:15)

您需要按位置确定列:

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

答案 1 :(得分:12)

这对我很有用:

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

答案 2 :(得分:2)

您可以按如下方式使用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);

    ...
}