Click here to Skip to main content
15,355,133 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
  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;
                


        }

// button to save 
<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)
                {
                    // First we get the textboxes
                    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];

                    // Insert to db
                    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();
                    // We increment the iterator variable by 2
                    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];
Posted
Updated 21-Mar-22 21:01pm

1 solution

Since n starts with one:
int n = 1;
while (n < controlNames.Count)
{
These three lines will automatically throw an exception as the index is negative:
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];


Do yourself a favour, and streamline that code: you have massive chunks of redundancy where the code uses is the identical - move that into a method, and call it to make your whole code both more reliable and easier to read.
Create a single font item, and reuse it, Fonts are scarce resources and shouldn't be created for no reason.
And don't write comments that describe what the code says - they are useless and tend to be inaccurate when the code gets changed. Just like this:
conn.Close();
// We increment the iterator variable by 2
n = n + 5;
Comments should help explain code, not say what we know. And wrong comments? worse than useless.
   
Comments
Imba Gaming 22-Mar-22 3:21am
   
How can i fix the code on that error.
OriginalGriff 22-Mar-22 3:52am
   
By thinking about what - exactly - you are trying to do (and remember, we have no idea what your project is all about) and working out how it should be done.

I can't write code if I have no idea why it is doing something that ... odd.

To be honest, that code looks like it was thrown together by a student who didn't think much about the task before he dived into coding - and that's always a mistake! Think about the task, and plan ahead - don't start a job until you know what you are trying to do, how you are going to do it, and have the material / knowledge you need ready. Five minutes of thinking can save you hours of pain!

That applies in computing as well as real life - it you start off without planning ahead when building steps in your garden you will be stopping work and visiting the builders merchant / garden centre / tool shop several times during the process. (The record for a friend of mine is 5 shopping trips in an afternoon for a job that would have taken him an hour at most if he'd planned ahead. Writing software is the same, honest!)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900