I have two forms. Clicking button on form1 opens form2 where user adds details and this is returned back to form1, information is stored to List and TextBoxes and other elements of interface are created.
I have 5 tabs for different levels but information added is the same.
How can I avoid creating similar code 5 times using
if (level==5) {//do this whole code again}
Example of element added:
int _nextTextBoxTop=15;
List<TextBox> CodesMy = new List<TextBox>();
var code = new TextBox();
CodesMy.Add(code);
code.Location = new Point(12, _nextTextBoxTop);
_nextTextBoxTop += 36;
code.Size = new Size(80, 25);
code.Text = mcode;
tabPageLevel5.Controls.Add.Controls.Add(code);
The simplest solution could be to refactor your element creation into a separate function like this:
Your client code would then look like:
A simpler way than dynamically adding controls could be to create a user control that is used on each of your tab pages.
Even if you need to add controls dynamically you could still do it with a user control and clone it each time per each additional tab page.