1

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.

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
Mark Roworth
  • 409
  • 2
  • 15
  • Try calling Visibile property instead of this.Close or this.Hide and check if it works. So basically it goes like this: frmSplash.Visible=false; // where frmSplash is the Splash Screen you wish to hide. – Rohan Rao Nov 23 '19 at 08:57
  • @Rohan - Well, the behaviour doesn't change with this, and I'm not surprised. Setting the visible property of frmSplash to false won't unload the form. I want to unload frmSplash and leave frmGeneric loaded. However, calling frmSplashClose() closes the application. – Mark Roworth Nov 23 '19 at 09:47
  • [Splash Screen waiting until thread finishes](https://stackoverflow.com/a/393870/7444103) – Jimi Nov 23 '19 at 10:42

1 Answers1

1

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();
    }
}
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398