问题
I have a Windows Forms user control, which (for various reasons irrelevant to this issue) are exposed in the designer, much like the panels in a split panel. Most everything works nicely, except for dragging the control. The child controls are created by the user control, and it does not accept new children. The intent is only to allow the properties of the predefined children to be edited.
Since the control allows the child controls to be designed, they can also be selected in the designer (this is a good thing, and I don't want to change this). However, they can not and should not be moved individually.
What I would like to happen is that when a child control is dragged in the designer, the drag actually moves the parent.
I've been skimming the documentation on control designers, but nothing popped up that would be simple or obvious.
回答1:
Try this and tell me what happens:
- Extend UserControl class
- Define a new ControlDesigner class deriving from the default UserControl designer class (UserControlDocumentDesigner I guess)
Within this designer write this inside the initialize method override:
IComponentChangeService changeService = this.GetService(typeof(IComponentChangeService)); changeService.ComponentRemoved += new ComponentEventHandler(changeService_ComponentRemoved);
you can now place logics about what happens when a control is removed. Remember that not just controls removed FROM your user control will fire this event. You have to check for that and also you need not to forget about the ComponentChanging and ComponentChanged events in order to make thinks work fine at design-time.
来源:https://stackoverflow.com/questions/1927400/design-time-moving-of-a-child-control-owned-by-a-composite-user-control