Click here to Skip to main content
13,254,754 members (49,876 online)
Click here to Skip to main content
Add your own
alternative version


33 bookmarked
Posted 25 Jun 2004

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;}
        if (MainForm != null)
          // close the current form, but don't exit the application
        // switch to the new form

    public MainFormManager()

    // 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();

    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.


Simple but useful when you need this kind of functionality!



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


About the Author

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

Marc lives in Philmont, NY.

You may also be interested in...


Comments and Discussions

GeneralMy vote of 5 Pin
Member 1308617026-Mar-17 21:24
memberMember 1308617026-Mar-17 21:24 
QuestionHow do I maintain the size of the form if I previously resized it? Pin
smeng6-Sep-10 1:49
membersmeng6-Sep-10 1:49 
AnswerRe: How do I maintain the size of the form if I previously resized it? Pin
Marc Clifton6-Sep-10 2:53
protectorMarc Clifton6-Sep-10 2:53 
QuestionMultiple Forms Vs One Form Pin
shubie10-Aug-08 15:19
membershubie10-Aug-08 15:19 
AnswerRe: Multiple Forms Vs One Form Pin
Marc Clifton10-Aug-08 15:27
protectorMarc Clifton10-Aug-08 15:27 
GeneralRe: Multiple Forms Vs One Form Pin
shubie10-Aug-08 15:39
membershubie10-Aug-08 15:39 
GeneralSystem.ObjectDisposedException: Request for Comment Pin
MillionsterNutzer19-Feb-06 23:18
memberMillionsterNutzer19-Feb-06 23:18 
GeneralYou save me a lot of time and frustration. Pin
Roger50022-Jan-06 15:09
memberRoger50022-Jan-06 15:09 
GeneralApplicationContext & SaveFileDialog Pin
gcfischer10-Nov-04 6:09
membergcfischer10-Nov-04 6:09 
GeneralRe: ApplicationContext & SaveFileDialog Pin
stimpyjcat22-Nov-05 4:17
memberstimpyjcat22-Nov-05 4:17 
Same problem here. Is this a bug with the .NET framework?
GeneralRe: ApplicationContext & SaveFileDialog Pin
ADLER112-Oct-08 3:56
memberADLER112-Oct-08 3:56 
QuestionHow else to do SDI winform applications? Pin
w_77727-Oct-04 22:27
memberw_77727-Oct-04 22:27 
AnswerRe: How else to do SDI winform applications? Pin
ADLER112-Oct-08 3:52
memberADLER112-Oct-08 3:52 
GeneralApplication.Run() with no Form parameter Pin
Frank Hileman1-Jul-04 8:10
memberFrank Hileman1-Jul-04 8:10 
GeneralRe: Application.Run() with no Form parameter Pin
rbelknap4-Mar-10 14:05
memberrbelknap4-Mar-10 14:05 
GeneralRe: Application.Run() with no Form parameter Pin
Frank Hileman4-Mar-10 14:27
memberFrank Hileman4-Mar-10 14:27 
GeneralHandy Pin
rdafoe26-Jun-04 17:26
memberrdafoe26-Jun-04 17:26 

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

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

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