I need to get the column clicked in a ListView in C#
I have some sample code from How to determine the clicked column index in a Listview but I'm not sure how I should implement it.
Jeez, everyone's too lazy to post code. There are three steps to the process:
Control.MousePosition
and convert to client coordinates.HitTest
function to find what the mouse is pointing to. This returns an object with lots of information except the actual column number so...IndexOf
to find the column number.Here's the code:
private void listViewMasterVolt_DoubleClick(object sender, EventArgs e)
{
Point mousePosition = myListView.PointToClient(Control.MousePosition);
ListViewHitTestInfo hit = myListView.HitTest(mousePosition);
int columnindex = hit.Item.SubItems.IndexOf(hit.SubItem);
}
The ListView
control has a HitTest
method. You give it the x- and y-coordinates of the mouse click event, and it gives you an object that tells you the row (list view item) and column (list view subitem) at that point.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With