<
Salaam Aleikum, SalouaK (hope that's appropriate), and welcome to CP QA.
This is much too long, and detailed, to be a comment on the OP, but I am going to hold off on giving
an example of how to accomplish what SalouaK wants to do here efficiently and easily until he shows
some initiative in responding to my detailed analysis here.
In English, the idiom that is appropriate for this type of "conditionality" for further investment is:
"putting skin in the game."[^]
>
Please consider:
0. Based on the code you've shown us here: we can only
fantasize what panel1 is, where it's created,
what else it may hold before you stick your new run-time created Controls into it..
Is panel1 meant to be the container that you will add all the run-time created Panels to the ControlCollection of: not "this" ?
Occam's Razor suggests
that is the most likely answer, and there is a better way to
suspend (hint, hint) panel1's run-time appearance while you go about creating all this stuff, if that's really necessary ... like when it's done outside the Form Load EventHandler, arbitrarily, at run-time, in response to some user action in the UI.
1. major flaw: all your run-time created Panels are added to the ControlCollection of 'this by
this.Controls.Add(panel[1]);
Okay, 'panel in this case is your variable name for a five-element array of Panels ('panelAry or 'ArrayOfPanels would be much more appropriate choices for this variable name, if you want other people to understand your code in the future). Also see comment #3 below.
But:
a. remember that when you create your array of Panels: your variable of type Panel[] 'panel, initially, contains 5 null entries.
b. so every time you execute: Controls.Add(panel[1]: you add a 'null, when the value of 'x == 0: once the value of 'x == 1: when you've actually inserted a new Panel into panel[1]: then:
c. for each iteration where 'x >= 1,
you will be re-adding panel[1] to the ControlsCollection of 'this !
This will not cause a run-time error, but: there will be one, and only one, instance of 'panel[1] added to this' ControlCollection !
~ aside: I'd actually like to see Visual Studio throw a run-time error if you re-add the same
instance of some Control to a ControlCollection, or add a 'null ! ~
Clearly, what you want to do is to add your run-time Panel to the "target" container using the index in your for-loop:
this.Controls.Add(panel[x]);
note that if you do, as I suspect, really intend 'panel1 to be the container for all the new run-time created Panels and their Label/TextBox pairs: then you'd want to see something like
panel1.Controls.Add(panel[x]);
2. we can see you add each new run-time created Panel to the ControlCollection of 'this: but if there are other Controls already in the ControlCollection of 'this: then the Index property of your run-time created Panels in that ControlCollection is going to be determined by how many other Controls are in it: is that going to create a problem for you later:
If you do, as I suspect, intend for 'panel1 to be the container for all the new run-time created Panels, and their Label/TextBox pairs: then that's not an issue
as long as 'panel1 has no other Controls in it..
3. most confusing of all: you set the Visible property of the Panel, 'panel1, to 'false every time you execute the for-loop. You only to make it
not visible once :)
4. you are using fixed values here both for the number of Panels created, and then the number of Label/TextBox "pairs" added into each Panel created: don't you really want to
make your code more flexible by using variables to contain the numbers of Panels, and Label/TextBox pairs ?
Well, okay, maybe it will always be 5 Panels and 20 Label/TextBoxes: no problem :)
5. because you set the Dock property to 'DockStyle.Fill, all Panels created at run-time, and that you add to whatever container's ControlCollection will appear "on top" of each other in their container Control, so "navigating" from Panel to Panel may require slight differences in code:
remember: as you add Controls to a ControlCollection, and set 'Dock = 'DockStyle.Fill: the first added will appear on top; the last added is at the bottom (default order is first to last): if you want the last added to appear on top, and the first on the bottom (last to first order): use 'BringToFront after you add the Control.
If do not want continuous navigation in-order from Panel to Panel, and are choosing which one to put on top based on some user action: this is irrelevant.
~
Now: how to do this much more easily: as soon as you, SalouaK, give some clear response to the questions I've asked here, and the issues I've pointed out in your code:
I'll be happy to help you further.
best, Bill