在设计器模式下将子项添加到自定义控件 [英] Add child to custom Control in Designer mode

查看:122
本文介绍了在设计器模式下将子项添加到自定义控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到了此答案,用于为自定义控件添加设计者支持。它的工作原理包括添加子控件。除了在关闭设计器并再次打开之后-这些孩子消失了。

I have already seen this answer for adding designer support to a custom Control. It works up to and including adding child Controls. Except that after closing the designer and opening it again - those children are gone.

是否有某种方法可以为在设计器中添加子控件提供支持?也许在某些情况下,我们可以手动处理(即通过我的代码)?

Is there some way to get support for adding child Controls in the designer? Perhaps by some event that we can handle somehow "manually" (i.e. by my code)?

这不是 UserControl 。这是从 Panel 继承的类。

This is not a UserControl. It's a class inheriting from Panel.

推荐答案

对于从 Panel ,默认情况下启用了设计器,您无需执行任何操作,因为面板的设计器是 PanelDesigner ScrollableControlDeigner 派生,而<$ href = https://msdn.microsoft.com/en-us/library/system.windows.forms。 design.parentcontroldesigner(v = vs.110).aspx#Anchor_7 rel = nofollow noreferrer> ParentControlDesigner

For a class derived from Panel, the designer is enabled by default and you don't need to do anything, because the designer of panel is PanelDesigner which is derived from ScrollableControlDeigner which is derived from ParentControlDesigner:


ParentControlDesigner 为可以包含子控件的
控件的设计者提供基类。除了从 ControlDesigner
ComponentDesigner 类继承的方法
和功能之外, ParentControlDesigner 使子
控件可以在其在设计时扩展的行为中添加,删除,选择以及在其内排列。 p>

ParentControlDesigner provides a base class for designers of controls that can contain child controls. In addition to the methods and functionality inherited from the ControlDesigner and ComponentDesigner classes, ParentControlDesigner enables child controls to be added to, removed from, selected within, and arranged within the control whose behavior it extends at design time.

您可以通过将 ParentControldesigner 设置为控件的设计者来启用设计器。例如:

You can enable the designer by setting ParentControldesigner as designer of your control. For example:

using System.ComponentModel;
using System.Windows.Forms;
using System.Windows.Forms.Design;
[Designer(typeof(ParentControlDesigner))]
public class MyControl : Control
{
}

这篇关于在设计器模式下将子项添加到自定义控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