2013-07-29 12 views

risposta

11

è necessario determinare la colonna per la sua posizione:

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

Questo non funzionerebbe su un evento click .. ma ha funzionato sull'evento doppio clic (.NET 4.5 :) – user1265146

+0

Anche w ork nell'evento MouseDown .. – TaW

11

Questo sta lavorando bene per 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)); 
    } 
1

È possibile utilizzare l'evento ListView.MouseClick come segue:

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

    ... 
} 
Problemi correlati