Microsoft & .NETVisual BasicUnloading All Forms in Visual Basic

Unloading All Forms in Visual Basic content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

How do you close your Visual Basic program? Do you use the End method? In this tip I will explain why using the End method is not the best way to close your program. It is an undocumented fact that using the End method does not completely remove all forms from memory. This means that your program does not give up all resources to other processes when it is closed using the end method. The only way to completely shut down the program is to unload each of the forms using the Unload method. Here is some code that loops through all loaded forms and unloads them one by one. As soon as no forms are loaded, your program has been closed.

Public Sub UnloadAllForms()
Dim Form As Form
   For Each Form In Forms
      Unload Form
      Set Form = Nothing
   Next Form
End Sub

It is best to call this procedure from the unload event of your main form.

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories