title: How to remove textboxes on button click (vb.net)?
body: <p>I am not knowing about how to delete textboxes on the click of a button in my Windows form.</p>
  
  <p>Here is my story:</p>
  
  <p>Initially, I wanted to add textboxes on the click of a button, and upon searching the net, I was able to find out, on this forum, how to do this. <a href="https://stackoverflow.com/questions/15461978/adding-new-textbox-with-button-click">https://stackoverflow.com/questions/15461978/adding-new-textbox-with-button-click</a></p>
  
  <p>I used the code that user "Rajaprabhu Aravindasam" (2nd answer) gave. Here is only part of my code that I used (in order not to confuse you):</p>
  
  <pre><code>Private Sub Button_AddTask_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_AddTask.Click
      count += 1
  
      TabPage_TaskStructure.Controls.Add(New Label() With _
                                         {.Name = "Label_Task" &amp; count})
      TabPage_TaskStructure.Controls.Add(New RichTextBox() With _
                                         {.Name = "RichTextBox" &amp; count})      
  End Sub
  </code></pre>
  
  <p>Now, as you can see, the purpose of the button 'Button_AddTask" ('+' button on my form) is to create rich textboxes and their respective label. Assume that the rich textboxes and the labels are being created below one another.</p>
  
  <p>Beside the '+' button, there is a '-' button. What I want is to use this '-' button to delete all created textboxes sequentially up. That is, if I have created 4 textboxes with the button '+', textbox no.4 will be deleted first when I click the '-' button, then no. 3 after a second click, then no.2 after a third click and so on.</p>
  
  <p>The sequential part is not a problem, I know perfectly how to do it. Here is part of the code I tried:</p>
  
  <pre><code>Private Sub Button_DeleteTask_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_DeleteTask.Click
      TabPage_TaskStructure.Controls.Remove(New Label() With _
                                         {.Name = "Label_Task" &amp; count})
      TabPage_TaskStructure.Controls.Remove(New RichTextBox() With _
                                         {.Name = "RichTextBox" &amp; count})
  
      count -= 1
  
      If count = 1 Then
          Button_DeleteTask.Visible = False
      End If
  End Sub
  </code></pre>
  
  <p>What I did, logically, was simply replace 'Add' with 'Remove', but I am not knowing with what to replace 'New'. And so my question is: What needs to be used instead of 'New'? If I need to use an entirely different code, please do tell me.</p>
  
  <p>Any help is greatly appreciated. </p>
  
  <h2>Update:</h2>
  
  <p>Ok, I have done some research since I've asked this question and I've been able to deduce that the removal of the controls (Label &amp; Rich Text Box) can be done by using a For Each loop. In my code for the '-' button, I have replaced the first 4 lines of code with this:</p>
  
  <pre><code>Dim Ctrl As Control
  For Each Ctrl In TabPage_TaskStructure.Controls
      If TypeOf Ctrl Is Label And Ctrl.Name = "Label_Task" &amp; count Then
         TabPage_TaskStructure.Controls.Remove(Ctrl)
      ElseIf TypeOf Ctrl Is RichTextBox And Ctrl.Name = "RichTextBox" &amp; count Then
          TabPage_TaskStructure.Controls.Remove(Ctrl)
      End If
  Next
  </code></pre>
  
  <p>And so, when I click the '-' button, the program checks whether each control on the tab page (TabPage_TaskStructure) is a label with the name ("Label_Task" &amp; count') or a rich text box with the name ("RichTextBox" &amp; count), and if they are, they will be removed. The rest of the code is the same.</p>
  
  <p>It's working, however not completely. It's working for the labels as they are successfully being removed, but not for the rich text boxes. I cannot understand why. I have tried the code for other controls such as date time pickers and text boxes, and it's not working for any of these either. It seems to be working for labels only.</p>
  
  <p>Also, I have tried using the code on a default rich text box, and it worked! But for rich text boxes being created at run time, it's not working.</p>
  
  <p>Can anyone clarify me on this?</p>