简体   繁体   中英

How can I route drag events in C# UserControl?

My intention was to write a LabelOrText UserControl that behaves like a Label or like a TextField - depending on a mode parameter in its constructor. The UserControl has a Text Property and it contains a Label and a TextField - but only one of both can be visible at a time. So in any Application Source I can now replace all the Labels and all the TextFields with my LabelOrText UserControl without changing any source code ... (Of course, not only the Text property is delegated to the visible inner control, also the Font property and the BackColor property and all the other properties are delegated to the visible inner control ...)

This works fine, except the DragOver and DragDrop Events don't work es expected.

--> LabelOrText1.DragDrop += myDDHandler should work like Label1.DragDrop += myDDHandler

--> LabelOrText1.DragOver += myDOHandler should work like Label1.DragOver += myDOHandler

and

--> any DragOver action on the inner Label with the mouse should call the myDOHandler(o, e)

--> any DragDrop action on the inner Label with the mouse should call the myDDHandler(o, e)


so it would be really simple to replace the Label Controls with my LabelOrText UserControl in any Application's Source Code.


Any Ideas on how to transparently route the Drag-Events from the inner Control through the UserControl will be highly appreciated!

Textbox alone sounds fine, but if you want to handle drag drop in a parent container, just set the DragDrop events of the label,text and self to the parent/self;

You can do this transparently by wiring up the events in the parent class, and it should be pretty easy.

in Parent object CTOR or elsewhere

//psuedocode
label.DragDrop += parentEvent;  //aggregate object
text.DragDrop += parentEvent;
DragDrop += parentEvent;

I did this with a panel, such that anything that was pushed and dragged cause the whole thing to follow your finger/mouse and scroll. Panning.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM