Reputation: 75
I have this program that creates control(textboxes, progressbars, labels, timers) dynamically. Now I created a button that when clicked, will erase the created controls on the form. What's the code to this?
Upvotes: 1
Views: 4130
Reputation: 46
Like @Jens said above
For each tb as TextBox in burstbox
burstbox.Remove(tb)
tb.Dispose()
Next
Upvotes: 0
Reputation: 6375
When creating the controls keep the references around. For example you can use a list as a global variable.
Dim MyControls as List(Of Control)
When you create the controls you add them to the form's control collection and also to the list
MyControls = New List(Of Control)
[...]
Me.Controls.Add(NewControl)
MyControls.Add(NewControl)
Do delete the controls you remove them from the form and dispose them (free ressources)
For each c as Control in MyControls
Me.Controls.Remove(c)
c.Dispose()
Next
You can do this because controls are reference types. That means the objects in both the MyControls
list and the ones displayed on the form point to the same instance and you can therefore dispose them easily afterwards.
Upvotes: 3