Click here to Skip to main content
11,630,144 members (76,801 online)
Click here to Skip to main content

Drag-and-Drop in UserControls

, 2 Sep 2004 CPOL 63.7K 3.5K 15
Rate this:
Please Sign up or sign in to vote.
How to pass drag-and-drop events to child controls in UserControls
<!-- Article Starts - DO NOT ADD HTML/BODY START TAGS-->

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)

Share

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.

You may also be interested in...

Comments and Discussions

 
GeneralCursor Image Pin
aldo hexosa2-Feb-09 20:40
memberaldo hexosa2-Feb-09 20:40 
GeneralNot Works in VS 2005 Pin
serhhio25-Nov-07 23:20
memberserhhio25-Nov-07 23:20 
GeneralThe Opposite. Pin
Forsworn12-Dec-06 18:19
memberForsworn12-Dec-06 18:19 
Generaldrag and drop files from OpenFileDialog box Pin
Phanindra Kumar5-Jul-06 23:42
memberPhanindra Kumar5-Jul-06 23:42 
GeneralRe: drag and drop files from OpenFileDialog box Pin
David Veeneman6-Jul-06 5:10
memberDavid Veeneman6-Jul-06 5:10 
GeneralExcellent! Pin
jlbarajas12312-Oct-05 6:52
memberjlbarajas12312-Oct-05 6:52 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150723.1 | Last Updated 3 Sep 2004
Article Copyright 2004 by David Veeneman
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid