Geekpedia Tutorials Home

Building a C# Chat Client and Server

Building a C# Chat Client and ServerA step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.

in C# Programming Tutorials

Getting Hard Drive Information

Getting Hard Drive InformationA C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.

in C# Programming Tutorials

UPS Shipping Calculator

UPS Shipping CalculatorThis tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.

in PHP Programming Tutorials

Create Your Own Rich Text Editor

Create Your Own Rich Text EditorCreating a Rich Text Editor using JavaScript is easier to do than you might think, thanks to the support of modern browsers; this tutorial will walk you through it.

in JavaScript Programming Tutorials
Search
Tutorials
Programming Tutorials
IT Jobs
From CareerBuilder

Using drag and drop operations

You'll learn how to support drag and drop operations in an application - dragging and dropping files on a form or control, using key combinations, retrieving information about the dropped files...

On Wednesday, November 3rd 2004 at 12:34 PM
By Andrew Pociu (View Profile)
*****   (Rated 4.8 with 41 votes)
Contextual Ads
More C# Resources
Advertisement
Drag and drop is the most natural way of doing some operations in software applications like adding items to a list. Thanks to .NET implementing drag and drop in your program is a very simple task. So let's cut to the chase.



Start a new C# Windows Application project which will create Form1, the form we'll be working with.



First you have to change the AllowDrop property of the form to True.








Change to the Events window now and scroll down to the DragEnter event:







This is the first event we are interested in, double click it so it can be created.

Now I should tell you that the DragEnter event of the form fires up when you drag something (an icon) on the form but you don't yet drop it there (release the mouse button).

In other applications we know,
what happens when you drag something on the form or over some control? Usually the cursor appears with a 'plus' (+) sign next to it to denote copying. But a arrow (typical shortcut arrow) can appear when creating a shortcut as a result of the drag and drop, or a rectangle when moving. Or all of them. Let's see.

Let's suppose that by default when dragging an icon on the form we want to move it, so on the DragEnter event use this:




private void Form1_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)

{

   e.Effect = DragDropEffects.Move;

}




As you can see, we are using the DragEventArgs
e
Effect property. Now try to drag an icon on the form and you'll see that next to the cursor the rectangle that symbolizes moving.

But there are applications which when you hold the Ctrl or Alt key pressed and dragging, they do something else like linking the file or copying it, and displaying the proper cursor. We can easily accomplish this using KeyState:





private void Form1_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)

{

   // If ALT is pressed

   if((e.KeyState & 32) == 32)

   {

      // It shows a cursor with an arrow

      e.Effect = DragDropEffects.Link;

   }

   // If CTRL is pressed

   else if((e.KeyState & 8) == 8)

   {

      // It shows a cursor with a plus sign

      e.Effect = DragDropEffects.Copy;

   }

   // If SHIFT is pressed

   else if((e.KeyState & 4) == 4)

   {

      // It shows an unavailable cursor

      e.Effect = DragDropEffects.None;

   }

   // If neither one is pressed

   else

   {

      // It shows the rectangle

      e.Effect = DragDropEffects.Move;

   }

}



Ok, but there's still something important we need to cover. How can you get the path of the file you have dropped?

Doubleclick the DragDrop event to get to the code. Here use the following:





private void Form1_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)

{

   string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);

   foreach(string file in files)

   {

      MessageBox.Show(file);

   }

}



The DragDrop event fires when you release the mouse button with an icon on top of a form, so exactly what we need.

Run the code now and drop a file on top of the form, the result is the path to the file displayed in a MessageBox:








Using the path of the file you can retrieve that file's information. And of course, the information doesn't have to be displayed in a MessageBox, you can use a ListBox or a ListView. And something else I should point out is that you can drag and drop multiple files at once because files is an array and we are using foreach().
Digg Digg It!     Del.icio.us Del.icio.us     Reddit Reddit     StumbleUpon StumbleIt     Newsvine Newsvine     Furl Furl     BlinkList BlinkList

Rate Rate this tutorial
Comment Current Comments
by ghjk on Monday, November 21st 2005 at 10:06 AM

hkjfg

by sowjanya on Friday, February 10th 2006 at 09:28 PM

dear sir/madam

your code is enough to write in c#.net, but i need code in vb.net. will you provide code for "drag and drop controls in multiple datagrids"

thanking you,
sowjanya

by FD on Tuesday, March 21st 2006 at 08:29 AM

Hi, thanks for the quick example. It's a good start.

here's thre VB code for anyone who's interested:

