Delete control at runtime

ohkovar

Member
Licensed User
Is there a way to delete a control at runtime and also a way to check if a control exists?

I have a list of buttons that are populated with text from a database and I name the buttons using a loop (ex. "BUTTON" & i, this gives me a list of buttons like BUTTON1, BUTTON2, etc.)

When I re-open the form, I need to delete the old buttons (because they link to old information) and start from scratch creating new buttons. How can I just check to see if the button exists and then delete all of the buttons from the form at runtime?
 

RandomCoder

Well-Known Member
Licensed User
Longtime User
Is there a way to delete a control at run-time and also a way to check if a control exists?

You need to Dispose of the control. I'm not familiar with finding out whether the control exists in the first place though.
You could just try to Dispose of all the controls and trap/ignore the error that would be generated if the control wasn't present.
I also believe that Disposing of a Form will Dispose of all controls that were present on it. This may have changed with version 5 though??

EDIT: I replied at the samtime as Erel, sorry. As normal I prefer Erel's solution.

Regards,
RandomCoder.
 
Last edited:

Cableguy

Expert
Licensed User
Longtime User
If the control doesn't exist a error is generated so u can use errorlabel to catch that..

I've done something similar like this:

sub controlsdispose
For i= 1 to whatever you like, but be sure to be over your last button i
ErrorLabel("Error")
Control("Button"&i).dispose
next i
error:
If u whant u can do something upon error
end sub
 
Top