Hi, I am creating a program on behalf of my business and have become stuck in one particular matter. I am fairly new to C#.
The application consists of the user entering data into a mixture of comboBoxes and textboxes, after the user has entered the data he clicks the add button, when pressed an identical set of textboxes and comboboxes will appear for which I am using the following code.
TextBox field1 = new TextBox();
field1.Location = new Point(15, 120);
field1.Size = new Size(230, 20);
field1.Name = "field1";
this.Controls.Add(field1);
TextBox field2 = new TextBox();
field2.Location = new Point(260, 120);
field2.Size = new Size(42, 20);
field2.Name = "field2";
this.Controls.Add(field2);
ComboBox comboBox3 = new ComboBox();
comboBox3.Location = new Point(330, 120);
comboBox3.Size = new Size(240, 21);
comboBox3.Items.Add("Clear Float");
comboBox3.Items.Add("Low E");
comboBox3.Items.Add("Toughened Low E");
comboBox3.Items.Add("Toughened Float One Side");
comboBox3.Items.Add("Clear Laminate Low E");
comboBox3.Items.Add("Float Gray Tint");
comboBox3.Items.Add("Float Gray Tint Toughened One Side");
comboBox3.Items.Add("Obscure - Toilet Only");
comboBox3.Items.Add("Obscure - Bathroom Toughened One Side");
comboBox3.Items.Add("Toughened E Tch Lite - Toilet Only");
comboBox3.Items.Add("E Tch Lite - Toughened One Side");
comboBox3.Name = "comboBox3";
comboBox3.SelectedIndexChanged += new System.EventHandler(comboBox3_SelectedIndexChanged);
this.Controls.Add(comboBox3);
TextBox field3 = new TextBox();
field3.Location = new Point(590, 120);
field3.Size = new Size(60, 20);
field3.Name = "field3";
this.Controls.Add(field3);
TextBox field4 = new TextBox();
field4.Location = new Point(655, 120);
field4.Size = new Size(60, 20);
field4.Name = "field4";
this.Controls.Add(field4);
ComboBox comboBox4 = new ComboBox();
comboBox4.Location = new Point(720, 120);
comboBox4.Size = new Size(120, 21);
comboBox4.Items.Add("Light");
comboBox4.Items.Add("Standard");
comboBox4.Items.Add("Heavy");
comboBox4.SelectedIndexChanged += new System.EventHandler(comboBox4_SelectedIndexChanged);
comboBox4.Name = "comboBox4";
this.Controls.Add(comboBox4);
Button button2 = new Button();
button2.Location = new Point(860, 120);
button2.Size = new Size(40, 20);
button2.Text = "Add";
button2.Click += new System.EventHandler(button2_Click);
this.Controls.Add(button2);
As the user needs to enter a maximium of 10 diffrent values I am having to use this code for every add button by copying and pasting and renaming the buttons, txtboxes and comboBoxes, My question is, is there a more simpler way to having this code as it takes up alot of space and time having to copy and ast all the time.
Any help is appreciated and thanked in advance.