How 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 straightforward:

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 instead.

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

Use the ApplicationContext Class to Fully Encapsulate Splash Screen Functionality



Downloads

Comments

  • Nice tip, Marc!

    Posted by Tom Archer on 07/14/2004 12:27am

    Definitely a '5'

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: August 14, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT Data protection has long been considered "overhead" by many organizations in the past, many chalking it up to an insurance policy or an extended warranty you may never use. The realities of today makes data protection a must-have, as we live in a data-driven society -- the digital assets we create, share, and collaborate with others on must be managed and protected for many purposes. Check out this upcoming eSeminar and join Seagate Cloud …

  • Hybrid cloud platforms need to think in terms of sweet spots when it comes to application platform interface (API) integration. Cloud Velocity has taken a unique approach to tight integration with the API sweet spot; enough to support the agility of physical and virtual apps, including multi-tier environments and databases, while reducing capital and operating costs. Read this case study to learn how a global-level Fortune 1000 company was able to deploy an entire 6+ TB Oracle eCommerce stack in Amazon Web …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds