Ahmed Cholluyev
Ahmed Cholluyev

Reputation: 41

How to remove controls from a form?

I have a Label, a Textbox and button3, button4 buttons in my Form.

On each click button3 adds new Label and Textbox under the existing ones.

I want to make button4 click to remove previously added Label and TextBox. How can I access to the last value of index? Here is code snippet:

public partial class Form4 : Form
{
    int index = 1;

    public Form4()
    {
        InitializeComponent();
    }

    private void button3_Click(object sender, EventArgs e)
    {
        Label l = new Label();
        TextBox t = new TextBox();
        t.Name = "txt" + index;
        l.Text = label2.Text;
        l.Width = label2.Width;
        t.Width = textBox2.Width;
        l.Name = "lbl" + index;
        t.Location = new Point(textBox2.Location.X, textBox2.Location.Y + 30 * index);
        l.Location = new Point(label2.Location.X, label2.Location.Y + 30 * index);
        if (index >8)
        {
            button3.Enabled =false;
        }
        button1.Location = new Point(button1.Location.X, button1.Location.Y + 30);
        button2.Location = new Point(button2.Location.X, button2.Location.Y + 30);
        this.Controls.Add(l);
        this.Controls.Add(t);
        this.Height += 30;
        index++;
    }
}

Upvotes: 0

Views: 1281

Answers (4)

ASh
ASh

Reputation: 35646

remove controls by name

private void button4_Click(object sender, EventArgs e)
{
    if (index > 1)
    {
        index--;
        this.Controls.RemoveByKey("txt" + index);
        this.Controls.RemoveByKey("lbl" + index);

        this.Height -= 30;
    }
}

RemoveByKey is a method of ControlCollection, which removes the child control with the specified key


@LarsTech suggested in a comment that removed controls should be also disposed:

private void button4_Click(object sender, EventArgs e)
{
    if (index > 1)
    {
        index--;
        // get a textBox by name
        Control c = Controls["txt" + index];
        // remove from form
        Controls.Remove(c);
        // clean up
        c.Dispose();

        // repeat for label
        c = Controls["lbl" + index];
        Controls.Remove(c);
        c.Dispose();

        Height -= 30;
    }
}

Upvotes: 1

Abdelmneim Hussein
Abdelmneim Hussein

Reputation: 51

The index variable already have the last value. You don't need to track it. use this.Controls.RemoveByKey();

Upvotes: 0

Ahmad
Ahmad

Reputation: 1524

Create a Stack, when you add a control Push the control to the Stack. When you want to remove a Control, Peek the Stack to get a reference to the last created Control, next remove the Control and finally Pop the Stack.

Sample code would look as such (draft):

  private Stack<Control> _Stack = new Stack<Control>();

  private void AddControlEventHandler(object sender, EventArgs e)
  {
     this.Controls.Add(addedControl);
     _Stack.Push(addedControl);
  }

  private void RemoveControlEventHandler(object sender, EventArgs e)
  {
     //Get a reference to the last added control from the stack.
     var lastAddedControl = _Stack.Peek();
     //Remove the control
     this.Controls.Remove(lastAddedControl);
     //Remove the reference to the control from the stack
     _Stack.Pop();
  }

Upvotes: 1

sm.abdullah
sm.abdullah

Reputation: 1802

you can do something like this.

 //get reference control
   var control = this.Controls[this.Controls.IndexOfKey("controlName")];
  this.Controls.Remove(control);
  //now dispose if do need it any more
  control.Dispose();

Upvotes: 0

Related Questions