Controling ShowDialog()

J

jp2msft

In our company's application, the employee is automatically logged out after
X minutes of inactivity.

After they have been logged out, the login screen is shown again using
ShowDialog().

If they are not able to log in as the same employee, the application starts
a new session. If the same employee logs back in, they can retain all of
their existing work.

The problem is here: If an employee is working on another application when
they are logged out and the login screen is called with ShowDialog(), the
login screen does not come to the top of every form on the desktop.

Now, my employees attempt to return to the application by selecting it from
the task tray. This brings up the program with the login screen somewhere
else, and they are unable to access the form at all.

What's the best way to correct this bug in my application?

Should I set my login form as TopMost=True?

Is there a way so that when an employee clicks the application and the login
form is in ShowDialog() mode, focus can automatically return to the login
form instead of giving them an error sound? (if so, how?)

This bug makes my application look weak because the employees using it do
not realize that another form is being called in ShowDialog() form.

Please help with any suggestions.

Thanks,
~Joe
 
I

Ignacio Machin ( .NET/ C# MVP )

In our company's application, the employee is automatically logged out after
X minutes of inactivity.

After they have been logged out, the login screen is shown again using
ShowDialog().

If they are not able to log in as the same employee, the application starts
a new session. If the same employee logs back in, they can retain all of
their existing work.

The problem is here: If an employee is working on another application when
they are logged out and the login screen is called with ShowDialog(), the
login screen does not come to the top of every form on the desktop.

Now, my employees attempt to return to the application by selecting it from
the task tray. This brings up the program with the login screen somewhere
else, and they are unable to access the form at all.

What's the best way to correct this bug in my application?

Should I set my login form as TopMost=True?

Is there a way so that when an employee clicks the application and the login
form is in ShowDialog() mode, focus can automatically return to the login
form instead of giving them an error sound? (if so, how?)

This bug makes my application look weak because the employees using it do
not realize that another form is being called in ShowDialog() form.

Please help with any suggestions.

Thanks,
~Joe

Hi,

You need to give more details about
- how you open your login screen in the middle of a session.
- How you control that if another user is logged the session is
restarted
- What window is activated when the login screen is dismissed.
- where is the "somewhere else" that the login screen is located.
- Is your login form a singleton or you create it on demand.
 
J

jp2msft

I've got an MDI Form with different Mdi Child applications.

The Login form's Background Color depends on which Mdi Child has focus.

I tried something like:

using (LoginForm login = new LoginForm())
{
if (ActiveMdiChild != null)
login.Parent = ActiveMdiChild;
else
login.Parent = this;
DialogResult dr = login.ShowDialog();
if (dr == DialogResult.OK)
{
// Code to authenticate and log them in
} else {
if (ActiveMdiChild != null)
ActiveMdiChild.Close();
}
}

If I set "login.Parent = ActiveMdiChild" I get ArgumentException: Top-level
control cannot be added to a control.

Should the parent of the login form just be the Mdi Parent?

Peter Duniho said:
[...]
Now, my employees attempt to return to the application by selecting it
from
the task tray. This brings up the program with the login screen somewhere
else, and they are unable to access the form at all.

"Somewhere else"? Where else?
What's the best way to correct this bug in my application?

Should I set my login form as TopMost=True?

Is there a way so that when an employee clicks the application and the
login
form is in ShowDialog() mode, focus can automatically return to the login
form instead of giving them an error sound? (if so, how?)

My first suggestion is to make sure you are passing the application form
to ShowDialog(), so that the modal login form has the application form as
its parent. This generally addresses most of the UI issues related to
modal dialogs.

If that's not the issue, you will need to post a concise-but-complete code
sample that reliably demonstrates the problem. It's just not clear enough
from your description what's actually going on. A modal dialog would
normally be presented to the user when that process becomes the foreground
process, so the fact that your modal dialog is "somewhere else" and not
being shown to the user is unusual. Without a code sample, it's difficult
to understand why this would happen, never mind provide details as to how
you might fix it.

Pete
 
J

jp2msft

Yeah, I thought about that right after I sent the email, so I gave it a try.
As you probably already knew, I got the same ArgumentException: Top-level
control cannot be added to a control.

I imagine the problem stems from the fact that a Timer expires, causing the
Modal Dialog box to display. If that happens whenever the application does
not have focus, problems ensue.

I have found a solution that I think is crude, but it works for now: I have
added an OnFocus event handler to the main form and made the Login form
global to the application. When the form receives focus, if the Login form is
not null, I bring it to the foreground. Further, as soon as I am finished
with the Login form, it is set to null.

LoginForm login;

void MdiForm_OnFocus(...) {
if (login != null) login.BringToFront();
}

//...
using (login = new LoginForm()) {
//...
login.ShowDialog();
//...
}
login = null;
//...

Crude example, but I don't have my project in front of me right now.

Does that look like a hack or is that reasonable?

Peter Duniho said:
[...]
Should the parent of the login form just be the Mdi Parent?

What happens when you try that instead?
 
J

jp2msft

Thanks Mr. Duniho!

I'll use "login.ShowDialog(this);" ...unless there is something I
misunderstood.

I've never used ShowDialog's IWin32Window overload, and I had to look up how
to get an IWin32Window value from MSDN just a moment ago.

As for the bad verbiage, that's something my wife fusses at me about too.
The login form was still there, but in order to find it (buried under all of
the other forms), I would have to minimize every active form one by one until
I found the login form.
 

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