Design-time moving of a child control owned by a composite user control

烂漫一生 提交于 2019-12-11 21:31:45

问题


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:

  1. Extend UserControl class
  2. Define a new ControlDesigner class deriving from the default UserControl designer class (UserControlDocumentDesigner I guess)
  3. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!