How to get ListViewItem under MouseCursor while dragging sth. over it
-
22-09-2019 - |
Question
I'm implementing drag & drop on a ListView. I already managed to get the ListViewItem under the cursor when dropping on it but I would like to get the ListViewItem under the mouse cursor while I'm dragging sth. over the ListView-Control.
I would like to select the ListViewItem (selected=true) like in Windows Explorer when you are dragging files over a folder.
I thought about events like ItemMouseHover, MouseMove in the ListView but they are not fired when dragging sth. over it.
Hope you can help me...
Regards,
inno
P.S.: I'm using .Net2.0
Solution
Have you tried responding to the DragOver event in the listview class? You should be able to do it this way.
private void listBox_DragOver(object sender,
DragEventArgs e)
{
//for ListView
var point = listView.PointToClient(new Point(e.X, e.Y));
var item = listView.GetItemAt( point.X, point.Y);
if(item != null)
{
//do whatever - select it, etc
}
//or, for ListBox
var indexOfItem =
listBox.IndexFromPoint(listBox.PointToClient(new Point(e.X, e.Y)));
if (indexOfItem != ListBox.NoMatches)
{
//do whatever - select it, etc
}
}
OTHER TIPS
If you are doing drag and drop in a ListView, you learn a lot by looking at the code of ObjectListView (an open source wrapper around .NET WinForms ListView).
If you use an ObjectListView instead of a normal ListView, a lot of things, like drag and drop, happen automatically.