Click here to Skip to main content
Click here to Skip to main content

Drag and Drop List View

, 12 May 2004
Rate this:
Please Sign up or sign in to vote.
Drag and Drop list items within list views or to other list views without manual intervention.

Sample Image - DragAndDropListView.gif

Introduction

An upcoming application at work required me to have drag and drop functionality between many different ListViews or DataGrids. When I got word, I decided to start searching the Internet and find a pre-existing free control that I could use. My personal requirements were that the ListView or DataGrid needed to have the ability to move rows around within its own control, but also allow me to move rows to other controls. My initial sorting pointed me towards Drag and Drop ListView row reordering by David Boland. As his control allowed users to reorder items in a ListView, it lacked support for moving to other ListViews as well as letting the user know where the new rows are to be placed. This control is somewhat similar to David's, however, it was rewritten only using David's code as a reference.

Using the Code

The DragAndDropListView control inherits from ListView, and provides native support for dragging and dropping ListItems to reorder them or move them to other DragAndDropListView controls. Because it needs to utilize Drag and Drop functionality, I had to override the OnDragDrop, OnDragOver, OnDragEnter, OnItemDrag, OnLostFocus, and OnDragLeave to provide the functionality.

When an item is selected and started to be dragged, OnItemDrag gets called which starts the Drag and Drop functionality by retrieving the control it originates from as well as the selected items. Both of these are stored in a private class that is passed around in the data of the drag and drop events.

protected override void OnItemDrag(ItemDragEventArgs e)
{
 if(!m_allowReorder)
 {
  base.OnItemDrag(e);
  return;
 }

 base.DoDragDrop(GetDataForDragDrop(), DragDropEffects.Move);

 base.OnItemDrag(e);
}

As the user moves the selected list items over the ListView, OnDragOver is called. This method basically determines if the selected items to be dragged & dropped can actually be dragged & dropped. By checking to see if the item it's currently hovering over is not one of the list items being moved, it either displays the move cursor or denied cursor. If the user is trying to drop the items into an area that doesn't have a list item, it will still allow you, thus allowing you to place and reorder items in any order.

