Click here to Skip to main content
15,902,189 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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
Posted
Updated 8-Dec-10 20:20pm
v2
Comments
TweakBird 9-Dec-10 2:22am    
corrected spellings.

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:

C#
usercontrol2.panel1.Controls.Add(content1);


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.
 
Share this answer
 
v6
Comments
Espen Harlinn 26-Feb-11 10:45am    
Good reply - a 5
Sergey Alexandrovich Kryukov 26-Feb-11 13:46pm    
Thank you.
--SA
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.
 
Share this answer
 
Comments
Espen Harlinn 26-Feb-11 10:44am    
Good reply - a 5
Sergey Alexandrovich Kryukov 26-Feb-11 13:46pm    
Thank you.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900