Basic drag and drop in WinForms

Adding drag and drop functionality to an app is no rocket science. All it takes is setting some properties and handling some events. But browsing MSDN and Google to memorize exactly which properties and events you easily drown in all the info. Due to several reasons: there are quite a few events you don’t need (yet); most examples treat several totally different subjects which makes it hard to quickly distill the drag and drop stuff; and not all member names are that logical at first sight. Enough reason for a little summary.


What do I want to do : Drag some info from one windows control and drop that on another. As an example I’ll drag a label and drop it on a listbox to add the caption to the items. The control where the drag is started is the originator, the control which is dropped upon the target.


Steps:


1. Start the drag operation in the originator. This is done by invoking the DoDragDrop method, a member of the Control base class. The average user expects to start a drag by clicking the mouse button, so a good place to fire of the method is the MouseDown event. The DoDragDrop method takes two parameters, the first one is an object which can contain any data you wish to drop on the target. The second parameter is an enumeration providing some info on the intent of the originator.


private void label_MyMouseDown(object sender, MouseEventArgs e)


{


    DoDragDrop((sender as Label).Text, DragDropEffects.Link);


}


Now when the user holds down the mouse and moves it a little the drag operation starts. The cursor will change providing visual feedback.


The TreeView and the ListView control have an ItemDrag event, which fires when the user starts dragging an individual node or item. These are not in the DragDrop category but in the Action category. But when handling those events you invoke DoDragDrop the same way.


2. Select potential drop targets by setting their AllowDrop property to true.


3. In the drop target determine whether it will accept a drop. This is done in the DragEnter event. The code can inspect the originator’s intent by examining the AllowedEffect property. This enumeration has a bitflag, so it can combine several AllowedEffect‘s. The code can also check if data of a certain desired type is available by inspecting the Data property. Here the listbox checks on the Link effect and checks for the presence of string data.


private void listBox_MyDragEnter(object sender, DragEventArgs e)


{


    if ((e.AllowedEffect & DragDropEffects.Link) != 0


        && e.Data.GetDataPresent(typeof(string)))


        e.Effect = DragDropEffects.Link;


}


By setting the Effect property the target tells it will accept a drop. The value of the Effect property determines what the cursor looks like when it enters the target. Targets which don’t have the AllowDrop property set or refuse to accept a drop will display a “no access” cursor.


4. Accept the drop and handle the data. When the user release the mouse and the target has expressed it will accept a drop the DragDrop event fires. This is passed the same DragEventArgs as the DragEnter event. Here the listbox actually extracts the string from the Data property, by invoking GetData instead of GetDataPresent, and adds it to its items.



private void listBox_MyDragDrop(object sender, DragEventArgs e)


{


    (sender as ListBox).Items.Add(e.Data.GetData(typeof (string)));


 


}


That’s all. Note that all eventhandler snippets are not bound to a specific control, they all take the sender parameter to find their target. There are many more possibilities and gadgets concerning drag and drop. But this is all you need for the basic functionality to work.

This entry was posted in Uncategorized. Bookmark the permalink. Follow any comments here with the RSS feed for this post.
  • noob java

    nice’

  • Vaqar Hyder3567

    Thanks a million…that worked like a charm

  • Anonymous

    The type of the data parameter in DoDragDrop is object. Which gives you the possibility to pass the droptarget all info needed. Instead of querying the originator of the drag-action, just pass the data itself.

  • ncoder83

    let’s say i am dropping a button on the list box.
    how do i know if the data is coming from the button or the label in the drag drop event?

  • http://codebetter.com/members/pvanooijen/default.aspx pvanooijen

    Sounds like a somewhat confusing UI to me. What if the user drags the mouse to the left and hits the rigth arrow button ?

    Perhaps this approaches your idea
    - Select the control by clicking. Handle mousedown and/or mouseclick
    - Move the control by using the arrowkeys Handle keydown
    - Deselect the control by releasing the mouse ? (Handle mouseup), by something else ?

    But that’s not drag and drop. Drag and drop is mouse controlled behaviour. By definition…

  • http://codebetter.com/members/Dinuni/default.aspx Dinuni

    Yes I can drag controls.But I actually want to drag control and move it with mouse to another panel.With DragDrop event as think there is only placing the control and not moving with mouse.Am I correct?I think you are the best person who can help me.Thank you.

  • http://codebetter.com/members/Dinuni/default.aspx Dinuni

    Thank you.I want to drag controls to another panel and move them with the cursor when dragging.How can I do that?I think you are the best guy who can help me.

  • http://codebetter.com/members/pvanooijen/default.aspx pvanooijen

    “m not sure if I understand the question. You can darg anything, darging a control is done by changing the control’s postition in the drop event..

  • http://codebetter.com/members/Dinuni/default.aspx Dinuni

    Thank you so much for your example.Can we move controls with mouse when dragging?

  • Himanshu Singh

    Compexity steps through Simplicity and Stong Fundamentals. You article of Drag & Drop in C# is proving the same point. Thanks a lot!

  • http://petersgekko.codebetter.com pvanooijen

    Thanks Randz

  • Randz

    Man, avcx, you ungreatful slob. How hard is it to copy and paste those events?

    Thanks Peter for the nice simple explanation.

  • Widgetx

    Thanks for posting this… last time I did windows based D&D was in VB456 … Now the next part is how to drag files from explorer… If I remember the vb property used to expose a files collection – the Data property on the DragEventArgs obj needs a cast etc…

  • http://codebetter.com/blogs/peter.van.ooijen/ pvanooijen

    Read the story and follow the steps. That will make you understand what you are doing.
    There are three events you have to implement, just copy the code from the post.

  • Codemonkey

    Can you post a downloadable version or a code sample of this?

  • avcx

    It would be nice if you had left the fucking code.