C# – How to get ListViewItem under MouseCursor while dragging sth. over it

cdrag and droplistviewlistviewitemwinforms

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

Best Answer

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
  }
}