Home > Back-end >  How to remove a 5 dynamic textboxes in a row by pressing the button delete
How to remove a 5 dynamic textboxes in a row by pressing the button delete

Time:03-23

Have this add button that will create and generate 5 dynamic textbox in a single click now. what i want is that i want also to remove them in a single click.

Here is The Code to generate textbox.

    new int Top = 459;
    new int Left = 619;
    Stack<TextBox> textboxes = new Stack<TextBox>();
    List<String> controlNames = new List<String>();



 private void button17_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(376, 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(998, 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(1056, Top);
                textadd3.Name = "textBox3"   controlNames.Count;
                textadd3.Name = "textBox3"   controlCounts;
                textadd3.BringToFront();
                textadd3.Size = new System.Drawing.Size(64, 24);
                textadd3.TextAlign = HorizontalAlignment.Center;
                textadd3.Text = "";
                textadd3.TextChanged  = textBox3_TextChanged;
                textboxes.Push(textadd3);
                controlNames.Add(textadd3.Name);

                TextBox textadd4 = new TextBox();
                this.Controls.Add(textadd4);
                textadd3.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(1122, Top);
                textadd4.Name = "textBox4"   controlNames.Count;
                textadd4.Name = "textBox4"   controlCounts;
                textadd4.BringToFront();
                textadd4.Size = new System.Drawing.Size(50, 24);
                textadd4.TextAlign = HorizontalAlignment.Center;
                textadd4.Text = "";
                textadd4.TextChanged  = textBox4_TextChanged;
                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(1174, Top);
                textadd5.Name = "textBox5"   controlNames.Count;
                textadd5.Name = "textBox5"   controlCounts;
                textadd5.BringToFront();
                textadd5.Size = new System.Drawing.Size(108, 24);
                textadd5.TextAlign = HorizontalAlignment.Center;
                textadd5.Text = "";
                textboxes.Push(textadd5);
                controlNames.Add(textadd5.Name);

                Top  = 22;
            
            

        }

and Here is the Code i Use to delete Textbox the only problem is that it will delete only 1 textbox not all 5.

 public void RemoveLast()
        {
            foreach (TextBox t in textboxes)
            {
                if (t is TextBox)

                {
                    this.Controls.Remove(t);
                    t.Dispose();
                    return;
                }
         }

and i try this code also it will delete all textbox. but if the user click the add button 5times it will generate 5 row of textbox. it will work but after pressing the delete it will delete all rows not 1 at a time. i need it to be 1 at a time. if the user click it. that is why i leave it // to be check

        foreach (TextBox t in textboxes)
        {
           this.Controls.Remove(t);
           t.Dispose();

        }

        

//Remove void to execute

private void remove_Click(object sender, EventArgs e) {

        RemoveLast();
    }

CodePudding user response:

Do something like:

public void RemoveLastRowOf5()
{
    for(int i=1; i<=5 && textboxes.Count>0; i  ) {
        textboxes.Pop().Dispose();
    }
}

CodePudding user response:

well you return from you delete function after only doing one delete

        public void RemoveLast()
        {
            foreach (TextBox t in textboxes)
            {
                if (t is TextBox)

                {
                    this.Controls.Remove(t);
                    t.Dispose();
                    return; <<<<<<<======================
            

                }
           }

I think you mean

        public void RemoveLast()
        {
            foreach (TextBox t in textboxes)
            {
                if (t is TextBox)

                {
                    this.Controls.Remove(t);
                    t.Dispose();
                
                }
         }

CodePudding user response:

public void RemoveLast()
{
    int count = 0;  

    while (textboxes.Count > 0 && count < 5) // the count of TextBox that you want to delete
    {
        TextBox tb = textboxes.Pop();
        this.Controls.Remove(tb);
        
        tb.Dispose();
        count  ;  // my mistake.. I modified.
    }
}

I think you want to do this.

  •  Tags:  
  • c#
  • Related