Modal form gets separated from app

  • Thread starter Thread starter Andrew
  • Start date Start date
A

Andrew

I am having problems with modal windows going behind the main form and
confusing users. I have found the simplest way to simulate this in VS by
putting a break point on a line to take focus away from the app.

When you run the code below and put a break point on the dummy statement and
when it goes into debug mode press F5 to continue the app you will see that
the message box is somehow separate from the main app. You can click on the
Windows task bar to select the app and the message box then goes behind.

This is all caused by the DoEvents statement, if you take this out it works
fine.

My question is what is the DoEvents statement doing to mess things up and
how can I fix this (without removing the DoEvents statement)?


Put this code inside a button click event on the main form:

// Put a break point on the code below
int Dummy = 1;

Application.DoEvents();

MessageBox.Show("This may now go behind main window");


Thanks for any help

Andrew
 
To answer my own question, you have to pass the current form as the form
owner.

MessageBox.Show(this, "message");
 
Andrew said:
I am having problems with modal windows going behind the main form and
confusing users. I have found the simplest way to simulate this in VS
by putting a break point on a line to take focus away from the app.

When you run the code below and put a break point on the dummy statement
and when it goes into debug mode press F5 to continue the app you will
see that the message box is somehow separate from the main app. You can
click on the Windows task bar to select the app and the message box then
goes behind.

This is all caused by the DoEvents statement, if you take this out it
works fine.

My question is what is the DoEvents statement doing to mess things up
and how can I fix this (without removing the DoEvents statement)?


Put this code inside a button click event on the main form:

// Put a break point on the code below
int Dummy = 1;

Application.DoEvents();

MessageBox.Show("This may now go behind main window");


Thanks for any help

Andrew

It's an interesting issue, but:

1) the dummy = 1 line has nothing to do with it. It occurs when
breaking on the Application.DoEvents() line without the dummy line.
2) It's not as if the main form can be interacted with now. In other
words the messagebox still is the only one that can be interacted.
3) There is no issue outside the debugger.
4) There is almost never a valid reason for Application.DoEvents.
 
Back
Top