Click here to Skip to main content
15,905,566 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
        new int Top = 159;
        new int Left = 319;
       
        Stack<TextBox> textboxes = new Stack<TextBox>();
        List<String> controlNames = new List<String>();

private void addTextBox_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);

}

  private void button4_Click(object sender, EventArgs e)
        {


                int n = 4;
                while (n < controlNames.Count)
                {
                    // First we get the textboxes
                    TextBox textBox1 = (TextBox)this.Controls.Find(controlNames[n - 3], 
                    conn.Open();
                    SqlCommand cmd = new SqlCommand("test_info", conn);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@test", textBox1.Text);
                    cmd.ExecuteNonQuery();
                    conn.Close();
  
                   
                  }

                     MessageBox.Show("Data Save");
                     ClearTextBoxes();
                }
true)[0];

 private void ClearTextBoxes()
        {
            

              foreach (TextBox control in textboxes)
                   if (control is TextBox)
                    (control as TextBox).Clear();

}


What I have tried:

I Tried this code but it will delete all textboxes input event static i only want to specify the dynamic textbox to be the one only clear after being save in the db.

C#
private void ClearTextBoxes()
 {
     Action<Control.ControlCollection> func = null;

     func = (controls) =>
         {
             foreach (Control control in controls)
                 if (control is TextBox)
                     (control as TextBox).Clear();
                 else
                     func(control.Controls);
         };

     func(Controls);
 }
Posted
Updated 24-Mar-22 10:18am
v2
Comments
[no name] 24-Mar-22 0:38am    
Add a property / field to store a reference to the "dynamic textbox" you create so you don't have to keep hunting for it in the Controls collection.

1 solution

Why to use Stack<TextBox>?
Use List<TextBox> instead.

C#
List<TextBox> myTexBoxes = new List<TextBox>();

private void addTextBox_Click(object sender, EventArgs e)
{
    TextBox textadd1 = new TextBox();
    //... your code here
   myTexBoxes.Add(textadd1);
}


Then...
C#
private void ClearTextBoxes()
{
    foreach(TextBox tb in myTextBoxes)
        tb.Clear(); //tb.Text = string.Empty;
}
 
Share this answer
 

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