// Form load event or a similar place
private void Form_Load(object sender, EventArgs e)
{
// Enable drag and drop for this form
// (this can also be applied to any controls)
this.AllowDrop = true;
// Add event handlers for the drag & drop functionality
this.DragEnter += new DragEventHandler(Form_DragEnter);
this.DragDrop += new DragEventHandler(Form_DragDrop);
}
// This event occurs when the user drags over the form with
// the mouse during a drag drop operation
void Form_DragEnter(object sender, DragEventArgs e)
{
// Check if the Dataformat of the data can be accepted
// (we only accept file drops from Explorer, etc.)
if (e.Data.GetDataPresent(DataFormats.FileDrop))
e.Effect = DragDropEffects.Copy; // Okay
else
e.Effect = DragDropEffects.None; // Unknown data, ignore it
}
// Occurs when the user releases the mouse over the drop target
void Form_DragDrop(object sender, DragEventArgs e)
{
// Extract the data from the DataObject-Container into a string list
string[] FileList = (string[])e.Data.GetData(DataFormats.FileDrop, false);
// Do something with the data...
// For example add all files into a simple label control:
foreach (string File in FileList)
this.label.Text += File + "\n";
}
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();
}
}
ASKER
DragDropEffects e = DragDrop.DoDragDrop(itemsCit means the item is Ready for Drag and Drop, Drop either to Copy the item or Moveontrol, dObject, DragDropEffects.Copy | DragDropEffects.Move);
Here's a pseudo code,
((e & DragDropEffects.Move) != 0) is used
and not justBecause, there is no point on that, it does not know if the item when Drop either to Move or Copy, So you must specify the Drag Effect.
(e != 0)
ASKER
ASKER
The .NET Framework is not specific to any one programming language; rather, it includes a library of functions that allows developers to rapidly build applications. Several supported languages include C#, VB.NET, C++ or ASP.NET.
TRUSTED BY
eg. If e is 3 (11 in binary) and the DrapDropEffects.Move is 4 (100 in binary) then the check used is false but e is not zero.