Dragging and dropping between ListView
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
public class Form1 : System.Windows.Forms.Form {
private System.Windows.Forms.ListView listView1= new System.Windows.Forms.ListView();
private System.Windows.Forms.ListView listView2= new System.Windows.Forms.ListView();
private System.ComponentModel.Container components = null;
System.Windows.Forms.ListViewItem listViewItem1 = new System.Windows.Forms.ListViewItem("List 2 Item 1");
System.Windows.Forms.ListViewItem listViewItem2 = new System.Windows.Forms.ListViewItem("List 2 Item 2");
System.Windows.Forms.ListViewItem listViewItem3 = new System.Windows.Forms.ListViewItem("List 2 Item 3");
System.Windows.Forms.ListViewItem listViewItem4 = new System.Windows.Forms.ListViewItem("Item 1");
System.Windows.Forms.ListViewItem listViewItem5 = new System.Windows.Forms.ListViewItem("Item 2");
System.Windows.Forms.ListViewItem listViewItem6 = new System.Windows.Forms.ListViewItem("Item 3");
public Form1() {
this.SuspendLayout();
this.listView2.AllowDrop = true;
listViewItem1.UseItemStyleForSubItems = false;
listViewItem2.UseItemStyleForSubItems = false;
listViewItem3.UseItemStyleForSubItems = false;
this.listView2.Items.AddRange(new System.Windows.Forms.ListViewItem[]{
listViewItem1,listViewItem2,listViewItem3});
this.listView2.Location = new System.Drawing.Point(320, 24);
this.listView2.Size = new System.Drawing.Size(200, 216);
this.listView2.View = System.Windows.Forms.View.List;
this.listView2.DragDrop += new System.Windows.Forms.DragEventHandler(this.OnDragDrop);
this.listView2.DragEnter += new System.Windows.Forms.DragEventHandler(this.OnDragEnter);
this.listView1.AllowDrop = true;
listViewItem4.UseItemStyleForSubItems = false;
listViewItem5.UseItemStyleForSubItems = false;
listViewItem6.UseItemStyleForSubItems = false;
this.listView1.Items.AddRange(new System.Windows.Forms.ListViewItem[] { listViewItem4, listViewItem5, listViewItem6 });
this.listView1.Location = new System.Drawing.Point(40, 24);
this.listView1.Size = new System.Drawing.Size(200, 216);
this.listView1.View = System.Windows.Forms.View.List;
this.listView1.ItemDrag += new System.Windows.Forms.ItemDragEventHandler(this.OnItemDrag);
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(576, 273);
this.Controls.AddRange(new System.Windows.Forms.Control[] { this.listView2, this.listView1 });
this.ResumeLayout(false);
}
[STAThread]
static void Main() {
Application.Run(new Form1());
}
private void OnItemDrag(object sender, System.Windows.Forms.ItemDragEventArgs e) {
string s = e.Item.ToString();
DoDragDrop(s, DragDropEffects.Copy | DragDropEffects.Move);
}
private void OnDragEnter(object sender,
System.Windows.Forms.DragEventArgs e) {
if (e.Data.GetDataPresent(DataFormats.Text))
e.Effect = DragDropEffects.Copy;
else
e.Effect = DragDropEffects.None;
}
private void OnDragDrop(object sender, System.Windows.Forms.DragEventArgs e) {
string typestring = "Type";
string s = e.Data.GetData(typestring.GetType()).ToString();
string orig_string = s;
s = s.Substring(s.IndexOf(":") + 1).Trim();
s = s.Substring(1, s.Length - 2);
this.listView2.Items.Add(s);
IEnumerator enumerator = listView1.Items.GetEnumerator();
int whichIdx = -1;
int idx = 0;
while (enumerator.MoveNext()) {
string s2 = enumerator.Current.ToString();
if (s2.Equals(orig_string)) {
whichIdx = idx;
break;
}
idx++;
}
this.listView1.Items.RemoveAt(whichIdx);
}
}
Related examples in the same category