Basically I am trying to implement a feature where if the user presses a key, I want to find out the item under the mouse cursor.
So I don't use Mouse events but Keyboard events which doesn't give me a ListViewItem of course.
I just don't know in what space I need to get the mouse position and convert it into the control's space.
Any ideas?
If you know which ListView control you are interested in, the following method will do the trick:
private ListViewItem GetItemFromPoint(ListView listView, Point mousePosition)
{
// translate the mouse position from screen coordinates to
// client coordinates within the given ListView
Point localPoint = listView.PointToClient(mousePosition);
return listView.GetItemAt(localPoint.X, localPoint.Y);
}
// call it like this:
ListViewItem item = GetItemFromPoint(myListView, Cursor.Position);
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