Click here to Skip to main content
Licence CPOL
First Posted 2 Sep 2004
Views 51,309
Bookmarked 13 times

Drag-and-Drop in UserControls

By | 2 Sep 2004 | Article
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)

About the Author

David Veeneman

Software Developer (Senior)
Foresight Systems
United States United States

Member

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.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralCursor Image Pinmemberaldo hexosa20:40 2 Feb '09  
GeneralNot Works in VS 2005 Pinmemberserhhio23:20 25 Nov '07  
GeneralThe Opposite. PinmemberForsworn18:19 12 Dec '06  
Generaldrag and drop files from OpenFileDialog box PinmemberPhanindra Kumar23:42 5 Jul '06  
GeneralRe: drag and drop files from OpenFileDialog box PinmemberDavid Veeneman5:10 6 Jul '06  
GeneralExcellent! Pinmemberjlbarajas1236:52 12 Oct '05  

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.

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