如何创建一个可以删除其他控件的UserControl?

时间:2022-09-25 08:46:11

In WinForms, how can I create a UserControl that when I put on my form I can then add other controls inside by dragging them from the toolbox, the same way as with all containers controls (panels, group boxes, etc)? I've tried to add controls by dropping them in my control but then when I move my control the controls I added stay right where they are, which wouldn't happen if instead of my control I would use a Panel (the other controls would move with the panel).

在WinForms中,我如何创建一个UserControl,当我放入我的表单时,我可以通过从工具箱中拖动它们来添加其他控件,就像所有容器控件(面板,组框等)一样?我试图通过将控件放在我的控件中来添加控件,但是当我移动控件时,我添加的控件保持在他们所在的位置,如果不是我的控制我将使用Panel(其他控件将会发生)与小组一起移动)。

1 个解决方案

#1


21  

Unlike a Panel control for example, a UserControl does not act as a container control once it is placed on another form. There is full design-time support while you are designing the UserControl itself, but its default behavior does not allow it to act as a constitutent control after it has been placed on another form. This is why you are unable to add other controls to it by dragging them from the toolbox.

例如,与Panel控件不同,一旦将UserControl放置在另一个窗体上,它就不会充当容器控件。在设计UserControl本身时有完整的设计时支持,但是它的默认行为不允许它在放置在另一个表单上之后充当构件控件。这就是您无法通过从工具箱中拖动其他控件来添加其他控件的原因。

In order to add this type of behavior to a UserControl, you need to add the DesignerAttribute to the definition of your custom UserControl class. For example:

为了将此类行为添加到UserControl,您需要将DesignerAttribute添加到自定义UserControl类的定义中。例如:

using System.ComponentModel;
using System.ComponentModel.Design;

[Designer("System.Windows.Forms.Design.ParentControlDesigner, System.Design", typeof(IDesigner))]
public class MyUserControl : System.Windows.Forms.UserControl
{
    //...your code here
}

(See the relevant MSDN article for further reading.)

(有关进一步阅读,请参阅相关的MSDN文章。)


If you want to implement full designer support for nested controls inside your UserControl, this is slightly more difficult. For a more comprehensive discussion, see this article on CodeProject.

如果要对UserControl中的嵌套控件实现完全设计器支持,则稍微困难一些。有关更全面的讨论,请参阅有关CodeProject的这篇文章。

#1


21  

Unlike a Panel control for example, a UserControl does not act as a container control once it is placed on another form. There is full design-time support while you are designing the UserControl itself, but its default behavior does not allow it to act as a constitutent control after it has been placed on another form. This is why you are unable to add other controls to it by dragging them from the toolbox.

例如,与Panel控件不同,一旦将UserControl放置在另一个窗体上,它就不会充当容器控件。在设计UserControl本身时有完整的设计时支持,但是它的默认行为不允许它在放置在另一个表单上之后充当构件控件。这就是您无法通过从工具箱中拖动其他控件来添加其他控件的原因。

In order to add this type of behavior to a UserControl, you need to add the DesignerAttribute to the definition of your custom UserControl class. For example:

为了将此类行为添加到UserControl,您需要将DesignerAttribute添加到自定义UserControl类的定义中。例如:

using System.ComponentModel;
using System.ComponentModel.Design;

[Designer("System.Windows.Forms.Design.ParentControlDesigner, System.Design", typeof(IDesigner))]
public class MyUserControl : System.Windows.Forms.UserControl
{
    //...your code here
}

(See the relevant MSDN article for further reading.)

(有关进一步阅读,请参阅相关的MSDN文章。)


If you want to implement full designer support for nested controls inside your UserControl, this is slightly more difficult. For a more comprehensive discussion, see this article on CodeProject.

如果要对UserControl中的嵌套控件实现完全设计器支持,则稍微困难一些。有关更全面的讨论,请参阅有关CodeProject的这篇文章。