protected override void OnDragOver(DragEventArgs drgevent)
{
 if(!m_allowReorder)
 {
  base.OnDragOver(drgevent);
  return;
 }
 if(!drgevent.Data.GetDataPresent(typeof(DragItemData).ToString()))
 {
  drgevent.Effect = DragDropEffects.None;
  return;
 }

 if(base.Items.Count > 0)
 {
  Point clientPoint = base.PointToClient(new Point(drgevent.X, drgevent.Y));
  ListViewItem hoverItem = base.GetItemAt(clientPoint.X, clientPoint.Y);

  Graphics g = this.CreateGraphics();

  if(hoverItem == null)
  {
   drgevent.Effect = DragDropEffects.Move;

   if(m_previousItem != null)
   {
    m_previousItem = null;
    Invalidate();
   }

   hoverItem = base.Items[base.Items.Count - 1];

   if(this.View == View.Details || this.View == View.List)
   {
    g.DrawLine(new Pen(m_lineColor, 2), new Point(hoverItem.Bounds.X, 
     hoverItem.Bounds.Y + hoverItem.Bounds.Height), 
     new Point(hoverItem.Bounds.X + 
     this.Bounds.Width, hoverItem.Bounds.Y + 
     hoverItem.Bounds.Height));
    g.FillPolygon(new SolidBrush(m_lineColor),
     new Point[] {new Point(hoverItem.Bounds.X, 
     hoverItem.Bounds.Y + hoverItem.Bounds.Height - 5), 
     new Point(hoverItem.Bounds.X + 5, hoverItem.Bounds.Y + 
     hoverItem.Bounds.Height), 
     new Point(hoverItem.Bounds.X, hoverItem.Bounds.Y + 
     hoverItem.Bounds.Height + 5)});
    g.FillPolygon(new SolidBrush(m_lineColor), 
     new Point[] {new Point(this.Bounds.Width - 4, 
     hoverItem.Bounds.Y + hoverItem.Bounds.Height - 5), 
     new Point(this.Bounds.Width - 9, 
     hoverItem.Bounds.Y + hoverItem.Bounds.Height), 
     new Point(this.Bounds.Width - 4, 
     hoverItem.Bounds.Y + hoverItem.Bounds.Height + 5)});
   }
   else
   {
    g.DrawLine(new Pen(m_lineColor, 2), 
     new Point(hoverItem.Bounds.X + 
     hoverItem.Bounds.Width, hoverItem.Bounds.Y), 
     new Point(hoverItem.Bounds.X + 
     hoverItem.Bounds.Width, hoverItem.Bounds.Y + 
     hoverItem.Bounds.Height));
    g.FillPolygon(new SolidBrush(m_lineColor), 
     new Point[] {new Point(hoverItem.Bounds.X + 
     hoverItem.Bounds.Width - 5, hoverItem.Bounds.Y), 
     new Point(hoverItem.Bounds.X + 
     hoverItem.Bounds.Width + 5, hoverItem.Bounds.Y), 
     new Point(hoverItem.Bounds.X + 
     hoverItem.Bounds.Width, hoverItem.Bounds.Y + 5)});
    g.FillPolygon(new SolidBrush(m_lineColor), 
     new Point[] {new Point(hoverItem.Bounds.X + 
     hoverItem.Bounds.Width - 5, hoverItem.Bounds.Y + 
     hoverItem.Bounds.Height), 
     new Point(hoverItem.Bounds.X + 
     hoverItem.Bounds.Width + 5, hoverItem.Bounds.Y + 
     hoverItem.Bounds.Height), 
     new Point(hoverItem.Bounds.X + hoverItem.Bounds.Width, 
     hoverItem.Bounds.Y + hoverItem.Bounds.Height - 5)});
   }

   base.OnDragOver(drgevent);

   return;
  }

  if((m_previousItem != null && 
    m_previousItem != hoverItem) || m_previousItem == null)
  {
   this.Invalidate();
  }

  m_previousItem = hoverItem;

  if(this.View == View.Details || this.View == View.List)
  {
   g.DrawLine(new Pen(m_lineColor, 2), new Point(hoverItem.Bounds.X, 
    hoverItem.Bounds.Y), new Point(hoverItem.Bounds.X + this.Bounds.Width, 
    hoverItem.Bounds.Y));
   g.FillPolygon(new SolidBrush(m_lineColor), 
    new Point[] {new Point(hoverItem.Bounds.X, 
    hoverItem.Bounds.Y - 5), new Point(hoverItem.Bounds.X + 5, 
    hoverItem.Bounds.Y), new Point(hoverItem.Bounds.X, 
    hoverItem.Bounds.Y + 5)});
   g.FillPolygon(new SolidBrush(m_lineColor), 
    new Point[] {new Point(this.Bounds.Width - 4, 
    hoverItem.Bounds.Y - 5), new Point(this.Bounds.Width - 9, 
    hoverItem.Bounds.Y), new Point(this.Bounds.Width - 4, 
    hoverItem.Bounds.Y + 5)});
  }
  else
  {
   g.DrawLine(new Pen(m_lineColor, 2), 
    new Point(hoverItem.Bounds.X, hoverItem.Bounds.Y), 
    new Point(hoverItem.Bounds.X, 
    hoverItem.Bounds.Y + hoverItem.Bounds.Height));
   g.FillPolygon(new SolidBrush(m_lineColor), 
    new Point[] {new Point(hoverItem.Bounds.X - 5, 
    hoverItem.Bounds.Y), 
    new Point(hoverItem.Bounds.X + 5, hoverItem.Bounds.Y), 
    new Point(hoverItem.Bounds.X, hoverItem.Bounds.Y + 5)});
   g.FillPolygon(new SolidBrush(m_lineColor), 
    new Point[] {new Point(hoverItem.Bounds.X - 5, 
    hoverItem.Bounds.Y + hoverItem.Bounds.Height), 
    new Point(hoverItem.Bounds.X + 5, 
    hoverItem.Bounds.Y + hoverItem.Bounds.Height), 
    new Point(hoverItem.Bounds.X, 
    hoverItem.Bounds.Y + hoverItem.Bounds.Height - 5)});

  }

  foreach(ListViewItem itemToMove in base.SelectedItems)
  {
   if(itemToMove.Index == hoverItem.Index)
   {
    drgevent.Effect = DragDropEffects.None;
    hoverItem.EnsureVisible();
    return;
   }
  }

  hoverItem.EnsureVisible();
 }

 drgevent.Effect = DragDropEffects.Move;

 base.OnDragOver(drgevent);
}

