0

With the help of this link i made a splash screen. It works but the form which starts after the spash screen doesn't get the focus anymore. I need to activate it with a mouse click

Program.cs

static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);

    //show splash
    Thread splashThread = new Thread(new ThreadStart(
        delegate
        {
            splashForm = new FormSplashScreen();
            Application.Run(splashForm);
        }
        ));

    splashThread.SetApartmentState(ApartmentState.STA);
    splashThread.Start();

    //run form - time taking operation
    FormQueryBoxMain mainForm = new FormQueryBoxMain();
    mainForm.Load += new EventHandler(mainForm_Load);
    Application.Run(mainForm);

    // Application.Run(new FormQueryBoxMain());
}

static void mainForm_Load(object sender, EventArgs e)
{
    //close splash
    if (splashForm == null)
    {
        return;
    }

    splashForm.Invoke(new Action(splashForm.Close));
    splashForm.Dispose();
    splashForm = null;
}

Main Form

private void FormMain_Load(object sender, EventArgs e)
{
    Logging.StartLogging();  //this just starts the logging  
}   

private void FormMain_VisibleChanged(object sender, EventArgs e)
{
    if (CheckLicense())
    {
        LoadTreeViewQueries();

        //Inlog
        FormGebruikerInlog ApplicatieLogIn = new FormGebruikerInlog(this, "Inlog Gebruiker");

        ApplicatieLogIn.ShowDialog(this);  --> This lost the focus.
    }
    else
    {
        LoadLicenseForm(); //Inlog voor licentie direct starten
    }
}   

ApplicatieLogIn.ShowDialog(this); the form doesn't have the focus anymore.

First i had the code from FormMain_VisibleChanged in the FormMain_Load standing and then ApplicatieLogIn opened on top and had focus But that is not possible any more now i add the spashscreen. How can i give ApplicatieLogIn the focus back?

this.focus(); or this.actitive() in the load of the ApplicatieLogIn doesn't the trick.

Rufus L
  • 36,127
  • 5
  • 30
  • 43
Hansvb
  • 113
  • 1
  • 1
  • 13
  • VisibleChanged runs very early, before the native window is created. So when the dialog displayed by ShowDialog() closes, there are no windows left that can still get the focus. Windows has to pick one of another app. Don't use VisibleChanged. – Hans Passant Nov 19 '17 at 18:59
  • What else can use then? FormShown is the last i think when a form starts and then the form stil doen't get the focus. – Hansvb Nov 19 '17 at 19:20
  • Why is it not possible to do it in the `Form_Load` event? – Rufus L Nov 19 '17 at 19:36
  • Because my login screen apaers while the spashscreen is open. – Hansvb Nov 19 '17 at 19:48
  • I found this link but this solution has the same problem. I loose the focus on my Applcatielogin form: https://stackoverflow.com/questions/32418695/show-splash-screen-during-loading-the-main-form. – Hansvb Nov 19 '17 at 19:49

0 Answers0