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