用户控件不能用作容器 [英] User Control doesn't work as a Container

查看:105
本文介绍了用户控件不能用作容器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我有2个用户控件{usercontrol1,usercontrol2}和1个Form {form1}.
Usercontrol1具有两个面板{panel1,panel2}.具有1个面板{panel1}的Usercontrol2.我从工具箱窗口中的Panel2 [usercontrol1.panel2]中添加(删除了)usercontrol2.
现在,当我将usercontol1拖放到form1上并尝试将控件(如按钮,文本框)放在panel1.usercontrol2中时,那么对于这些​​控件(如按钮,文本框),usercontrol1是单个控件.我无法添加panel1(用户控件2的)中的任何控件.

请给我建议解决方案...

在此先感谢,
Avdesh Kataria

Hello ,

I have 2 user controls {usercontrol1, usercontrol2} & 1 Form{form1}.
Usercontrol1 having two Panels {panel1, panel2}. Usercontrol2 having 1 Panel{panel1}. I added(dropped)the usercontrol2 in Panel2[usercontrol1.panel2]from toolbox window.
Now when I drag & drop the usercontol1 on form1 and try to put controls(like button,textbox) in a panel1.usercontrol2.Then for these controls(like button,textbox) usercontrol1 is a single Control.I''m not able add any control in a panel1(of a usercontrol2].

Please suggest me solution...

Thanks In Advance,
Avdesh Kataria

推荐答案

您的报告是正确的;不能做的就是不能做的. UserControl在以下形式的设计时不能用作容器(或其他用作现有UserControl的父级的控件).请考虑一下,这不是一个缺点:这样,UserControl的功能与其容器(可以是另一个用户控件或Form)隔离开来.如果您可以做自己想做的事情,则会造成一些混乱:您的usercontrol2.panel1将具有不同类型的不同子级.因此,您需要重新考虑控件层次结构.

第一个选项:为usercontrol2.panel1:content1content2创建不同的子控件(这些控件可以是两个或更多的用户控件,也可以是任何其他控件).在运行时,请执行以下操作:

Your report is correct; and what can not be done just can not be done. UserControl cannot work as a container in design time of the form (or other control you use as a parent of you existing UserControl). Come to think about, this is not a disadvantage: this way, functionality of UserControl is isolated from its container which can be another user control or Form. If you could do what you want, you would create some kind of mess: your usercontrol2.panel1 would have different children of different types. So, you need to rethink your control hierarchy.

First option: create different children controls for usercontrol2.panel1: content1 and content2 (these could be two more User Controls or any other controls, whatever you want). During run time, do the following:

usercontrol2.panel1.Controls.Add(content1);



和另一个实例与content2相同.当然,您将需要调整Docking,Padding等.您可以在呈现表单之前很好地调用此设置代码,例如,在表单构造函数的最后.

第二个选择:放弃额外的灵活性.不仅仅是拥有usercontrol2,还可以创建更多具有panel1内容的用户控件类型,例如usercontrol21usercontrol22.

顺便说一句,不要使用此命名约定.我只是用你的为例.最好不要在名称中使用数字.

一般说明:如果您需要真正的灵活性并重用某些控件的某些布局,则必须通过创建控件实例和调整布局来进行某些运行时操作.这非常容易,实际上比使用设计时间更容易获得支持.仅将设计时间用于基本布局.

一个简单的例子:如果您有5个几乎相同的控件实例(用户控件或任何其他实例)排列在行或列中,则应在设计时仅创建一个并将其用作示例.在运行时,您可以克隆它们,计算它们的布局并相应地调整表单的大小.

谢谢您的关注.



and same thing with another instance with content2. Of course you will need to adjust Docking, Padding, etc. You can call this set-up code well before rendering the form, for example, at the very end of the form constructor.

Second Option: Give up extra flexibility. Instead of having just usercontrol2, create more User Control types, such as usercontrol21 and usercontrol22, with different contents of panel1.

By the way, don''t use this naming conventions. I used yours just for example. Better never use numbers in names.

General note: If you need real flexibility and re-use of some layouts of some control, you''re bound to some run-time manipulations with creation of control instances and adjustments of layout. This is quite easy and actually much easier for support then using design time. Use design time only for basic layouts.

Simple example: if you have, say, 5 nearly identical instances of a control (User Control or any other) arranged in a row or a column, you should create just one during design time and use it for a sample. During run-time you can clone them, calculate their layouts and adjust the size of the form accordingly.

Thank you for your attention.


使用其他方法:切换到WPF:内容模型更加灵活(请参阅内容模型的帮助以了解有关内容)加上更多其他好处.
Use different approach: switch to WPF: the content model is much more flexible (see help on content model to learn about it) plus a lot more other benefits.


这篇关于用户控件不能用作容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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