Click here to Skip to main content
Click here to Skip to main content
Go to top

How To Swap Top Level Forms

, 25 Jun 2004
Rate this:
Please Sign up or sign in to vote.
Use a specialized ApplicationContext to swap top level forms.

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.

Conclusion

Simple but useful when you need this kind of functionality!

References

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Marc Clifton

United States United States
Marc is the creator of two open source projets, MyXaml, a declarative (XML) instantiation engine and the Advanced Unit Testing framework, and Interacx, a commercial n-tier RAD application suite.  Visit his website, www.marcclifton.com, where you will find many of his articles and his blog.
 
Marc lives in Philmont, NY.

Comments and Discussions

 
GeneralYou save me a lot of time and frustration. PinmemberRoger50022-Jan-06 14:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140905.1 | Last Updated 26 Jun 2004
Article Copyright 2004 by Marc Clifton
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid