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
Re: Dispose all usercontrols...
Do the rest of userDisplaySet controls reside in the tableDisplaySet container?
Re: Dispose all usercontrols...
Yes, they do.
I am adding the userControl using the following code:
Code:
Me.tableDisplaySet.Controls.Add(New userDisplaySet())
Re: Dispose all usercontrols...
your code iterates through all controls of tableDisplaySet, not just userDisplaySet.
Try it:
vb.net Code:
For Each ctl As userDisplaySet In Me.Controls.OfType(Of userDisplaySet)()
ctl.Dispose
Next
Re: Dispose all usercontrols...
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
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
Re: Dispose all usercontrols...
Kris,
The tolist does not exist and is giving me an error?
Thanks
Simon
Re: Dispose all usercontrols...
To clear the controls collection:
Code:
Me.tableDisplaySet.Controls.Clear
Re: Dispose all usercontrols...
Thanks Cicatrix,
So simple
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'.