Csharp/CSharp Tutorial/GUI Windows Forms/Drag Drop

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Drag and Drop TextBox

<source lang="csharp">using System; using System.Windows.Forms; public class TextBoxDragDropDemo : Form {

   public TextBoxDragDropDemo()
   {
       InitializeComponent();
   }
   private void TextBox_MouseDown(object sender, MouseEventArgs e)
   {
       TextBox txt = (TextBox)sender;
       txt.SelectAll();
       txt.DoDragDrop(txt.Text, DragDropEffects.Copy);
   }
   private void TextBox_DragEnter(object sender, DragEventArgs e)
   {
       if (e.Data.GetDataPresent(DataFormats.Text))
       {
           e.Effect = DragDropEffects.Copy;
       }
       else
       {
           e.Effect = DragDropEffects.None;
       }
   }
   private void TextBox_DragDrop(object sender, DragEventArgs e)
   {
       TextBox txt = (TextBox)sender;
       txt.Text = (string)e.Data.GetData(DataFormats.Text);
   }
   [STAThread]
   public static void Main(string[] args)
   {
       Application.Run(new TextBoxDragDropDemo());
   }
   private System.Windows.Forms.TextBox TextBox2;
   private System.Windows.Forms.TextBox TextBox1;
   private void InitializeComponent()
   {
       this.TextBox2 = new System.Windows.Forms.TextBox();
       this.TextBox1 = new System.Windows.Forms.TextBox();
       this.SuspendLayout();
       this.TextBox2.AllowDrop = true;
       this.TextBox2.Location = new System.Drawing.Point(28, 129);
       this.TextBox2.Multiline = true;
       this.TextBox2.Size = new System.Drawing.Size(196, 77);
       this.TextBox2.DragDrop += new System.Windows.Forms.DragEventHandler(this.TextBox_DragDrop);
       this.TextBox2.DragEnter += new System.Windows.Forms.DragEventHandler(this.TextBox_DragEnter);
       this.TextBox2.MouseDown += new System.Windows.Forms.MouseEventHandler(this.TextBox_MouseDown);
       this.TextBox1.AllowDrop = true;
       this.TextBox1.Location = new System.Drawing.Point(28, 36);
       this.TextBox1.Multiline = true;
       this.TextBox1.Size = new System.Drawing.Size(196, 77);
       this.TextBox1.DragDrop += new System.Windows.Forms.DragEventHandler(this.TextBox_DragDrop);
       this.TextBox1.DragEnter += new System.Windows.Forms.DragEventHandler(this.TextBox_DragEnter);
       this.TextBox1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.TextBox_MouseDown);
       this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
       this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
       this.ClientSize = new System.Drawing.Size(292, 266);
       this.Controls.Add(this.TextBox2);
       this.Controls.Add(this.TextBox1);
       this.ResumeLayout(false);
       this.PerformLayout();
   }

}</source>

Drag Drop Sample

<source lang="csharp">using System; using System.Drawing; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data;

 public class DragDropForm : System.Windows.Forms.Form
 {
   private System.Windows.Forms.ListBox lbDragDropSource;
   private System.Windows.Forms.Splitter splitterCentral;
   private System.Windows.Forms.TextBox txtMain;
   public DragDropForm()
   {
     InitializeComponent();
     this.lbDragDropSource.Items.Add("<html>");
     this.lbDragDropSource.Items.Add("<head>");
     this.lbDragDropSource.Items.Add("<title>");
     this.lbDragDropSource.Items.Add("</title>");
     this.lbDragDropSource.Items.Add("</head>");
     this.lbDragDropSource.Items.Add("<body>");
     this.lbDragDropSource.Items.Add("</body>");
     this.lbDragDropSource.Items.Add("</html>");
   }
   private void InitializeComponent()
   {
     this.lbDragDropSource = new System.Windows.Forms.ListBox();
     this.splitterCentral = new System.Windows.Forms.Splitter();
     this.txtMain = new System.Windows.Forms.TextBox();
     this.SuspendLayout();
     // 
     // lbDragDropSource
     // 
     this.lbDragDropSource.Dock = System.Windows.Forms.DockStyle.Left;
     this.lbDragDropSource.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
     this.lbDragDropSource.IntegralHeight = false;
     this.lbDragDropSource.ItemHeight = 20;
     this.lbDragDropSource.Name = "lbDragDropSource";
     this.lbDragDropSource.Size = new System.Drawing.Size(152, 301);
     this.lbDragDropSource.TabIndex = 0;
     this.lbDragDropSource.MouseDown += new System.Windows.Forms.MouseEventHandler(this.lbDragDropSource_MouseDown);
     this.lbDragDropSource.QueryContinueDrag += new System.Windows.Forms.QueryContinueDragEventHandler(this.lbDragDropSource_QueryContinueDrag);
     // 
     // splitterCentral
     // 
     this.splitterCentral.Location = new System.Drawing.Point(152, 0);
     this.splitterCentral.Name = "splitterCentral";
     this.splitterCentral.Size = new System.Drawing.Size(3, 301);
     this.splitterCentral.TabIndex = 1;
     this.splitterCentral.TabStop = false;
     // 
     // txtMain
     // 
     this.txtMain.AcceptsReturn = true;
     this.txtMain.AcceptsTab = true;
     this.txtMain.AllowDrop = true;
     this.txtMain.Dock = System.Windows.Forms.DockStyle.Fill;
     this.txtMain.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
     this.txtMain.Location = new System.Drawing.Point(155, 0);
     this.txtMain.Multiline = true;
     this.txtMain.Name = "txtMain";
     this.txtMain.Size = new System.Drawing.Size(333, 301);
     this.txtMain.TabIndex = 2;
     this.txtMain.Text = "";
     this.txtMain.DragOver += new System.Windows.Forms.DragEventHandler(this.txtMain_DragOver);
     this.txtMain.DragDrop += new System.Windows.Forms.DragEventHandler(this.txtMain_DragDrop);
     this.txtMain.TextChanged += new System.EventHandler(this.txtMain_TextChanged);
     // 
     // DragDropForm
     // 
     this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
     this.ClientSize = new System.Drawing.Size(488, 301);
     this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                     this.txtMain,
                                     this.splitterCentral,
                                     this.lbDragDropSource});
     this.Name = "DragDropForm";
     this.Text = "Drag and Drop Sample";
     this.ResumeLayout(false);
   }
   static void Main() 
   {
     Application.Run(new DragDropForm());
   }
   private void lbDragDropSource_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
   {
     int nSelectedIndex = lbDragDropSource.SelectedIndex;
     string strItem = (string)lbDragDropSource.Items[nSelectedIndex];
     DragDropEffects dde = lbDragDropSource.DoDragDrop(strItem,
       DragDropEffects.Copy);
     if (DragDropEffects.None == dde)
       MessageBox.Show("Our drag and drop offer was not accepted.");
   }
   private void txtMain_DragOver(object sender, System.Windows.Forms.DragEventArgs e)
   {
     if (e.Data.GetDataPresent(typeof(string)))
     {
       e.Effect = DragDropEffects.Copy;
     }
   }
   private void txtMain_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
   {
     if (e.Data.GetDataPresent(typeof(string)))
     {
       string strData = (string)e.Data.GetData(typeof(string));
       txtMain.AppendText(strData);
     }
   }
   private void lbDragDropSource_QueryContinueDrag(object sender, System.Windows.Forms.QueryContinueDragEventArgs e)
   {
     // DO NOT DO THIS
     // e.Action = DragAction.Continue;
   }
   private void txtMain_TextChanged(object sender, System.EventArgs e)
   {
   
   }
 }</source>

File Drop Target

<source lang="csharp">using System; using System.Drawing; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data; using System.IO;

 public class FileDropTargetForm : System.Windows.Forms.Form
 {
   private System.Windows.Forms.TextBox txtMain;
   public FileDropTargetForm()
   {
     this.txtMain = new System.Windows.Forms.TextBox();
     this.SuspendLayout();
     // 
     // txtMain
     // 
     this.txtMain.AcceptsReturn = true;
     this.txtMain.AcceptsTab = true;
     this.txtMain.AllowDrop = true;
     this.txtMain.Dock = System.Windows.Forms.DockStyle.Fill;
     this.txtMain.Multiline = true;
     this.txtMain.Name = "txtMain";
     this.txtMain.ScrollBars = System.Windows.Forms.ScrollBars.Both;
     this.txtMain.Size = new System.Drawing.Size(292, 273);
     this.txtMain.TabIndex = 0;
     this.txtMain.Text = "";
     this.txtMain.DragOver += new System.Windows.Forms.DragEventHandler(this.txtMain_DragOver);
     this.txtMain.DragDrop += new System.Windows.Forms.DragEventHandler(this.txtMain_DragDrop);
     this.txtMain.TextChanged += new System.EventHandler(this.txtMain_TextChanged);
     // 
     // FileDropTargetForm
     // 
     this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
     this.ClientSize = new System.Drawing.Size(292, 273);
     this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                     this.txtMain});
     this.Name = "FileDropTargetForm";
     this.Text = "FileDrop Target";
     this.ResumeLayout(false);
   }
   static void Main() 
   {
     Application.Run(new FileDropTargetForm());
   }
   private void txtMain_DragOver(object sender, System.Windows.Forms.DragEventArgs e)
   {
     if (e.Data.GetDataPresent(DataFormats.FileDrop))
     {
       e.Effect = DragDropEffects.Copy;
     }
   }
   private void txtMain_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
   {
     if (e.Data.GetDataPresent(DataFormats.FileDrop))
     {
       string[] strFiles = (string[])e.Data.GetData(DataFormats.FileDrop);
       StreamReader reader = new StreamReader(strFiles[0]);
       this.txtMain.Clear();
       this.txtMain.Text = reader.ReadToEnd();
       reader.Close();
     }
   }
   private void txtMain_TextChanged(object sender, System.EventArgs e)
   {
   
   }
 }</source>