Deleting controls

W

WH

I put a few controls on a form at runtime like
Dim bt as Button
For n = to to 5
bt = new Button
...
Me.Controls.add(bt)
Next

How can I again delete them at runtime? I tried
For each bt in Me.controls
bt.dispose()
Next

But only a few controls disappear, some stay on the form.
bt = Nothing() does'nt do the job either
Is there a way to completely discard then from the form and from memory?
Thanks
WH
 
M

Martin

You could add them to a collection as they are created and then refer to the
collection to remove them from the form later. I'm sure there are other ways
as well but this would work.

HTH

Martin
 
D

davidhosking

Hi WH,

You must remember to remove them from the form before disposing them.
So just before you dispose them, use me.controls.remove(bt).

Regards
 
C

Cor Ligthert [MVP]

Willy,

You add the buttons to the controls with Add, what command do you think
there is to Remove them from the Controls?

Keep in mind that if you remove buttons from a parent control that you do it
bottom up because the index is everytime regenerated.

It depends on were you created the buttons when they will be garbaged, but
normally you can forget to take actions in that because the Garbage
Collector does that.

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top