Mark Roworth
Mark Roworth

Reputation: 566

How do I close application after close of the last form?

I am working in C#. I am starting up in Program.Main. This opens frmSplash. frmSplash does a whole load of initialisation (Errs collection/database connection etc) and then opens frmGeneric. frmGeneric loads a bunch of information from the database and populates itself with controls as defined in the database. It might open other instances of frmGeneric. In fact, it may close itself before the other instances are closed.

The initialisation process occurs in frmSplash once it is visible, when the user clicks a 'Continue' button. At the moment, once the first instance of frmGeneric is shown, I call this.Hide() in frmSplash, but I actually want frmSplash to unload.

If I call this.Close() in frmSplash, even after frmGeneric is shown, the entire app closes.

Obviously, the last frmGeneric to close won't know that it is the last one (it's generic). How do I close down frmSplash after initialisation, without quitting the app?

private void cmdContinue_Click(object sender, EventArgs e)
{
    Globals oG = null;
    App oApp = null;
    frmGeneric oForm = null;

    try
    {
        txtStatus.Text = "Initialising Globals object...";
        oG = new Globals();

        // some other stuff redacted 
        txtStatus.Text = "Showing startup form...";
        oForm = new frmGeneric();
        oForm.Globals = oG;
        if (!oForm.RunForm() throw new Exception("Could not run form");

        // enough of me
        this.Close();
    }

    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
        Application.Exit();
    }
}

At the this.Close() in the above code, the entire app closes down, even though oForm has been loaded and is visible.

Upvotes: 1

Views: 974

Answers (1)

Reza Aghaei
Reza Aghaei

Reputation: 125277

There are two points in the question:

  1. Showing Splash Screen
  2. Close Application after closing the last form (not the main form).

For both requirements, you can rely on WindowsFormsApplicationBase which exists in Microsoft.VisualBasic.dll.

  • It allows you to specify a splash screen to show at startup of the application.
  • It also allows you to specify a shutdown style to close application after closing main form or close application after closing all forms.

Example

using System;
using System.Windows.Forms;
using Microsoft.VisualBasic.ApplicationServices;
static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(true);
        var app = new MyApplication();
        app.Run(Environment.GetCommandLineArgs());
    }
}
public class MyApplication : WindowsFormsApplicationBase
{
    public MyApplication()
    {
        this.ShutdownStyle = ShutdownMode.AfterAllFormsClose;
    }
    protected override void OnCreateMainForm()
    {
        MainForm = new YourMainForm();
    }
    protected override void OnCreateSplashScreen()
    {
        SplashScreen = new YourSplashForm();
    }
}

Upvotes: 1

Related Questions