Dailycode.info

Short solution for short problems

Clear the controls from a .Net panel of container.

If you are working a lot with usercontrols and dynamlically adding them to the GUI, you might end up with the error saying that there are no more window handlers.

One of the reasons could be that you clear controls from your screen but they do not get discposed. Most of the time its just to many usercontrols, so add some paging or something.

But always try the following solution first. In stead of using the clear call to the panel, dispose each object in the panel. You can use this code:

    Private Shared Sub DisposeObjectsInPanel(ByVal pnl As Panel)

        For iCounter As Integer = 1 To pnl.Controls.Count Step 1

            If Not pnl.Controls(0) Is Nothing Then

                pnl.Controls(0).Dispose()

            End If

        Next

    End Sub