Simple drag and drop example
This example shows how to implement a basic drag and drop functionality.
This is useful if you want to drag files directly from the Windows explorer
(or any related software) into your application.
// 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"; }
Author:
Jonas John
License:
Public domain
Language:
C#
Created:
07/20/2007
Updated:
07/21/2007
Tags:
windows forms, gui, example
Related links:
Sorry folks, comments have been deactivated for now due to the large amount of spam.
Please try to post your questions or problems on a related programming board, a suitable mailing list, a programming chat-room,
or use a QA website like stackoverflow because I'm usually too busy to answer any mails related
to my code snippets. Therefore please just mail me if you found a serious bug... Thank you!
Older comments:
string[] FileList = (string[])e.Data.GetData(DataFormats.FileDrop, false);
foreach (string File in FileList)
{
#region Video
if (File.EndsWith(".wmv")||File.EndsWith(".mpg")||File.EndsWith(".mpeg")||File.EndsWith(".avi")||File.EndsWith(".rmvb"))
{
if (v != null)
{
v.Stop();
v.Dispose();
v = null;
btPause.Visible = false;
btPlay.Visible = true;
}
v = new Video(File);
v.Owner = panel1;
v.Play();
v.Pause();
labelDuracaoOuCP.Text = (TimeSpan.FromSeconds(Convert.ToInt32(v.CurrentPosition))).ToString();
FileName = File;
tabPage1.Text = FileName;
trackPosition.Maximum = Convert.ToInt32(v.Duration);
trackVolume.Value = v.Audio.Volume;
}
e.Effect=DragDropEffects.All;
and when i Drag into the panel wich is the owner of the video it seems to create two objects insted of one.
how do I get only one of them?
ps: your article is great helped me alot!
Thanks
:)
i test it its greate ;) and simple :)