Modal form doesn't get the focus....

  • Thread starter Albert Tollkuçi
  • Start date
A

Albert Tollkuçi

Hi guys,
The MainForm of my application is not visible at startup. In the contructor
I show a modal dialog (the login form) and I want to set the focus to this
form. I have tried a lot of options but it doesn't work.
Any help is appreciated.

Thanx,
Albert
 
S

Stoitcho Goutsev \(100\) [C# MVP]

Hi Albert,
What exactly your concerns are. If you do that at the begining only the
login form is visible that is because you haven't start the application
message loop yet, but the login form is opened with ShowDialog and it has to
be active and working.

Can you post some sample code demonstrating your problem.
 
A

Albert Tollkuçi

Main method:
***************************************************************
Application.CurrentCulture = new System.Globalization.CultureInfo("de-DE",
false);

Splasher.Show();

MainForm frm = null;

try

{

frm = new MainForm();

}

catch

{

Splasher.Close();

System.Windows.Forms.MessageBox.Show("Anmeldung an die Datenbank nicht
möglich. Bitte wenden Sie sich an Ihren Systemadministrator.",
Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);

Application.Exit();

return;

}

UnhandledExceptionHandler ueh = new UnhandledExceptionHandler();

Application.ThreadException += new
System.Threading.ThreadExceptionEventHandler(ueh.OnThreadException);

System.AppDomain.CurrentDomain.UnhandledException += new
UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

Application.Run(frm);

*************************************************************************

In MainForm cosntructor:
*********************************************************************
// Update splash screen

Splasher.Status = "Lesung Default-Werte...";

// Load dynamic modules

this.FindPlugins(System.IO.Path.GetDirectoryName(Application.ExecutablePath)
, "EloSoft.Windows.Forms.IPlugin");

Splasher.Status = "Module laden...";

// Read Default Values

ReadDefaultValues();

// Update splash screen

Splasher.Status = "Öffnung Logon-Form...";

// Create the Login form

Login frmLogin = new Login();

// Close the splash screen

Splasher.Close();

// Show Login form

frmLogin.ShowDialog();

if(this.userInfo != null)

{

// Add the Startup Page

TransportSearch frm;

try

{

frm = new TransportSearch();

}

catch(Exception ex)

{

EloSoft.Windows.Forms.MessageBox.Show(this, Application.ProductName,
EloSoft.Windows.Forms.Form.GetMessageText(EloSoft.Windows.Forms.ErrorMessage
s.CannotOpenForm), ex);

return;

}

AddNewTab(frm);

this.GetTemplates(10);

this.Visible = true;

this.docs.FocusedDocument = this.docs.TabStrips[0].Documents[0];

this.docs.FocusedDocument.Control.Focus();

}

****************************************************************************
**********8

Stoitcho Goutsev (100) said:
Hi Albert,
What exactly your concerns are. If you do that at the begining only the
login form is visible that is because you haven't start the application
message loop yet, but the login form is opened with ShowDialog and it has to
be active and working.

Can you post some sample code demonstrating your problem.

--
B\rgds
100 [C# MVP]

Albert Tollkuçi said:
Hi guys,
The MainForm of my application is not visible at startup. In the contructor
I show a modal dialog (the login form) and I want to set the focus to this
form. I have tried a lot of options but it doesn't work.
Any help is appreciated.

Thanx,
Albert
 
S

Stoitcho Goutsev \(100\) [C# MVP]

Hi Albert,
From what I see you shouldn't see the slpash form, but you should see the
Login form. Is it what you see. And what is your problem again? Is it the
splash form?

--
B\rgds
100 [C# MVP]
Albert Tollkuçi said:
Main method:
***************************************************************
Application.CurrentCulture = new System.Globalization.CultureInfo("de-DE",
false);

Splasher.Show();

MainForm frm = null;

try

{

frm = new MainForm();

}

catch

{

Splasher.Close();

System.Windows.Forms.MessageBox.Show("Anmeldung an die Datenbank nicht
möglich. Bitte wenden Sie sich an Ihren Systemadministrator.",
Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);

Application.Exit();

return;

}

UnhandledExceptionHandler ueh = new UnhandledExceptionHandler();

Application.ThreadException += new
System.Threading.ThreadExceptionEventHandler(ueh.OnThreadException);

System.AppDomain.CurrentDomain.UnhandledException += new
UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

Application.Run(frm);

*************************************************************************

In MainForm cosntructor:
*********************************************************************
// Update splash screen

Splasher.Status = "Lesung Default-Werte...";

// Load dynamic modules

this.FindPlugins(System.IO.Path.GetDirectoryName(Application.ExecutablePath)
, "EloSoft.Windows.Forms.IPlugin");

Splasher.Status = "Module laden...";

// Read Default Values

ReadDefaultValues();

// Update splash screen

Splasher.Status = "Öffnung Logon-Form...";

// Create the Login form

Login frmLogin = new Login();

// Close the splash screen

Splasher.Close();

// Show Login form

frmLogin.ShowDialog();

if(this.userInfo != null)

{

// Add the Startup Page

TransportSearch frm;

try

{

frm = new TransportSearch();

}

catch(Exception ex)

{

EloSoft.Windows.Forms.MessageBox.Show(this, Application.ProductName,
EloSoft.Windows.Forms.Form.GetMessageText(EloSoft.Windows.Forms.ErrorMessage
s.CannotOpenForm), ex);

return;

}

AddNewTab(frm);

this.GetTemplates(10);

this.Visible = true;

this.docs.FocusedDocument = this.docs.TabStrips[0].Documents[0];

this.docs.FocusedDocument.Control.Focus();

}

****************************************************************************
**********8

Stoitcho Goutsev (100) said:
Hi Albert,
What exactly your concerns are. If you do that at the begining only the
login form is visible that is because you haven't start the application
message loop yet, but the login form is opened with ShowDialog and it
has
to
be active and working.

Can you post some sample code demonstrating your problem.

--
B\rgds
100 [C# MVP]

Albert Tollkuçi said:
Hi guys,
The MainForm of my application is not visible at startup. In the contructor
I show a modal dialog (the login form) and I want to set the focus to this
form. I have tried a lot of options but it doesn't work.
Any help is appreciated.

Thanx,
Albert
 
E

Elp

Hi,

Albert Tollkuçi said:
Hi guys,
The MainForm of my application is not visible at startup. In the contructor
I show a modal dialog (the login form) and I want to set the focus to this
form. I have tried a lot of options but it doesn't work.
Any help is appreciated.

You forgot to mention that a splash form was involved....

I've done exactly the same thing as you did: splash form -> login form ->
main form. And ran into the same problem as you.

How is your splash form implemented? In my case, the problem was that once
the splash form was closed, my application lost the focus (as it had no more
windows diplayed) and therefore, the login form that i displayed after the
splash form was closed was not focused.

My workaround for this problem was to close the splash form in the Login
form load function instead of closing it before displaying the login form. I
used this code:
private void LoginForm_Load(object sender, System.EventArgs e)

{

this.Activate(); //to set the focus to the login form

SplashControl.closeSplash(); //well, you guesed that this closes the splash
form....

}
 
A

Albert Tollkuçi

Thanx a lot, that did the trick....:)
I think you guessed that the Splash form is implemented in another thread
and another message loop...
 
A

Albert Tollkuçi

Elp solution worked...
Anyway, the splash screen works as expected, but the login form didn't get
the focus...


Stoitcho Goutsev (100) said:
Hi Albert,
From what I see you shouldn't see the slpash form, but you should see the
Login form. Is it what you see. And what is your problem again? Is it the
splash form?

--
B\rgds
100 [C# MVP]
Albert Tollkuçi said:
Main method:
***************************************************************
Application.CurrentCulture = new System.Globalization.CultureInfo("de-DE",
false);

Splasher.Show();

MainForm frm = null;

try

{

frm = new MainForm();

}

catch

{

Splasher.Close();

System.Windows.Forms.MessageBox.Show("Anmeldung an die Datenbank nicht
möglich. Bitte wenden Sie sich an Ihren Systemadministrator.",
Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);

Application.Exit();

return;

}

UnhandledExceptionHandler ueh = new UnhandledExceptionHandler();

Application.ThreadException += new
System.Threading.ThreadExceptionEventHandler(ueh.OnThreadException);

System.AppDomain.CurrentDomain.UnhandledException += new
UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

Application.Run(frm);

*************************************************************************

In MainForm cosntructor:
*********************************************************************
// Update splash screen

Splasher.Status = "Lesung Default-Werte...";

// Load dynamic modules
this.FindPlugins(System.IO.Path.GetDirectoryName(Application.ExecutablePath)
, "EloSoft.Windows.Forms.IPlugin");

Splasher.Status = "Module laden...";

// Read Default Values

ReadDefaultValues();

// Update splash screen

Splasher.Status = "Öffnung Logon-Form...";

// Create the Login form

Login frmLogin = new Login();

// Close the splash screen

Splasher.Close();

// Show Login form

frmLogin.ShowDialog();

if(this.userInfo != null)

{

// Add the Startup Page

TransportSearch frm;

try

{

frm = new TransportSearch();

}

catch(Exception ex)

{

EloSoft.Windows.Forms.MessageBox.Show(this, Application.ProductName,
EloSoft.Windows.Forms.Form.GetMessageText(EloSoft.Windows.Forms.ErrorMessage
s.CannotOpenForm), ex);

return;

}

AddNewTab(frm);

this.GetTemplates(10);

this.Visible = true;

this.docs.FocusedDocument = this.docs.TabStrips[0].Documents[0];

this.docs.FocusedDocument.Control.Focus();

}
****************************************************************************
**********8

Stoitcho Goutsev (100) said:
Hi Albert,
What exactly your concerns are. If you do that at the begining only the
login form is visible that is because you haven't start the application
message loop yet, but the login form is opened with ShowDialog and it
has
to
be active and working.

Can you post some sample code demonstrating your problem.

--
B\rgds
100 [C# MVP]

Hi guys,
The MainForm of my application is not visible at startup. In the
contructor
I show a modal dialog (the login form) and I want to set the focus to
this
form. I have tried a lot of options but it doesn't work.
Any help is appreciated.

Thanx,
Albert
 
J

James Hancock

This is actually a bug in Windows API and has been since Windows 3.x How it
made it into Windows 95 is beyond me, but it lives to this day. You can
really magnify this problem (they're related if you watch the wndprc) by
opening a Dialog and then opening another Dialog on top of that. In .net
closing the top most dialog closes the parent dialog too at the same time if
you return a positive DialogResult. In VB6 the same thing results in the
application losing focus to the next application in the task list. (You can
also get this behaviour with .net too, but you have to work a little harder
and use MDI children to do it.)

I reported this in V1 of the .net framework and V1.1 and now V2 and every
time it's been closed as not fixable.

James Hancock
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top