The Problem
I recently had a requirement where I needed to change the top level form. The problem is, the ApplicationContext
hooks the Form
's Close
event, so that when you use the Close
method to close the current form, the application exits. This is no good!
The Solution
The solution is to implement a specialized ApplicationContext
that allows the application to close the current top level form and replace it with a different one. The implementation is quite straight forward:
using System;
using System.Windows.Forms;
namespace ApplicationContextDemo
{
public class MainFormManager : ApplicationContext
{
protected bool exitAppOnClose;
public Form CurrentForm
{
get {return MainForm;}
set
{
if (MainForm != null)
{
// close the current form, but don't exit the application
exitAppOnClose=false;
MainForm.Close();
exitAppOnClose=true;
}
// switch to the new form
MainForm=value;
MainForm.Show();
}
}
public MainFormManager()
{
exitAppOnClose=true;
}
// when a form is closed, don't exit the application if this is a swap
protected override void OnMainFormClosed(object sender, EventArgs e)
{
if (exitAppOnClose)
{
base.OnMainFormClosed(sender, e);
}
}
}
}
In the above code, assigning the CurrentForm
property to a Form
blocks the OnMainFormClosed
method from its usual operation, which is to call ExitThreadCore
.
Your main application would look something like this:
using System;
using System.Windows.Forms;
namespace ApplicationContextDemo
{
public class App
{
private static MainFormManager mainFormManager;
public static MainFormManager MainFormManager
{
get {return mainFormManager;}
}
public App()
{
mainFormManager=new MainFormManager();
mainFormManager.CurrentForm=new Form1();
Application.Run(mainFormManager);
}
[STAThread]
static void Main()
{
new App();
}
}
}
The above code instantiates the first form, and instead of the typical Application.Run(new Form1)
method, the specialized application context is provided.
To swap a form, simply assign a new Form
to the CurrentForm
property, for example:
App.MainFormManager.CurrentForm=new Form1();
I've provided a demonstration application that illustrates swapping three different top-level forms.