Form starts minimized

4.4k views Asked by At

I have 3 forms - the main form is Form1 the others are a splash form and then a login screen. Splash screen displays first and populates servers. Then, the frmAppLogin is shown, user enters a hardcoded password and result returns to form1.

public Form1()
    {
        _assembly = Assembly.GetExecutingAssembly();

        Stream icon = _assembly.GetManifestResourceStream.....

        this.Icon = new Icon(icon);

        Thread t = new Thread(new ThreadStart(SplashScreen));
        t.Start();
        InitializeComponent();
        PopulateServers();
        //Thread.Sleep(800);
        Form frmLogin1 = new frmAppLogin();
        t.Abort();

        frmLogin1.ShowDialog();


        DialogResult res = new DialogResult();
        res = frmLogin1.DialogResult;
        if (res == DialogResult.OK)
        {
            _LoggedIn = true;
        }

        else
        {
            _LoggedIn = false;
        }
 }

This is the code for form1_load:

private void Form1_Load(object sender, EventArgs e)
    {
        if (_LoggedIn)
        {
            try
            {
                blah blah........
            }
            catch
            {
                MessageBox.Show("Error accessing resources!");
            }
        }
        else
        {
            this.Close();
        }
    }

And the code for the Login form:

public frmAppLogin()
    {
        InitializeComponent();
        this.WindowState = FormWindowState.Normal;
    }


    private void btnAppLogin_Click(object sender, EventArgs e)
    {
        if (txtAppPass.Text.ToString() == requiredPass)
        {
            this.DialogResult = DialogResult.OK;
            this.Close();
        }
        else
        {
            txtAppPass.Clear();
            txtAppPass.Focus();
            MessageBox.Show("Incorrect Password");
        }
    }

Problem is when the splash screen disappears, the log in form pops up for a split second but immediately minimizes to the taskbar.

Startposition is set to CenterScreen and WindowState Normal via GUI.

Also this only happens when I run the application.exe in (or copy it from) the debug folder, I.E. it does not happen when I debug in Visual Studio 2010.

Edit: Just to add this in, I have also tried:

   private void frmAppLogin_Load(object sender, EventArgs e)
    {
        this.WindowState = FormWindowState.Normal;
    }

Which didn't help.

5

There are 5 answers

1
Hans Passant On BEST ANSWER

I'm pretty sure you are diagnosing this wrong. The dialog doesn't minimize, it disappears behind the window of another application. Usually Visual Studio, everybody maximizes it so it is really good at covering other windows. Minimize other windows to find the dialog back.

What goes wrong here is that for a split second, your app doesn't have any windows that Windows can give the focus to. Twice actually, between the splash screen and the login form. Again between the login form and your main form. The Windows window manager is forced to find another window to focus and, since you don't have any candidates, it will pick the window of another app.

The window manager has allowance to give an app time to create its first window, that inevitably takes time. Your splash screen no doubt messes up that logic. The code is not easy to repair as posted, the standard tricks to invoke code after calling ShowDialog() won't work because your app is not yet pumping a message loop. Which in itself is a problem. Start tackling this problem at least by fixing the splash screen, .NET already has solid support for them built-in. Answer is here.

0
Jayant Varshney On

I know I am answering too late. I got the same issue and read this post and tried to figure out solution.

I have created a Timer object and set its Interval = 10 and started the Timer on the Form Load event. Then opened the dialog box on timer's Tick event and stopped the timer and shown the login.showDialog(). It works fine for me. See the code below.

        private Timer dispatcher;

        public MainView()
        {
            InitializeComponent();
            frmMdiChildList = new List<Form>();
            Load += new EventHandler(MainView_Load);
            FormClosing += new FormClosingEventHandler(MainView_FormClosing);
            Activated += new EventHandler(MainView_Activated);

        }

        void MainView_Load(object sender, EventArgs e)
        {
            dispatcher = new Timer();
            dispatcher.Interval = 10; //10 milliseconds
            dispatcher.Tick += new EventHandler(dispatcher_Tick);
            dispatcher.Start();
        }

        void dispatcher_Tick(object sender, EventArgs e)
        {
            dispatcher.Stop();
            dispatcher = null;
            showLoginForm();
        }

        private void showLoginForm() {
            Form loginForm = new LoginView();
            loginForm.ShowDialog();
        }
0
Neno On

For those who will have a problem that form pops up for a split of second but immediately minimizes to the taskbar, here is a possible solution:

FormNew f = new FormNew();
f.Owner = this; // - this is the solution line
f.Show();
0
Ankit Srivastava On
    t.Start();
    InitializeComponent(); 
    PopulateServers();
    Form frmLogin1 = new frmAppLogin();
    this.Activate(); //This Line will Solve your problem
    t.Abort();
0
yaboi On

I had this same issue with a program I was working on. Here is what I did to get around it. Seems to work well.

In my main form, I setup constructor and StartForm method as follows:

public FrmMain()
{
    Thread thread = new Thread(new ThreadStart(StartForm));
    thread.Start();
    Thread.Sleep(7000);
    InitializeComponent();
    thread.Abort();
}

private void StartForm()
{
    Application.Run(new FrmSplash());
}

Then in my main form, I setup the FrmMain_Load event as follows:

private void FrmMain_Load(object sender, EventArgs e)
{
    Activate(); // Give focus to FrmMain and maximize window
}

Done!