private void list_DragOver(object sender, DragEventArgs e) { if (dragData == null) { return; } Point cp = list.PointToClient(new Point(e.X, e.Y)); ListViewItem dragToItem = list.GetItemAt(10, cp.Y); if (dragToItem == null) { return; } DragEventData.DragEventTargetMode mode = ((cp.Y > (dragToItem.Bounds.Top + dragToItem.Bounds.Height / 2)) ? DragEventData.DragEventTargetMode.insertAfter : DragEventData.DragEventTargetMode.insertBefore); if (dragData.DragToItem == dragToItem && dragData.Mode == mode) { return; } using (System.Drawing.Graphics gr = System.Drawing.Graphics.FromHwnd(list.Handle)) { if (dragData.DragToItem != null) { DragEventData.DrawMoveLine(gr, list.BackColor, dragData.DragToItem.Bounds, dragData.Mode); } DragEventData.DrawMoveLine(gr, Color.Red, dragToItem.Bounds, mode); } dragData.DragToItem = dragToItem; dragData.Mode = mode; }
private void list_DragDrop(object sender, DragEventArgs e) { try { if (dragData == null) { return; } ListViewItem[] sel = null; if (e.Effect == DragDropEffects.Move && dragData.DragToItem != null && list.SelectedItems.Count != 0) { //Obtain the index of the item at the mouse pointer. sel = new ListViewItem[list.SelectedItems.Count]; for (int i = 0; i <= list.SelectedItems.Count - 1; i++) { sel[i] = list.SelectedItems[i]; } } if (sel == null) { return; } int counter = 0; foreach (ListViewItem selItem in sel) { if (selItem == dragData.DragToItem) { continue; } if (list.Items.Contains(selItem)) { list.Items.Remove(selItem); } int dragIndex = dragData.DragToItem.Index; switch (dragData.Mode) { case DragEventData.DragEventTargetMode.insertBefore: list.Items.Insert(dragIndex, selItem); break; case DragEventData.DragEventTargetMode.insertAfter: list.Items.Insert(dragIndex + 1 + counter++, selItem); break; } } if (AfterLegendOrdering != null) { AfterLegendOrdering(this, new EventArgs()); } } finally { if (dragData != null && dragData.DragToItem != null) { using (System.Drawing.Graphics gr = System.Drawing.Graphics.FromHwnd(list.Handle)) { DragEventData.DrawMoveLine( gr, list.BackColor, dragData.DragToItem.Bounds, dragData.Mode); } } dragData = null; } }