Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handle click on a sub-item of ListView

How can I handle click on a sub-item of ListView (detail mode)? i.e. I need to detect what exactly column was clicked.

like image 408
Steve Kero Avatar asked Jul 29 '13 04:07

Steve Kero


3 Answers

You need to determine the column by its position:

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);
}
like image 139
Romano Zumbé Avatar answered Oct 31 '22 15:10

Romano Zumbé


This is working well for me:

    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));
    }
like image 24
Mishka Avatar answered Oct 31 '22 15:10

Mishka


You can use the ListView.MouseClick event as follows:

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

    ...
}
like image 44
Pollitzer Avatar answered Oct 31 '22 13:10

Pollitzer