One of the really cool things that I wanted to do, was to add a line either above or below the hovered list item, however, this really didn't pose an easy feat at first. To figure this out, I had 2 actual implementations of creating the line, 1 for when you were hovering over a list item, and 1 for when you were in an empty area. When you were hovering over a list item, the line needed to be added between that item and the item above it. If the user is currently over the empty area, and items existed, then the line would be added to the bottom of the very last item. Below is the code that shows how to add the line and arrows on both side:

[Add the Line to the Last Item on Bottom]
g.DrawLine(new Pen(Brushes.Red, 2), new Point(hoverItem.Bounds.X, 
    hoverItem.Bounds.Y + hoverItem.Bounds.Height), 
    new Point(hoverItem.Bounds.X + this.Bounds.Width, 
    hoverItem.Bounds.Y + hoverItem.Bounds.Height));
g.FillPolygon(Brushes.Red, new Point[] {new Point(hoverItem.Bounds.X, 
    hoverItem.Bounds.Y + hoverItem.Bounds.Height - 5), 
    new Point(hoverItem.Bounds.X + 5, 
    hoverItem.Bounds.Y + hoverItem.Bounds.Height), 
    new Point(hoverItem.Bounds.X, 
    hoverItem.Bounds.Y + hoverItem.Bounds.Height + 5)});
g.FillPolygon(Brushes.Red, new Point[] {new Point(this.Bounds.Width - 4, 
    hoverItem.Bounds.Y + hoverItem.Bounds.Height - 5), 
    new Point(this.Bounds.Width - 9, 
    hoverItem.Bounds.Y + hoverItem.Bounds.Height), 
    new Point(this.Bounds.Width - 4, 
    hoverItem.Bounds.Y + hoverItem.Bounds.Height + 5)});
[Add the Line to the Top of the Current Item]
g.DrawLine(new Pen(Brushes.Red, 2), new Point(hoverItem.Bounds.X, 
    hoverItem.Bounds.Y), new Point(hoverItem.Bounds.X + 
    this.Bounds.Width, hoverItem.Bounds.Y));
g.FillPolygon(Brushes.Red, new Point[] {new Point(hoverItem.Bounds.X, 
    hoverItem.Bounds.Y - 5), new Point(hoverItem.Bounds.X + 5, 
    hoverItem.Bounds.Y), 
    new Point(hoverItem.Bounds.X, hoverItem.Bounds.Y + 5)});
g.FillPolygon(Brushes.Red, new Point[] {new Point(this.Bounds.Width - 4, 
    hoverItem.Bounds.Y - 5), new Point(this.Bounds.Width - 9, 
    hoverItem.Bounds.Y), 
    new Point(this.Bounds.Width - 4, hoverItem.Bounds.Y + 5)});

Previous versions of this article stated that lines would only be drawn if it was in Details or Line mode, however this updated version includes both Small Icon List and Large Icon List replacing horizontal lines with vertical lines. The demo has also been updated, so you can test this method out as well.

As the user is dragging the selected items around the form, OnLostFocus and OnDragLeave are utilized to invalidate the form so the drawn lines are erased.

