In an application, you implement a drag-and-drop operation in a Windows application by handling a series of events, such as Dragenter,dragleave and DragDrop events. You can easily implement drag-and-drop operations by using the information available in these event parameters.
The drag-and-drop operation is implemented in code in three steps, starting with a drag-and-drop operation, implementing the MouseDown event response code on a control that needs to be dragged, and invoking the DoDragDrop () method, followed by a drag-and-drop effect, adding the DragEnter event response code on the target control. Use the DragDropEffects enumeration type to implement drag effects such as move or copy, and finally place data operations, add DragDrop response code to the target control, and add the data to the target control.
Using System;
Using System.Drawing;
Using System.Collections;
Using System.ComponentModel;
Using System.Windows.Forms;
Using System.Data;
Namespace DragDrop
{
<summary>
Summary description of the Form1.
</summary>
public class Form1:System.Windows.Forms.Form
{
Private System.Windows.Forms.ListBox ListBox1;
Private System.Windows.Forms.ListBox ListBox2;
<summary>
The required designer variable.
</summary>
Private System.ComponentModel.Container components = null;
Public Form1 ()
{
//
Required for Windows Forms Designer support
//
InitializeComponent ();
//
TODO: Add any constructor code after the InitializeComponent call
//
}
<summary>
Clean up all resources that are in use.
</summary>
protected override void Dispose (bool disposing)
{
if (disposing)
{
if (Components!= null)
{
Components. Dispose ();
}
}
Base. Dispose (disposing);
}
Code generated #region the Windows forms Designer
<summary>
Designer supports required methods-do not use the Code editor to modify
The contents of this method.
</summary>
private void InitializeComponent ()
{
This.listbox1 = new System.Windows.Forms.ListBox ();
This.listbox2 = new System.Windows.Forms.ListBox ();
This. SuspendLayout ();
//
ListBox1
//
This.listBox1.ItemHeight = 12;
This.listBox1.Location = new System.Drawing.Point (32, 24);
This.listBox1.Name = "ListBox1";
This.listBox1.Size = new System.Drawing.Size (120, 280);
This.listBox1.TabIndex = 0;
This.listBox1.MouseDown + = new System.Windows.Forms.MouseEventHandler (This.listbox1_mousedown);
//
ListBox2
//
This.listBox2.ItemHeight = 12;
This.listBox2.Location = new System.Drawing.Point (248, 24);
This.listBox2.Name = "ListBox2";
This.listBox2.Size = new System.Drawing.Size (120, 280);
This.listBox2.TabIndex = 0;
This.listBox2.DragDrop + = new System.Windows.Forms.DragEventHandler (This.listbox2_dragdrop);
This.listBox2.DragEnter + = new System.Windows.Forms.DragEventHandler (this.listbox2_dragenter);
//
Form1
//
This. AutoScaleBaseSize = new System.Drawing.Size (6, 14);
This. ClientSize = new System.Drawing.Size (408, 333);
This. Controls.Add (This.listbox1);
This. Controls.Add (THIS.LISTBOX2);
This. Name = "Form1";
This. Text = "Form1";
This. Load + = new System.EventHandler (this. Form1_Load);
This. ResumeLayout (FALSE);
}
#endregion
<summary>
The main entry point for the application.
</summary>
[STAThread]
static void Main ()
{
Application.Run (New Form1 ());
}
private void Form1_Load (object sender, System.EventArgs e)
{
This.listBox1.AllowDrop = true;
This.listBox2.AllowDrop = true;
THIS.LISTBOX1.ITEMS.ADD ("a");
THIS.LISTBOX1.ITEMS.ADD ("B");
THIS.LISTBOX1.ITEMS.ADD ("C");
}
private void Listbox1_mousedown (object sender, System.Windows.Forms.MouseEventArgs e)
{
This.listBox1.DoDragDrop (This.listbox1.items[this.listbox1.selectedindex],dragdropeffects.move);
}
private void Listbox2_dragenter (object sender, System.Windows.Forms.DragEventArgs e)
{
if (E.data.getdatapresent ("Text"))
{
E.effect = DragDropEffects.Move;
}
}
private void Listbox2_dragdrop (object sender, System.Windows.Forms.DragEventArgs e)
{
THIS.LISTBOX2.ITEMS.ADD (e.Data.GetData ("Text"));
This.listBox1.Items.Remove (e.Data.GetData ("Text"));
}
}
}