Stack<TextBox> textboxes = new Stack<TextBox>();
List<String> controlNames = new List<String>();
private void button3_Click(object sender, EventArgs e)
{
TextBox textadd1 = new TextBox();
this.Controls.Add(textadd1);
textadd1.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
textadd1.ForeColor = System.Drawing.Color.Black;
textadd1.Location = new System.Drawing.Point(Left, Top);
textadd1.Name = "textBox1"+ controlNames.Count;
textadd1.BringToFront();
textadd1.Size = new Size(36, 24);
textadd1.TextAlign = HorizontalAlignment.Center;
textadd1.Text = "";
textboxes.Push(textadd1);
controlNames.Add(textadd1.Name);
TextBox textadd2 = new TextBox();
this.Controls.Add(textadd2);
textadd2.BackColor = System.Drawing.Color.White;
textadd2.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
textadd2.ForeColor = System.Drawing.Color.Black;
textadd2.Location = new System.Drawing.Point(250, Top);
textadd2.Name = "textBox2" + controlNames.Count;
textadd2.BringToFront();
textadd2.Size = new System.Drawing.Size(56, 12);
textadd2.TextAlign = HorizontalAlignment.Center;
textboxes.Push(textadd2);
controlNames.Add(textadd2.Name);
TextBox textadd3 = new TextBox();
this.Controls.Add(textadd3);
textadd3.BackColor = System.Drawing.Color.White;
textadd3.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
textadd3.ForeColor = System.Drawing.Color.Black;
textadd3.Location = new System.Drawing.Point(450, Top);
textadd3.Name = "textBox3" + controlNames.Count;
textadd3.BringToFront();
textadd3.Size = new System.Drawing.Size(56, 12);
textadd3.TextAlign = HorizontalAlignment.Center;
textboxes.Push(textadd3);
controlNames.Add(textadd3.Name);
TextBox textadd4 = new TextBox();
this.Controls.Add(textadd4);
textadd4.BackColor = System.Drawing.Color.White;
textadd4.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
textadd4.ForeColor = System.Drawing.Color.Black;
textadd4.Location = new System.Drawing.Point(650, Top);
textadd4.Name = "textBox4" + controlNames.Count;
textadd4.BringToFront();
textadd4.Size = new System.Drawing.Size(56, 12);
textadd4.TextAlign = HorizontalAlignment.Center;
textboxes.Push(textadd4);
controlNames.Add(textadd4.Name);
TextBox textadd5 = new TextBox();
this.Controls.Add(textadd5);
textadd5.BackColor = System.Drawing.Color.White;
textadd5.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
textadd5.ForeColor = System.Drawing.Color.Black;
textadd5.Location = new System.Drawing.Point(850, Top);
textadd5.Name = "textBox5" + controlNames.Count;
textadd5.BringToFront();
textadd5.Size = new System.Drawing.Size(56, 12);
textadd5.TextAlign = HorizontalAlignment.Center;
textboxes.Push(textadd5);
controlNames.Add(textadd5.Name);
Top += 22;
}
<pre> if (t1.Text == "")
{
MessageBox.Show("Invalid");
}
else
{
conn.Open();
SqlCommand cm = new SqlCommand("test_info", conn);
cm.CommandType = CommandType.StoredProcedure;
cm.Parameters.AddWithValue("@test", t1.Text);
cm.Parameters.AddWithValue("@test1", t2.Text);
cm.Parameters.AddWithValue("@test2", t3.Text);
cm.Parameters.AddWithValue("@test3", t4.Text);
cm.Parameters.AddWithValue("@test4", t5.Text);
cm.Parameters.AddWithValue("@fname", label1.Text);
cm.ExecuteNonQuery();
conn.Close();
int n = 1;
while (n < controlNames.Count)
{
TextBox textBox1 = (TextBox)this.Controls.Find(controlNames[n - 1], true)[0];
TextBox textBox2 = (TextBox)this.Controls.Find(controlNames[n], true)[0];
TextBox textBox3 = (TextBox)this.Controls.Find(controlNames[n - 2], true)[0];
TextBox textBox4 = (TextBox)this.Controls.Find(controlNames[n - 3], true)[0];
TextBox textBox5 = (TextBox)this.Controls.Find(controlNames[n - 4], true)[0];
conn.Open();
SqlCommand cmd = new SqlCommand("test_info", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@test", textBox1.Text);
cmd.Parameters.AddWithValue("@test1", textBox2.Text);
cmd.Parameters.AddWithValue("@test2", textBox3.Text);
cmd.Parameters.AddWithValue("@test3", textBox4.Text);
cmd.Parameters.AddWithValue("@test4", textBox5.Text);
cmd.Parameters.AddWithValue("@fname", label1.Text);
cmd.ExecuteNonQuery();
conn.Close();
n = n + 5;
}
MessageBox.Show("Data Save");
textboxes.Clear();
refresh();
}
What I have tried:
i have 5 original textbox setup in my form if i press the button it will generate 5 the same dynamic textbox align in the original 5 text box if i put details in all textbox
like 1,2,3,4,5 and next dynamic is 6,7,8,9,10. it will give me an error
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index'
in this line of codes :/
TextBox textBox3 = (TextBox)this.Controls.Find(controlNames[n - 2], true)[0];