Thursday, September 23, 2010

. NET drag and drop operation


In the application, through processing a series of events, such as DragEnter, DragLeave and DragDrop events to implement the application in the Windows drag and drop operation. Parameters of these events through the use of the available information, you can easily drag and drop. 
Drag and drop in the code is achieved through three steps, first start the drag and drop, drag the data control is needed to achieve MouseDown event response code, and calls DoDragDrop () method; followed by drag and drop effects, the target Add DragEnter event on the control response code, the use of mobile DragDropEffects enumerated type or copy and other drag effect; the last data operation is placed in the target control to add DragDrop response code, the data added to the target control. 
using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data;
namespace DragDrop ( / / /  / / / Form1 summary description. / / /  public class Form1: System.Windows.Forms.Form ( private System.Windows.Forms.ListBox listBox1; private System.Windows.Forms.ListBox listBox2; / / /  / / / Required designer variable. / / /  private System.ComponentModel.Container components = null;
public Form1 () ( / / / / Windows Form Designer support necessary / / InitializeComponent ();
/ / / / TODO: add in the InitializeComponent call any constructor code after / / )
/ / /  / / / Clean up any resources being used. / / /  protected override void Dispose (bool disposing) ( if (disposing) ( if (components! = null) ( components.Dispose (); ) ) base.Dispose (disposing); )
# Region Windows Form Designer generated code / / /  / / / Required method for Designer support - do not use the code editor to modify / / / The contents of this method. / / /  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
/ / /  / / / Application main entry point. / / /  [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")); ) ) )

No comments:

Post a Comment