如何创建一个用户控件,你可以滴在它的其他控件? [英] How to create a UserControl that you can drop other controls in it?

查看:132
本文介绍了如何创建一个用户控件,你可以滴在它的其他控件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在的WinForms,我怎么可以创建一个用户控件,当我把我的表,我可以再里面从工具箱拖动添加其他控件,以同样的方式作为与所有容器控件(面板,组合框等)?我试图通过降低它们在我的控制添加控件但是当我提出我的控制控件,我添加呆在他们在哪里,这是不会发生的,而不是我控制的,如果我会用一个面板(其它控件将与面板中移动)。

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).

推荐答案

不像面板控件例如用户控件不充当一个容器控制,一旦它被放置在另一种形式。有完整的设计时支持,而你正在设计用户控件本身,而是它的默认行为不允许它作为一个组成成分控制后它已被放置在另一种形式。这就是为什么你无法从工具箱中拖动其他控件添加到它。

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.

为了增加这种行为的类型为用户控件,你需要添加<一href="http://msdn.microsoft.com/en-us/library/system.componentmodel.designerattribute.aspx"><$c$c>DesignerAttribute您的自定义用户控件类的定义。例如:

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:

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
}

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

(See the relevant MSDN article for further reading.)


如果你想实施的嵌套的控制完全设计器支持在你的用户控件,这是稍有难度。对于多个COM prehensive的讨论,请参见在$ C $的CProject这篇文章


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.

这篇关于如何创建一个用户控件,你可以滴在它的其他控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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