Delete control at runtime

Discussion in 'Questions (Windows Mobile)' started by ohkovar, Jun 27, 2007.

  1. ohkovar

    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?
  2. Erel

    Erel Administrator Staff Member Licensed User

    When you add the buttons, add their names to an ArrayList.
    Now you will be able to check if a specific button exists.
    Use the Dispose method to delete a control.
    Something like:
    For i = 0 To ArrayList1.Count-1
  3. RandomCoder

    RandomCoder Well-Known Member Licensed User

    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.

    Last edited: Jun 27, 2007
  4. Cableguy

    Cableguy Expert Licensed 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
    next i
    If u whant u can do something upon error
    end sub
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice