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

问题描述

你好,

我有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

推荐答案

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.



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

05-25 18:22