Lastly, OnDragDrop is called whenever the user drops the selected items into a DragAndDropListView. This method determines if the items to be dropped are from the current DragAndDropListView or from another DragAndDropListView. At which point, the items are appended to the end, or above the hovered item. Finally, the selected items are removed from the respective DragAndDropListView.

protected override void OnDragDrop(DragEventArgs drgevent)
{
 if(!m_allowReorder)
 {
  base.OnDragDrop(drgevent);
  return;
 }

 Point clientPoint = base.PointToClient(new Point(drgevent.X, drgevent.Y));
 ListViewItem hoverItem = base.GetItemAt(clientPoint.X, clientPoint.Y);

 if(!drgevent.Data.GetDataPresent(typeof(DragItemData).ToString()) || 
  ((DragItemData) 
   drgevent.Data.GetData(typeof(DragItemData).ToString())).ListView == null ||
  ((DragItemData) 
   drgevent.Data.GetData(typeof(DragItemData).ToSTring())).DragItems.Count == 0)
     return;
  
 DragItemData data = 
   (DragItemData) drgevent.Data.GetData(typeof(DragItemData).ToString());

 if(hoverItem == null)
 {
  for(int i=0; i<insertItems.Count; i++)
  {
   ListViewItem newItem = (ListViewItem) insertItems[i];
   base.Items.Add(newItem);
  }
 }
 else
 {
  int hoverIndex = hoverItem.Index;

  if(this == data.ListView)
  {
   if(hoverIndex > base.SelectedItems[0].Index)
    hoverIndex++;
  }

  for(int i=data.DragItems.Count - 1; i >= 0; i--)
  {
   ListViewItem newItem = (ListViewItem) data.DragItems[i];
   base.Items.Insert(hoverIndex, newItem);
  }
 }

 if(data.ListView != null)
 {
  foreach(ListViewItem itemToRemove in data.ListView.SelectedItems)
  {
   data.ListView.Items.Remove(itemToRemove);
  }
 }


 if(m_previousItem != null)
 {
  m_previousItem = null;
 }

 this.Invalidate();

 base.OnDragDrop (drgevent);

}

Known Problems

  • I've fixed all known problems so far.

Version History

  • May 17, 2004
    • Fixed inability to have lines shown for Large or Small Icon listings.
    • Fixed ability to fully move ListViewItems without losing data.
    • Removed hack to find the corresponding DragAndDropListView control.
  • May 14, 2004
    • Fixed ListView not scrolling.
    • Added AllowReorder to allow turning row reordering and row transfer off.
    • Added LineColor to allow you to set the color of the line drawn.
  • May 13, 2004
    • Initial launch showing functionality.

References

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Matt Hawley

United States United States
No Biography provided

Comments and Discussions

 
QuestionLicensing PinmemberMember 104455314-Dec-13 12:15 
QuestionMy vote is 5 Pinmemberpige28-Jun-13 1:36 
GeneralMy vote of 5 PinmemberPaul E. Bible27-Mar-13 4:23 
QuestionHow about Double Click support Pinmemberrewrewewrerw21-May-12 12:14 
BugJUMPY when doing drap drop Pinmemberrewrewewrerw21-May-12 12:12 
GeneralGet Drop Data PinmemberLaserson1-Oct-10 20:49 
GeneralLiscensing request Pinmembernickfas8-Jun-10 14:01 
GeneralRetaining tag or name property of item after drag-drop reorder Pinmemberhoncho2612-Jun-08 9:41 
GeneralFOUND THE ANSWER! Re: Retaining tag or name property of item after drag-drop reorder Pinmemberhoncho2612-Jun-08 10:45 
General"Jumping" at the bottom on drag and drop [FIX] - dragging down 2 nodes instead of one [FIX] Pinmemberpaintor11-Feb-08 1:37 
QuestionProblem with Sorting [modified] Pinmembershashank_15610-Feb-08 23:31 
GeneralBug Fix - When reordering down and horizontal scroll bar is visible PinmemberSteve Marshall29-Oct-07 6:25 
GeneralVB Code Pinmemberpowermetal1-Oct-07 8:19 
GeneralBug Fix for LargeIcon or SmallIcon view Pinmemberhuanyi25-Sep-07 3:02 
It works very well but in LargeIcon or SmallIcon
I do some fix in DragAndDropListView.cs as below
 
using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.ComponentModel;
 
namespace DragNDrop
{
public class DragAndDropListView : ListView
{
#region Private Members
 
private ListViewItem m_previousItem;
private bool m_allowReorder;
private Color m_lineColor;
 
#endregion
 
#region Public Properties
 
[Category("Behavior")]
public bool AllowReorder
{
get { return m_allowReorder; }
set { m_allowReorder = value; }
}
 
[Category("Appearance")]
public Color LineColor
{
get { return m_lineColor; }
set { m_lineColor = value; }
}
 
#endregion
 
#region Protected and Public Methods
 
public DragAndDropListView()
: base()
{
m_allowReorder = true;
m_lineColor = Color.Black;
}
 
protected override void OnDragDrop(DragEventArgs drgevent)
{
if (!m_allowReorder)
{
base.OnDragDrop(drgevent);
return;
}
 
// get the currently hovered row that the items will be dragged to
Point clientPoint = base.PointToClient(new Point(drgevent.X, drgevent.Y));
ListViewItem hoverItem = base.GetItemAt(clientPoint.X, clientPoint.Y);
 
if (!drgevent.Data.GetDataPresent(typeof(DragItemData).ToString()) || ((DragItemData)drgevent.Data.GetData(typeof(DragItemData).ToString())).ListView == null || ((DragItemData)drgevent.Data.GetData(typeof(DragItemData).ToString())).DragItems.Count == 0)
return;
 
// retrieve the drag item data
DragItemData data = (DragItemData)drgevent.Data.GetData(typeof(DragItemData).ToString());
 
if (hoverItem == null)
{
// the user does not wish to re-order the items, just append to the end
for (int i = 0; i < data.DragItems.Count; i++)
{
ListViewItem newItem = (ListViewItem)data.DragItems[i];
base.Items.Add(newItem);
}
}
else
{
// the user wishes to re-order the items
 
// get the index of the hover item
int hoverIndex = hoverItem.Index;
 
// determine if the items to be dropped are from
// this list view. If they are, perform a hack
// to increment the hover index so that the items
// get moved properly.
if (this == data.ListView)
{
if (base.SelectedItems[0].Index <= hoverIndex && hoverIndex <= base.SelectedItems[base.SelectedItems.Count - 1].Index + 1)
{
if (m_previousItem != null)
{
m_previousItem = null;
}
 
this.Invalidate();
 
// call the base on drag drop to raise the event
base.OnDragDrop(drgevent);
return;
}
//else if (hoverIndex > base.SelectedItems[0].Index)
// hoverIndex += base.SelectedItems.Count;
}
 
// insert the new items into the list view
// by inserting the items reversely from the array list
for (int i = data.DragItems.Count - 1; i >= 0; i--)
{
ListViewItem newItem = (ListViewItem)data.DragItems[i];
base.Items.Insert(hoverIndex, newItem);
base.Items[hoverIndex].Position = base.Items[hoverIndex + 1].Position;
}
}
 
// remove all the selected items from the previous list view
// if the list view was found
if (data.ListView != null)
{
foreach (ListViewItem itemToRemove in data.ListView.SelectedItems)
{
data.ListView.Items.Remove(itemToRemove);
}
}
//this.ArrangeIcons();
//this.
//this.Sorting = SortOrder.Ascending;
//this.Sort();
 
//base.AutoArrange = true;
//base.Refresh();
//this.ArrangeIcons();
this.Refresh();
//this.ArrangeIcons();
//this.RedrawItems(0, base.Items.Count - 1, false);
// set the back color of the previous item, then nullify it
if (m_previousItem != null)
{
m_previousItem = null;
}
 
this.Invalidate();
 
// call the base on drag drop to raise the event
base.OnDragDrop(drgevent);
}
 
protected override void OnDragOver(DragEventArgs drgevent)
{
if (!m_allowReorder)
{
base.OnDragOver(drgevent);
return;
}
 
if (!drgevent.Data.GetDataPresent(typeof(DragItemData).ToString()))
{
// the item(s) being dragged do not have any data associated
drgevent.Effect = DragDropEffects.None;
return;
}
 
if (base.Items.Count > 0)
{
// get the currently hovered row that the items will be dragged to
Point clientPoint = base.PointToClient(new Point(drgevent.X, drgevent.Y));
ListViewItem lastItem = base.Items[base.Items.Count - 1];
Point ptLastItem = new Point(lastItem.Bounds.X + lastItem.Bounds.Width + lastItem.Bounds.X, lastItem.Bounds.Y + lastItem.Bounds.Height);
ListViewItem hoverItem = base.GetItemAt(clientPoint.X, clientPoint.Y);
 
Graphics g = this.CreateGraphics();
 
if (hoverItem == null && (clientPoint.X > ptLastItem.X || clientPoint.Y > ptLastItem.Y))
{
//MessageBox.Show(base.GetChildAtPoint(new Point(clientPoint.X, clientPoint.Y)).GetType().ToString());
 
// no item was found, so no drop should take place
drgevent.Effect = DragDropEffects.Move;
 
if (m_previousItem != null)
{
m_previousItem = null;
Invalidate();
}
 
hoverItem = base.Items[base.Items.Count - 1];
 
if (this.View == View.Details || this.View == View.List)
{
g.DrawLine(new Pen(m_lineColor, 2), new Point(hoverItem.Bounds.X, hoverItem.Bounds.Y + hoverItem.Bounds.Height), new Point(hoverItem.Bounds.X + this.Bounds.Width, hoverItem.Bounds.Y + hoverItem.Bounds.Height));
//g.FillPolygon(new SolidBrush(m_lineColor), new Point[] {new Point(hoverItem.Bounds.X, hoverItem.Bounds.Y + hoverItem.Bounds.Height - 5), new Point(hoverItem.Bounds.X + 5, hoverItem.Bounds.Y + hoverItem.Bounds.Height), new Point(hoverItem.Bounds.X, hoverItem.Bounds.Y + hoverItem.Bounds.Height + 5)});
//g.FillPolygon(new SolidBrush(m_lineColor), new Point[] {new Point(this.Bounds.Width - 4, hoverItem.Bounds.Y + hoverItem.Bounds.Height - 5), new Point(this.Bounds.Width - 9, hoverItem.Bounds.Y + hoverItem.Bounds.Height), new Point(this.Bounds.Width - 4, hoverItem.Bounds.Y + hoverItem.Bounds.Height + 5)});
}
else
{
g.DrawLine(new Pen(m_lineColor, 2), new Point(hoverItem.Bounds.X + hoverItem.Bounds.Width, hoverItem.Bounds.Y), new Point(hoverItem.Bounds.X + hoverItem.Bounds.Width, hoverItem.Bounds.Y + hoverItem.Bounds.Height));
g.FillPolygon(new SolidBrush(m_lineColor), new Point[] { new Point(hoverItem.Bounds.X + hoverItem.Bounds.Width - 3, hoverItem.Bounds.Y), new Point(hoverItem.Bounds.X + hoverItem.Bounds.Width + 3, hoverItem.Bounds.Y), new Point(hoverItem.Bounds.X + hoverItem.Bounds.Width, hoverItem.Bounds.Y + 3) });
g.FillPolygon(new SolidBrush(m_lineColor), new Point[] { new Point(hoverItem.Bounds.X + hoverItem.Bounds.Width - 4, hoverItem.Bounds.Y + hoverItem.Bounds.Height), new Point(hoverItem.Bounds.X + hoverItem.Bounds.Width + 4, hoverItem.Bounds.Y + hoverItem.Bounds.Height), new Point(hoverItem.Bounds.X + hoverItem.Bounds.Width, hoverItem.Bounds.Y + hoverItem.Bounds.Height - 4) });
}
 
// call the base OnDragOver event
base.OnDragOver(drgevent);
 
return;
}
 
// determine if the user is currently hovering over a new
// item. If so, set the previous item's back color back
// to the default color.
if ((m_previousItem != null && m_previousItem != hoverItem) || m_previousItem == null)
{
this.Invalidate();
}
 
// set the background color of the item being hovered
// and assign the previous item to the item being hovered
//hoverItem.BackColor = Color.Beige;
m_previousItem = hoverItem;
 
if (this.View == View.Details || this.View == View.List)
{
g.DrawLine(new Pen(m_lineColor, 2), new Point(hoverItem.Bounds.X, hoverItem.Bounds.Y/* + hoverItem.Bounds.Height*/), new Point(hoverItem.Bounds.X + this.Bounds.Width, hoverItem.Bounds.Y /*+ 2 *hoverItem.Bounds.Height*/));
//g.FillPolygon(new SolidBrush(m_lineColor), new Point[] { new Point(hoverItem.Bounds.X, hoverItem.Bounds.Y - 3 + hoverItem.Bounds.Height), new Point(hoverItem.Bounds.X + 3, hoverItem.Bounds.Y + hoverItem.Bounds.Height), new Point(hoverItem.Bounds.X, hoverItem.Bounds.Y + 3 + hoverItem.Bounds.Height) });
//g.FillPolygon(new SolidBrush(m_lineColor), new Point[] { new Point(this.Bounds.Width - 4, hoverItem.Bounds.Y - 4 + hoverItem.Bounds.Height), new Point(this.Bounds.Width - 9, hoverItem.Bounds.Y + hoverItem.Bounds.Height -1 ), new Point(this.Bounds.Width - 4, hoverItem.Bounds.Y + 3 + hoverItem.Bounds.Height -1) });
}
else
{
g.DrawLine(new Pen(m_lineColor, 2), new Point(hoverItem.Bounds.X, hoverItem.Bounds.Y), new Point(hoverItem.Bounds.X, hoverItem.Bounds.Y + hoverItem.Bounds.Height));
g.FillPolygon(new SolidBrush(m_lineColor), new Point[] { new Point(hoverItem.Bounds.X - 3, hoverItem.Bounds.Y), new Point(hoverItem.Bounds.X + 3, hoverItem.Bounds.Y), new Point(hoverItem.Bounds.X, hoverItem.Bounds.Y + 3) });
g.FillPolygon(new SolidBrush(m_lineColor), new Point[] { new Point(hoverItem.Bounds.X - 4, hoverItem.Bounds.Y + hoverItem.Bounds.Height), new Point(hoverItem.Bounds.X + 4, hoverItem.Bounds.Y + hoverItem.Bounds.Height), new Point(hoverItem.Bounds.X, hoverItem.Bounds.Y + hoverItem.Bounds.Height - 4) });
}
 
// go through each of the selected items, and if any of the
// selected items have the same index as the item being
// hovered, disable dropping.
foreach (ListViewItem itemToMove in base.SelectedItems)
{
if (itemToMove.Index == hoverItem.Index)
{
drgevent.Effect = DragDropEffects.None;
hoverItem.EnsureVisible();
return;
}
}
 
// ensure that the hover item is visible
hoverItem.EnsureVisible();
}
 
// everything is fine, allow the user to move the items
drgevent.Effect = DragDropEffects.Move;
 
// call the base OnDragOver event
base.OnDragOver(drgevent);
}
 
protected override void OnDragEnter(DragEventArgs drgevent)
{
if (!m_allowReorder)
{
base.OnDragEnter(drgevent);
return;
}
 
if (!drgevent.Data.GetDataPresent(typeof(DragItemData).ToString()))
{
// the item(s) being dragged do not have any data associated
drgevent.Effect = DragDropEffects.None;
return;
}
 
// everything is fine, allow the user to move the items
drgevent.Effect = DragDropEffects.Move;
 
// call the base OnDragEnter event
base.OnDragEnter(drgevent);
}
 
protected override void OnItemDrag(ItemDragEventArgs e)
{
if (!m_allowReorder)
{
base.OnItemDrag(e);
return;
}
 
// call the DoDragDrop method
base.DoDragDrop(GetDataForDragDrop(), DragDropEffects.Move);
 
// call the base OnItemDrag event
base.OnItemDrag(e);
}
 
protected override void OnLostFocus(EventArgs e)
{
// reset the selected items background and remove the previous item
ResetOutOfRange();
 
Invalidate();
 
// call the OnLostFocus event
base.OnLostFocus(e);
}
 
protected override void OnDragLeave(EventArgs e)
{
// reset the selected items background and remove the previous item
ResetOutOfRange();
 
Invalidate();
 
// call the base OnDragLeave event
base.OnDragLeave(e);
}
 
#endregion
 
#region Private Methods
 
private DragItemData GetDataForDragDrop()
{
// create a drag item data object that will be used to pass along with the drag and drop
DragItemData data = new DragItemData(this);
 
// go through each of the selected items and
// add them to the drag items collection
// by creating a clone of the list item
foreach (ListViewItem item in this.SelectedItems)
{
data.DragItems.Add(item.Clone());
}
 
return data;
}
 
private void ResetOutOfRange()
{
// determine if the previous item exists,
// if it does, reset the background and release
// the previous item
if (m_previousItem != null)
{
m_previousItem = null;
}
 
}
 
#endregion
 
#region DragItemData Class
 
private class DragItemData
{
#region Private Members
 
private DragAndDropListView m_listView;
private ArrayList m_dragItems;
 
#endregion
 
#region Public Properties
 
public DragAndDropListView ListView
{
get { return m_listView; }
}
 
public ArrayList DragItems
{
get { return m_dragItems; }
}
 
#endregion
 
#region Public Methods and Implementation
 
public DragItemData(DragAndDropListView listView)
{
m_listView = listView;
m_dragItems = new ArrayList();
}
 
#endregion
}
 
#endregion
 
private void InitializeComponent()
{
this.SuspendLayout();
this.ResumeLayout(false);
 
}
}
}

GeneralRe: Bug Fix for LargeIcon or SmallIcon view Pinmembernullesc19-Nov-08 17:24 
GeneralDrag&Drop only inside of current List PinmemberMichaZ1238-Aug-07 0:30 
GeneralQuestion Pinmemberloneferret17-Jul-06 12:02 
QuestionLicensing Pinmembermark_ledwich20-Apr-06 19:44 
QuestionView LargeIcon ? Pinmembermiha527-Nov-05 22:59 
QuestionFlicker when dragging Pinsussdgannon3416-Sep-05 18:16 
Questionhow to implement drag and drop in the asp.net page ,like list data or multiple select from one to another PinmemberJade_king15-Sep-05 22:13 
GeneralListViewItem Image problem Pinmemberdidis1-Dec-04 5:24 
GeneralRe: ListViewItem Image problem PinmemberDiego F.3-Apr-07 4:30 
GeneralAllow both items drag &amp; drop and files drag &amp; drop PinsussBertrand Dunogier3-Oct-04 2:35 
GeneralRe: Allow both items drag &amp; drop and files drag &amp; drop PinmemberCraig Nagy1-Mar-05 18:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.141015.1 | Last Updated 13 May 2004
Article Copyright 2004 by Matt Hawley
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid