0

So i have a class that creates a new label and textbox when it is called. The names of each label and textbox are in a continues order counting upward using a variable called i:

  • "Label" & i creates the name Label1 and "Textbox" & i creates the name Textbox1
  • i += 1

And so on. Now i want to add a procedure that deletes the last label and textbox using i. I have tried to make this procedure using the lines of code below but this doesnt work as the string cannot be converted to system.windows.forms.control :

Form1.Controls.Remove("Label" & i) Form1.Controls.Remove("Textbox" & i) i -= 1

Controls is a list of controls using the line of code: Public controls As List(Of Control)

Basically i need a way to delete a label and textbox using the variable i in the class. Any ideas? Thanks.

Dan Hayes
  • 97
  • 12

2 Answers2

1

Try...

Form1.Controls.RemoveByKey("Label" & i)
Form1.Controls.RemoveByKey("Textbox" & i)

This does not work if you have these controls tucked in GroupBox, Panel, or some other container on your form.

Shar1er80
  • 9,001
  • 2
  • 20
  • 29
0

I think you should try adding your controls into a panel, and then delete it as this link suggests (it's quite like what you tried, but inside a panel):

Removing Dynamically created controls in Panel

Community
  • 1
  • 1
ilans
  • 2,537
  • 1
  • 28
  • 29