views:

248

answers:

2

Hi SO-followers,

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

+1  A: 

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)
{
  indexOfItem = 
    listBox.IndexFromPoint(listBox.PointToClient(new Point(e.X, e.Y)));
  if (indexOfItem != ListBox.NoMatches)
  {
     //do whatever - select it, etc
  }
}
Philip Rieck
Wow, the german short description of DragOver says sth. like: Occurs, if an element is dragged over the borders of the ListView-Control (so I thought DragOver occurs only one time if an element is dragged *out* of the borders of the ListView).Thank you very much!!!
Inno
+1  A: 

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.

Grammarian
Do I guess right that you are the guy behind ObjectListView (had a short look at your profile at SO and at CodeProject) ;).Thanks for the hint. ObjectListView looks great!
Inno
Yep. I'm the author. So, I'm biased -- but also accurate in this case :)
Grammarian