<div style="font-family: Courier New; font-size: 10pt; color: black; background: white; border-top: windowtext 1pt solid; padding-top: 0pt; border-left: windowtext 1pt solid; padding-left: 10pt; border-right: windowtext 1pt solid; padding-right: 0pt; border-bottom: windowtext 1pt solid; padding-bottom: 0pt; margin : 10px;">
<p style="margin: 0px;"><span style="color: blue;">Private</span> <span style="color: blue;">Sub</span> Form1_DragEnter(<span style="color: blue;">ByVal</span> sender <span style="color: blue;">As</span> <span style="color: blue;">Object</span>, <span style="color: blue;">ByVal</span> e <span style="color: blue;">As</span> System.Windows.Forms.DragEventArgs) <span style="color: blue;">Handles</span> <span style="color: blue;">MyBase</span>.DragEnter</p>
<p style="margin: 0px;">&nbsp;&nbsp; e.Effect = DragDropEffects.Move</p>
<p style="margin: 0px;"><span style="color: blue;">End</span> <span style="color: blue;">Sub</span></p>
<p style="margin: 0px;">&nbsp;</p>
<p style="margin: 0px;"><span style="color: blue;">Private</span> <span style="color: blue;">Sub</span> Form1_DragDrop(<span style="color: blue;">ByVal</span> sender <span style="color: blue;">As</span> <span style="color: blue;">Object</span>, <span style="color: blue;">ByVal</span> e <span style="color: blue;">As</span> System.Windows.Forms.DragEventArgs) <span style="color: blue;">Handles</span> <span style="color: blue;">MyBase</span>.DragDrop</p>
<p style="margin: 0px;">&nbsp;&nbsp; <span style="color: blue;">Dim</span> files() <span style="color: blue;">As</span> <span style="color: blue;">String</span> = e.Data.GetData(DataFormats.FileDrop)</p>
<p style="margin: 0px;">&nbsp;</p>
<p style="margin: 0px;">&nbsp;&nbsp; <span style="color: blue;">For</span> <span style="color: blue;">Each</span> file <span style="color: blue;">As</span> <span style="color: blue;">String</span> <span style="color: blue;">In</span> files</p>
<p style="margin: 0px;">&nbsp;&nbsp; &nbsp;&nbsp; MessageBox.Show(file)</p>
<p style="margin: 0px;">&nbsp;&nbsp; <span style="color: blue;">Next</span></p>
<p style="margin: 0px;"><span style="color: blue;">End</span> <span style="color: blue;">Sub</span></p>
</div>

by bezorn on Saturday, April 8th 2006 at 12:25 PM

Great work!

When I drag an icon over the form with one keystate and then change the keystate, the new keystate is not recognized unless I move the cursor back off the form before changing the keystate. How can I fix this?

Thanks!

by Aydin on Wednesday, April 26th 2006 at 05:15 AM

Thank you very much :)

by Lars Mæhlum on Monday, May 22nd 2006 at 11:15 AM

Bezorn:

Do a check for keystate in both DragEnter, and DragDrop, and make a \"sub\" for key-events.

by Kennebel on Wednesday, June 14th 2006 at 02:19 PM

bezorn/Anyone Else Curious:

Just make the "DragOver" event use the same code as the "DragEnter".

(object).DragEnter += new DragEventHandler(Form1_DragEnter);
(object).DragOver += new DragEventHandler(Form1_DragEnter);

You can do this manually, or simply copy the text in the "Properties" window from DragEnter to DragOver.

by Phanindra on Monday, July 3rd 2006 at 03:24 AM

hai,

I am able to drag the files from the My Pictures folder but i am not able to drag the files from the openFileDialog box which i opened from my application through a button. How can i fix this?

by priya on Tuesday, June 12th 2007 at 02:29 AM

Hi,

I want to to create a dragover option for input file in .net 1.0

regards,
Priya.B

by sandeep on Friday, December 21st 2007 at 04:02 AM

i want to drag and drop control on the asp.net page. how to do it.
it \'s urgent please provide me the solution

by Yekyaa on Monday, December 24th 2007 at 02:06 PM

This code was written in 2004 and you posted 3 days ago urgently
wanting this information given to you. Go look at rentacoder.com
You can pay someone to give you your solution "urgently".

by Atif on Tuesday, January 13th 2009 at 02:31 PM

Great Example. I was really looking for this. But Now please anyone can tell me how i can make drag and drop CustomBuild Text Box or buttons on my application

by Atif on Tuesday, January 13th 2009 at 02:31 PM

Great Example. I was really looking for this. But Now please anyone can tell me how i can make drag and drop CustomBuild Text Box or buttons on my application

by mutasem on Monday, April 20th 2009 at 03:21 AM

thank you for this , can you do the same example for web application

by Nagarathna on Tuesday, May 12th 2009 at 08:13 AM

DragDrop event is not firing at all. please anyone can help me in this regard.

thanks in advance.

by Nagarathna on Tuesday, May 12th 2009 at 08:20 AM

DragDrop event is not firing at all. please anyone can help me in this regard.

thanks in advance.

by dgasgsg on Tuesday, October 27th 2009 at 11:26 PM

good but I know this already.................

by falcon on Wednesday, November 18th 2009 at 07:46 AM

thanks good starting

by falcon on Wednesday, November 18th 2009 at 07:46 AM

thanks good starting

by хрень полная!!!!! on Friday, April 9th 2010 at 04:09 AM

хрень полная!!!!!!!!!

by Niranjan kumar on Monday, May 3rd 2010 at 04:32 AM

Good one to start with. I followed your steps and successfully implemented drag and drop function.


Comment Comment on this tutorial
Name: Email:
Message:
Comment Related Tutorials
There are no related tutorials.

Comment Related Source Code
There is no related source code.

Jobs C# Job Search
My skills include:
Enter a City:

Select a State:


Advanced Search >>
Sponsors
Discover Geekpedia

Other Resources