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.
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.