WinForms 设计器:添加控件并使其对设计器可见
我有一个带有自定义 UI 编辑器的 WinForms 用户控件 Host
。 通过该编辑器,可以将子控件 (Child
) 添加到 Host
。
(UI 编辑器创建 Child
并设置 Child.Parent = Host
)
Child 通过 Holder
辅助类进行处理,该辅助类设置作为例如 ListViewItem 的 Tag
属性。
相应的代码(至少其中一些)被添加到表单中:创建 Holder,并将其设置为 Tag,这也足以在运行时创建。
但是,子级对设计者来说是不可见的 - 它会显示,但无法选择,也不会出现在带有父窗体控件的下拉列表中。
我想:
- 在设计器中查看
Child
控件,以便我可以修改属性, - 并在控件被删除时收到通知
这可能吗?
[编辑]感谢大家的意见。我决定跳过设计师——我希望能快速拼凑出一些东西,但显然这需要更多的计划,而不是我现在应该允许自己花在上面的东西。
I have a WinForms user control Host
with a custom UI Editor.
Through that editor, a child control (Child
) can be added to Host
.
(The UI Editor creates Child
and sets Child.Parent = Host
)
Child is handled through a Holder<Child>
helper class, which is set as Tag
property of e.g. a ListViewItem.
The respective code - some of it, at least - gets added to the form: Holder is created, and set as Tag, which is enough to be created at runtime, too.
However, Child is not visible to the designer - it is displayed, but it can't be selected, nor does it occur in the drop down list with controls for the parent form.
I would like to:
- see the
Child
control in the designer, so that I can modify properties - get notified if the control is removed
Is this possible?
[edit] Thanks all for your input. I've decided to skip the designer - I hoped to throw together something quickly, but apparently it requires more planning than I should allow myself to spend on it right now.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在宿主类上使用
this.Controls.Add(/*子级的实例*/);
。然后,为通知添加主机 ControlRemoved 事件的事件处理程序 (this.ControlRemoved += new ControlEventHandler(Host_ControlRemoved);
)。Use
this.Controls.Add(/*Instance of the child*/);
on the host class. Then for the notification add event handler for the host's ControlRemoved event (this.ControlRemoved += new ControlEventHandler(Host_ControlRemoved);
).我不能说我完全理解你想要做什么。
如果您正在处理如何使在设计时放置在窗体上的 UserControl 的“子”控件充当容器的问题,您可以将工具箱中的其他控件拖放到其上:此 CodeProject Henry Minute 的文章可能会有所帮助:设计嵌套控件。例如:您有一个带有面板的 UserControl:UserControl 的实例放置在表单上:在表单的设计时视图中:您希望能够将控件拖放到 UserControl 中的面板上并使它们成为面板的子控件:亨利的文章将向您展示如何做到这一点。
来自 Microsoft 的:如何使用 Visual C# 使 UserControl 对象在设计时充当控件容器
也许也可能有用,尽管您似乎已经完成了这一步。
I can't say I fully understand exactly what you are trying to do.
If you are dealing with the problem of how a "child" Control of a UserControl placed on a Form at Design-Time can be made to function as a container onto which you can drag-and-drop other controls from the Toolbox : this CodeProject article by Henry Minute may be helpful : Designing Nested Controls. For example : you have a UserControl with a Panel inside it : an instance of the UserControl is placed on a Form : in the Design-time view of the Form : you want to be able to drag-drop controls onto the Panel in the UserControl and have them become child controls of the Panel : Henry's article will show you how to do that.
This from Microsoft : How to make a UserControl object acts as a control container design-time by using Visual C#
Perhaps might also be useful, although it seems like you already have this step accomplished.