Drag and Drop in Windows Forms - A Primer

Your First Drag and Drop

Your First Drag and Drop

Assuming you have Visual Studio.Net all fired up and a Windows Application in front of you then add a form to the project (or use an existing one), call it what you will and add 2 textboxes called tbFrom and tbTo. This complicated naming system indicates that one will contain text to drag and the other will be the dropee or the control you drop the text into. Set tbFrom's text property to "DragMe" or whatever you want, I hate telling people what to do, and blank out tbTo's text property. So now lets write some code for the whole process. First off you have to allow drag & drop operations on the controls you want to have the facility. With a textbox and most other controls this is done by setting their AllowDrop=True. With this done, in this example setting the tbTo AllowDrop to True, we can code the actual drag&drop. Consider this code:-

Private Sub tbMouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles tbFrom.MouseDown
    tbFrom.DoDragDrop(tbFrom.Text, DragDropEffects.Move)
End Sub

Private Sub tbDragEnter(ByVal sender As Object, ByVal e As DragEventArgs) Handles tbTo.DragEnter
    If (e.Data.GetDataPresent(DataFormats.Text)) Then
        e.Effect = DragDropEffects.Move
    End If
End Sub

Private Sub tbDragDrop(ByVal sender As Object, ByVal e As DragEventArgs) Handles tbTo.DragDrop
    tbTo.Text &= e.Data.GetData(DataFormats.Text).ToString
    tbFrom.Text = ""
End Sub

So not too bad is it. For the mouse down event we start a D&D process by using the DoDragDrop Event. This takes 2 parameters, the data to go into the clipboard and the effect to show the user. This can be one effect, DragDropEffects.Move, or a combination of more than one, DragDropEffects.Move Or DragDropEffects.Copy. Experimenting with this you'll see different visual styles for each. This effect does not affect what actually happens when you drag and drop. You implement this yourself using the DragDrop event. In the above example we first coded for the DragEnter event which you use to show an effect for when the user is hovering over a control that allows objects to be dropped. So in the above code we check to see if there is text in the clipboard, If (e.Data.GetDataPresent(DataFormats.Text)) Then, and set the effect accordingly. Controls that don't allow drops won't raise this event and windows helpfully shows the "You can't drop that here mate" symbol. So to drop we code the DragDrop event where we simply retrieve the text from the clipboard and put it in the textbox, tbTo.Text &= e.Data.GetData(DataFormats.Text).ToString, and in this case I've chosen a Move operation which I must implement myself and so I have blanked out the tbFrom textbox. Easy enough but pretty useless really. This is so basic it hampers the user. For example you would have to implement code to allow the user to select text in tbFrom as in the above example you can't. It initiates a D&D operation the minute you click on it and stops you from selecting text. A simple case of needing to be careful how you implement it. O.k. onwards to more useful examples....

Accepting Drops from another program

This is very simple so I won't spend too much time on it. It's exactly as above except you only need to code the DragEnter and DragDrop events on your control (and of course have the AllowDrop property set to True). The example I have included with this article accepts text being dragged from another program. The value of this is in how you might extend it. For example you might have a browser control and a textbox. When a user drags a valid url into the textbox you could navigate to that url or simply get the value from the clipboard when the mouseup event fires over the browser control. So a simpler example than above but perhaps more useful.

You might also like...

Comments

About the author

Brian O'Connell Ireland

Microsoft Certified Applications Developer with 10 years experience developing web based applications using asp, asp.net for a Local Authority in Dublin. Clings to a firm belief that a web appli...

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“C++ : Where friends have access to your private members.” - Gavin Russell Baker