Results 1 to 11 of 11

Thread: Dispose all usercontrols...

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2006
    Posts
    170

    Dispose all usercontrols...

    I had a number of userControls added to a tableLayout, what I want to do is dispose of all userControls however if I use the following code it only seems to dispose of the first userControl and not the rest:

    Code:
                For Each myControl As userDisplaySet In Me.tableDisplaySet.Controls
                    ' Closes control
                    myControl.Dispose()
                Next
    Thanks

    Simon

  2. #2

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    May 2006
    Posts
    170

    Re: Dispose all usercontrols...

    Yes, they do.

    I am adding the userControl using the following code:

    Code:
    Me.tableDisplaySet.Controls.Add(New userDisplaySet())

  4. #4
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: Dispose all usercontrols...

    your code iterates through all controls of tableDisplaySet, not just userDisplaySet.

    Try it:

    vb.net Code:
    1. For Each ctl As userDisplaySet In Me.Controls.OfType(Of userDisplaySet)()
    2.    ctl.Dispose
    3. Next

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    May 2006
    Posts
    170

    Re: Dispose all usercontrols...

    OfType gives me an error

  6. #6
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: Dispose all usercontrols...

    That's because not all versions have it... put this in your loop instead, and change the type to control:
    Code:
    For Each myControl As Control In Me.tableDisplaySet.Controls
         If TypeOf ctl Is userDisplayList Then
              Me.tableDisplaySet.Controls.Remove(ctl)
         End If
    Next

  7. #7
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,390

    Re: Dispose all usercontrols...

    if you remove the first item from a list and then move to the next item it will may not work correctly

    Try using a reverse loop through the list or use: For Each myControl As userDisplaySet In Me.tableDisplaySet.Controls.tolist

    Let me know
    Kris

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    May 2006
    Posts
    170

    Re: Dispose all usercontrols...

    Kris,

    The tolist does not exist and is giving me an error?

    Thanks

    Simon

  9. #9

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    May 2006
    Posts
    170

    Re: Dispose all usercontrols...

    Thanks Cicatrix,

    So simple

  11. #11
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: Dispose all usercontrols...

    Clearing the Controls collection is not the same as disposing every control in the collection. Clearing will only remove the references to the controls from the collection, but the controls will still exist 'out there'.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width