Click here to Skip to main content
15,881,812 members
Articles / Programming Languages / C#
Article

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 82.6K   4.7K   16   6
How to pass drag-and-drop events to child controls in UserControls

Introduction

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.

License

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


Written By
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 20:40
professionalaldo hexosa2-Feb-09 20:40 
How to change the cursor image when i drag usercontrol in panel?
thank you
GeneralNot Works in VS 2005 Pin
serhhio25-Nov-07 23:20
serhhio25-Nov-07 23:20 
GeneralThe Opposite. Pin
Forsworn12-Dec-06 18:19
Forsworn12-Dec-06 18:19 
Generaldrag and drop files from OpenFileDialog box Pin
Phanindra Kumar5-Jul-06 23:42
Phanindra Kumar5-Jul-06 23:42 
GeneralRe: drag and drop files from OpenFileDialog box Pin
David Veeneman6-Jul-06 5:10
David Veeneman6-Jul-06 5:10 
GeneralExcellent! Pin
jlbarajas12312-Oct-05 6:52
jlbarajas12312-Oct-05 6: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.