简体   繁体   English

删除按钮上的文本框/标签,单击C#

[英]Removing textbox/label on button click C#

I am trying to remove textboxes and labels one by one by pressing a button. 我试图删除文本框并通过按一个按钮一个标签。 I have a list of textboxes called inputTextBoxes. 我有一个名为inputTextBoxes的文本框列表。

Here is the code for adding : 这是添加的代码:

private void onClickAdd(object sender, EventArgs e)
    {
        inputTextBoxes = new List<TextBox>();
        Label label1 = new Label();
        label1.Name = "label1";
        label1.Text = "w" + i;
        label1.Location = new System.Drawing.Point(5, 10 + (20 * i));
        label1.Size = new System.Drawing.Size(30, 20);
        this.Controls.Add(label1);

            TextBox text1 = new TextBox();
            text1.Name = "text1";
            text1.Location = new System.Drawing.Point(35, 10 + (20 * i));
            text1.Size = new System.Drawing.Size(25, 20);
            inputTextBoxes.Add(text1);
            this.Controls.Add(text1);
            i++;
    }

For removing I am trying this : 对于删除,我正在尝试这样做:

 private void onClickRemove(object sender, EventArgs e)
    {
       foreach(TextBox text1 in inputTextBoxes)
        {
            this.Controls.Remove(text1);
        }

    }

But it removes only the last textbox added,clicking againg on the button doesn't do anything. 但是它仅删除最后添加的文本框,再次单击按钮没有任何作用。

You are constantly creating a new list in your OnClickAdd() method: 您将在OnClickAdd()方法中不断创建一个新列表:

inputTextBoxes = new List<TextBox>();

Try to check if the inputTextBoxes is null and only then do this line of code. 尝试检查inputTextBoxes是否为null,然后再执行此行代码。 Otherwise, just let the rest of the code run. 否则,只需运行其余代码即可。 Also, remember about clearing the inputTextBoxes list after the onClickRemove() method finishes removing textboxes/labels. 另外,请记住在onClickRemove()方法完成删除文本框/标签后清除inputTextBoxes列表的操作。

You want to remove only one TextBox at a time, why do you need a foreach loop? 您一次只想删除一个TextBox,为什么需要一个foreach循环? just grab the last or first TextBox and if it is not null remove it from the Controls: 只需抓住最后一个或第一个TextBox,如果它不为null,则从控件中将其删除:

 private void onClickRemove(object sender, EventArgs e)
 {
     var textBoxToRemove = inputTextBoxes.LastOrDefault();
     // or
     // var textBoxToRemove = inputTextBoxes.FirstOrDefault();
     if (textBoxToRemove != null)
     {
         this.Controls.Remove(textBoxToRemove);
         inputTextBoxes.Remove(textBoxToRemove);
     }
 }

Make sure you remove it from inputTextBoxes also so the next time you will ask to remove a TextBox it will not try to remove it again and go on to the next one. 确保也将其从inputTextBoxes中删除,因此下次您将要求删除TextBox时,它将不会尝试再次将其删除并继续进行下一个操作。

Edit 编辑

@Piotr Nowak has pointed one more problem you have, you allocate a new list for inputTextBox every time you add a new TextBox, you should allocate the list only once when you create your class. @Piotr Nowak指出了您遇到的另一个问题,每次添加新的TextBox时都要为inputTextBox分配一个新列表,创建类时只应分配一次列表。

Remove this from onClickAdd method: 从onClickAdd方法中删除它:

inputTextBoxes = new List<TextBox>();

And use this when you declare the list as a field it your class: 并将列表声明为您的类的字段时使用此方法:

private readonly inputTextBoxes = new List<TextBox>();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM