A 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.
A C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.
This 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.
Creating 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.
Using drag and drop operationsYou'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) |
|||
|
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:
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:
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:
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 It!
Del.icio.us
Reddit
StumbleIt
Newsvine
Furl
BlinkList
|
||||
|
||||
Current Commentshkjfg
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
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;"> 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;"> </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;"> <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;"> </p>
<p style="margin: 0px;"> <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;"> MessageBox.Show(file)</p>
<p style="margin: 0px;"> <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>
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!
Thank you very much :)
Bezorn:
Do a check for keystate in both DragEnter, and DragDrop, and make a \"sub\" for key-events.
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.
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?
Hi,
I want to to create a dragover option for input file in .net 1.0
regards,
Priya.B
i want to drag and drop control on the asp.net page. how to do it.
it \'s urgent please provide me the solution
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".
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
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
thank you for this , can you do the same example for web application
DragDrop event is not firing at all. please anyone can help me in this regard.
thanks in advance.
DragDrop event is not firing at all. please anyone can help me in this regard.
thanks in advance.
good but I know this already.................
thanks good starting
thanks good starting
хрень полная!!!!!!!!!
Good one to start with. I followed your steps and successfully implemented drag and drop function.
Related Tutorials
Related Source Code
C# Job Search