Problem displaying modal dialog boxes

G

Guest

I'm following the example from the MSDN library on how to create modal dialog
boxes in C#, but something isn't working properly. If I create a modal
dialog box and show it using ShowDialog(), the main form behind the dialog
box is still enabled and can operate normally. If I use MessageBox.Show() to
show a message box, the main form is disabled until the dialog box
terminates. How can I make a custom dialog box do this?
 
L

Linda Liu [MSFT]

Hi,
Thank you for posting. From your post, my understanding on this issue is:
You can not make a Windows form show in modal mode. If I¡¯m off base,
please feel free to let me know.
If you want to show a Windows form in modal mode, you should use
ShowDialog method to show it. There is an example. There¡¯re two forms ¨C
Form1 and Form2. Form1 is the Main Form. There¡¯s a button on Form1. You
can open Form2 if you click the button. To show Form2 in modal mode, the
code in Form1 should be:
private void button1_Click(object sender, EventArgs e)
{
Form2 frm = new Form2();
frm.ShowDialog(this);
}
The ShowDialog method has an optional argument, owner, that can be used to
specify a parent-child relationship for a form. Parent-child relationship
means that if the parent form is minimized, the child form will be
minimized too.
Please let me know if you have any other concerns, or need anything else.



Sincerely,
Linda Liu
Microsoft Online Community Support

====================================================
When responding to posts,please "Reply to Group" via
your newsreader so that others may learn and benefit
from your issue.
====================================================
 
G

Guest

Hi, Linda. Thanks for your response. The code you provided is exactly what
I saw in the MSDN Library documentation. I tried that, and it does work as a
modal dialog box - i.e. the code immediately after ShowDialog() doesn't
execute until you close the dialog box. However, the main form is still
running, and you can click on buttons, press keys, etc., behind the dialog
box. When calling MessageBox.Show() to use the built-in message box instead
of a custom form, however, the main form becomes disabled until the message
box is closed. How can you make a modal dialog box have the same effect? I
don't want the user to be able to do things on the main form until the dialog
box is closed.

Mike
 
C

Charles Calvert

Hi, Linda. Thanks for your response. The code you provided is exactly what
I saw in the MSDN Library documentation. I tried that,

Did you supply the reference to the parent window as the argument to
ShowDialog()?
and it does work as a modal dialog box - i.e. the code immediately
after ShowDialog() doesn't execute until you close the dialog box.
However, the main form is still running, and you can click on
buttons, press keys, etc., behind the dialog box.

I created a quick test program. Here's the relevant code from
Form1.cs:

private void button1_Click(object sender, System.EventArgs e)
{
Form2 frm = new Form2();
frm.ShowDialog(this);
}

Form2 was displayed and Form1 was disabled. I could not bring it to
the foreground, give it the focus or click on any of its child
controls.

What version of .NET are you using? I'm using 1.1.
 
L

Linda Liu [MSFT]

Hi Mike,

Thank you for your reply.

I have created a test project with the code in my previous message, and it
works well. I think the problem may be related to the actual code in your
project. If your project is not complex, you can send it to me to have a
look. If the project is too large to send by email, you may create a simple
winform project and copy your related code into it, so that the new project
also can reproduce the problem and send the new project to me please. To
get my actual email, Please remove the ¡°online¡± from my display email.

Thanks.



Sincerely,
Linda Liu
Microsoft Online Community Support

====================================================
When responding to posts,please "Reply to Group" via
your newsreader so that others may learn and benefit
from your issue.
====================================================
 

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