Splash screen does not return focus to main form

妖精的绣舞 提交于 2019-12-05 03:48:20

The question is not in detail.

1) What the is the relationship between the SplashScreen and the main form of the application?

2) How does SplashScreen automatically close?

I'm sure the problem here is that the main form had already started loading up in the background while SplashScreen is yet to close. Due to bad timing, the main form loads up in the background and the SplashScreen unloads... hence the flash in the taskbar.

Make them appear in serial controlled manner. There are many ways. I cannot suggest exactly how since hardly any detail has been provided. Like what is VB doing in the project, how many threads are working, what are the custom forms used here.. etc.

EDIT:

Algorithm to implement Splash screen (IMHO) :)

1) Create a custom form - splash screen

2) Run it on a separate thread. Implement it's behaviour as you like.

3) In your splash screen form, write a handler to capture a custom unload event handler which closes the splash screen form.

4) Now, back in the main thread, create you main app form. Set its Visible property to false.

5) Write even handler of the main form's Load event. In this handler, fire an event to splash screen to unload. Then, make the main form visible.

You need to call the Activate() method on your main form in order to display it properly.

Jonathan Crossland

In some of my implementations of splash forms on a different thread, I've seen this situation as well.

usually if I call this.Activate(); as my first line of code in the form load it tends to work as expected. putting this after hiding the splash usually doesnt work correctly.

other methods that can help.

this.BringToFront();
this.TopMost = true; //setting this to true and back to false sometimes can help

What if you set the focus explicitly in your Form1 Load event?

For some obscure reason, focus seems to be working correctly. I am going to try and incorporate more of my normal execution of my main program into the MyMainForm and try to find what really causes the focus change to fail. I will keep you all posted.

SlickJayD

try calling form1.show() after this.MainForm = new Form1(); It worked for me.

I had the same issue and wanted easy solution for this.

I tried above but no luck, but ideas from above useful information.

I tried my trick and it works for me.

This is my Programs.cs code which works fine:

static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        var mainWindow = new frmMain();
        splashScreen.DisplaySplashScreen(mainWindow, 10000);
        **mainWindow.TopMost = true;**
        Application.Run(mainWindow);
    }
}

Notice the mainWindow.TopMost = true;

Ok that was working enough but I did not want the mainWindow to stay on top.

So in Shown event i added this.TopMost = false;

    private void frmMain_Shown(object sender, EventArgs e)
    {
        this.TopMost = false;
    }

Now my application is working as I wanted it to work.

Note: Just posted to help any other user who face the same issue what I faced.

Happy Coding :)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!