Click here to Skip to main content
15,113,131 members
Articles / Programming Languages / C#
Posted 2 Sep 2004


16 bookmarked

Drag-and-Drop in UserControls

Rate me:
Please Sign up or sign in to vote.
2.73/5 (11 votes)
2 Sep 2004CPOL2 min read
How to pass drag-and-drop events to child controls in UserControls


Here's a problem that comes up pretty regularly: You have a complex user interface with a number of controls, groups of which need to be reused in different places. To manage the complexity and facilitate reuse, you encapsulate each group of controls in a UserControl. All works well, until you need to perform a drag-and-drop operation involving one of the child controls in one of your UserControls. The UserControl intercepts drag-and-drop events, so they never make it to the child controls.

This problem must come up all the time, but oddly enough, there isn't much written about it in the Microsoft newsgroups. This article addresses the problem and provides a simple solution along with a sample project.

The Simple Solution

The solution to this problem is simple, but it isn't intuitive. To pass drag-and-drop events to child controls in a UserControl, set the AllowDrop property of the UserControl to false in the UserControl project. Then set the AllowDrop properties of its child controls to true. Once these changes are made, the UserControl won't intercept drag-and-drop events intended for its child controls.

Note that changing the UserControl's AllowDrop property in a client form has no effect. When the UserControl is added to a form, its child controls will receive drag-and-drop events regardless of whether the UserControl's AllowDrop property is set to true or false.

The Demo Project

This article includes a simple demo solution that shows how to pass drag-and-drop events to child controls. The demo contains two projects, UserControl and TestHarness. The purposes of the projects are self-explanatory.

The TestHarness contains three controls; a button to drag, an 'ordinary' text box to drop it on, and another text box inside a UserControl. The ordinary text box is provided to verify that drag-and-drop is working; drag the button to this text box to verify. The UserControl  passes a drop event to its child text box; drag the button to this text box to verify.

Please post any questions here; I'll check in periodically to answer them.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

David Veeneman
Software Developer (Senior) Foresight Systems
United States United States
David Veeneman is a financial planner and software developer. He is the author of "The Fortune in Your Future" (McGraw-Hill 1998). His company, Foresight Systems, develops planning and financial software.

Comments and Discussions

GeneralCursor Image Pin
aldo hexosa2-Feb-09 21:40
professionalaldo hexosa2-Feb-09 21:40 
How to change the cursor image when i drag usercontrol in panel?
thank you
GeneralNot Works in VS 2005 Pin
serhhio26-Nov-07 0:20
Memberserhhio26-Nov-07 0:20 
GeneralThe Opposite. Pin
Forsworn12-Dec-06 19:19
MemberForsworn12-Dec-06 19:19 
Generaldrag and drop files from OpenFileDialog box Pin
Phanindra Kumar6-Jul-06 0:42
MemberPhanindra Kumar6-Jul-06 0:42 
GeneralRe: drag and drop files from OpenFileDialog box Pin
David Veeneman6-Jul-06 6:10
MemberDavid Veeneman6-Jul-06 6:10 
GeneralExcellent! Pin
jlbarajas12312-Oct-05 7:52
Memberjlbarajas12312-Oct-05 7:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.