Getting an item under the mouse cursor in a list control?

Basically, I am trying to implement a function in which, if the user presses a key, I want to find out the element under the mouse cursor.

Therefore, I do not use Mouse events, but Keyboard events that do not give me a ListViewItem, of course.

I just don’t know in which space I need to get the mouse position and convert it to a control space.

Any ideas?

+3
source share
2 answers

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);
+13
source

, , . - , // , , .

- , "" () .

0

Source: https://habr.com/ru/post/1711290/


All Articles