How to Dispose a Form in C# without closing the Application

As a good programmer you should be thinking about the resources your software uses and be as gentle as possible on the Computer’s memory.

With C# you would be probably clearing the objects you no longer need from the memory using the Dispose method.

But if you dispose a Windows Form in C#, the whole application will quit! WHY?

That’s because by default C# select a start up form defined in the Program.cs file like this:

Application.Run(new Form1());

So to fix this just delete the “new Form1()” , and load up an instance of the form you want from Main() in the Program.cs so the whole thing will look like:

Form1 frm1 = new Form1();
frm1.Show();
Application.Run();

Now you can load and dispose forms all you want without the application quiting on you:)

Share