ListView 拖拽实例
实例内容:
ListView之间单项数据拖拽;
ListView之间多项数据拖拽;
ListView之间多项数据双向拖拽;
ListView内部位置移动;
ListView内部值的交换。
部分代码:
#region ListView之间单项数据拖拽
private void listView4_ItemDrag(object sender, ItemDragEventArgs e)
{
listView4.DoDragDrop(e.Item, DragDropEffects.Move);
}
private void listView5_DragEnter(object sender, DragEventArgs e)
{
e.Effect = e.AllowedEffect;
}
private void listView5_DragDrop(object sender, DragEventArgs e)
{
ListViewItem draggedItem = (ListViewItem)e.Data.GetData(typeof(ListViewItem));
listView5.Items.Insert(0,(ListViewItem)draggedItem.Clone());
}
#endregion
private void listView4_ItemDrag(object sender, ItemDragEventArgs e)
{
listView4.DoDragDrop(e.Item, DragDropEffects.Move);
}
private void listView5_DragEnter(object sender, DragEventArgs e)
{
e.Effect = e.AllowedEffect;
}
private void listView5_DragDrop(object sender, DragEventArgs e)
{
ListViewItem draggedItem = (ListViewItem)e.Data.GetData(typeof(ListViewItem));
listView5.Items.Insert(0,(ListViewItem)draggedItem.Clone());
}
#endregion
#region ListView之间多项数据拖拽
private void listView2_ItemDrag(object sender, ItemDragEventArgs e)
{
ListViewItem[] items = new ListViewItem[((ListView)(sender)).SelectedItems.Count];
int i = 0;
foreach (ListViewItem item in ((ListView)(sender)).SelectedItems)
{
items[i] = item;
i++;
}
((ListView)(sender)).DoDragDrop(new DataObject("System.Windows.Forms.ListViewItem()", items), DragDropEffects.Copy);
}
private void listView3_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent("System.Windows.Forms.ListViewItem()"))
{
e.Effect = DragDropEffects.Copy;
}
else
{
e.Effect = DragDropEffects.None;
}
}
private void listView3_DragDrop(object sender, DragEventArgs e)
{
ListViewItem[] items = (ListViewItem[])(e.Data.GetData("System.Windows.Forms.ListViewItem()"));
int i = 0;
foreach (ListViewItem item in items)
{
ListViewItem listItem = new ListViewItem(items[i].Text,item.ImageIndex);
listItem.SubItems.Add(items[i].SubItems[1].Text);
listItem.SubItems.Add(items[i].SubItems[2].Text);
((ListView)(sender)).Items.Add(listItem);
try
{
if (sender == listView3)
{
listView2.Items.Remove(listView2.SelectedItems[0]);
}
}
catch (Exception exc)
{
MessageBox.Show(exc.Message);
}
i++;
}
}
#endregion
private void listView2_ItemDrag(object sender, ItemDragEventArgs e)
{
ListViewItem[] items = new ListViewItem[((ListView)(sender)).SelectedItems.Count];
int i = 0;
foreach (ListViewItem item in ((ListView)(sender)).SelectedItems)
{
items[i] = item;
i++;
}
((ListView)(sender)).DoDragDrop(new DataObject("System.Windows.Forms.ListViewItem()", items), DragDropEffects.Copy);
}
private void listView3_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent("System.Windows.Forms.ListViewItem()"))
{
e.Effect = DragDropEffects.Copy;
}
else
{
e.Effect = DragDropEffects.None;
}
}
private void listView3_DragDrop(object sender, DragEventArgs e)
{
ListViewItem[] items = (ListViewItem[])(e.Data.GetData("System.Windows.Forms.ListViewItem()"));
int i = 0;
foreach (ListViewItem item in items)
{
ListViewItem listItem = new ListViewItem(items[i].Text,item.ImageIndex);
listItem.SubItems.Add(items[i].SubItems[1].Text);
listItem.SubItems.Add(items[i].SubItems[2].Text);
((ListView)(sender)).Items.Add(listItem);
try
{
if (sender == listView3)
{
listView2.Items.Remove(listView2.SelectedItems[0]);
}
}
catch (Exception exc)
{
MessageBox.Show(exc.Message);
}
i++;
}
}
#endregion
#region ListView内部位置移动
private void listView1_ItemDrag(object sender, ItemDragEventArgs e)
{
listView1.DoDragDrop(e.Item, DragDropEffects.Move);
}
private void listView1_DragEnter(object sender, DragEventArgs e)
{
e.Effect = e.AllowedEffect;
}
private void listView1_DragOver(object sender, DragEventArgs e)
{
Point targetPoint = listView1.PointToClient(new Point(e.X, e.Y));
int targetIndex = listView1.InsertionMark.NearestIndex(targetPoint);
if (targetIndex>-1)
{
Rectangle itemBounds = listView1.GetItemRect(targetIndex);
if (targetPoint.X>itemBounds.Left+(itemBounds.Width/2))
{
listView1.InsertionMark.AppearsAfterItem = true;
}
else
{
listView1.InsertionMark.AppearsAfterItem = false;
}
}
listView1.InsertionMark.Index = targetIndex;
}
private void listView1_DragLeave(object sender, EventArgs e)
{
listView1.InsertionMark.Index = -1;
}
private void listView1_DragDrop(object sender, DragEventArgs e)
{
int targetIndex = listView1.InsertionMark.Index;
if (targetIndex==-1)
{
return;
}
if (listView1.InsertionMark.AppearsAfterItem)
{
targetIndex++;
}
ListViewItem draggedItme = (ListViewItem)e.Data.GetData(typeof(ListViewItem));
listView1.Items.Insert(targetIndex,(ListViewItem)draggedItme.Clone());
listView1.Items.Remove(draggedItme);
}
private class ListViewIndexComparer : System.Collections.IComparer
{
public int Compare(object x, object y)
{
return ((ListViewItem)x).Index - ((ListViewItem)y).Index;
}
}
#endregion
。。。。。。private void listView1_ItemDrag(object sender, ItemDragEventArgs e)
{
listView1.DoDragDrop(e.Item, DragDropEffects.Move);
}
private void listView1_DragEnter(object sender, DragEventArgs e)
{
e.Effect = e.AllowedEffect;
}
private void listView1_DragOver(object sender, DragEventArgs e)
{
Point targetPoint = listView1.PointToClient(new Point(e.X, e.Y));
int targetIndex = listView1.InsertionMark.NearestIndex(targetPoint);
if (targetIndex>-1)
{
Rectangle itemBounds = listView1.GetItemRect(targetIndex);
if (targetPoint.X>itemBounds.Left+(itemBounds.Width/2))
{
listView1.InsertionMark.AppearsAfterItem = true;
}
else
{
listView1.InsertionMark.AppearsAfterItem = false;
}
}
listView1.InsertionMark.Index = targetIndex;
}
private void listView1_DragLeave(object sender, EventArgs e)
{
listView1.InsertionMark.Index = -1;
}
private void listView1_DragDrop(object sender, DragEventArgs e)
{
int targetIndex = listView1.InsertionMark.Index;
if (targetIndex==-1)
{
return;
}
if (listView1.InsertionMark.AppearsAfterItem)
{
targetIndex++;
}
ListViewItem draggedItme = (ListViewItem)e.Data.GetData(typeof(ListViewItem));
listView1.Items.Insert(targetIndex,(ListViewItem)draggedItme.Clone());
listView1.Items.Remove(draggedItme);
}
private class ListViewIndexComparer : System.Collections.IComparer
{
public int Compare(object x, object y)
{
return ((ListViewItem)x).Index - ((ListViewItem)y).Index;
}
}
#endregion
下载:https://files.cnblogs.com/EasyData/ListViewDragTest.rar