Ben McCormack
Ben McCormack

Reputation: 33078

How do I make a form modal in Windows Forms?

I'm trying to create a child form that helps the user to enter data for a field in the parent form. I want this child form to be modal, but what do I need to do to make this form modal?

Is there's a different type of item I need to use?

Upvotes: 48

Views: 117618

Answers (5)

Andrei Krasutski
Andrei Krasutski

Reputation: 5257

After you close the modal form, dispose the resources

using (Form form = new Form())
{
  form.ShowDialog(this);
} // Dispose form

The using statement ensures that Dispose is called even if an exception occurs within the using block.

More using statement (C# Reference)

Upvotes: 6

Kimberly Olivo
Kimberly Olivo

Reputation: 121

Call the ShowDialog method.

Form f = new Form();
f.ShowDialog(this);

For more info click this https://msdn.microsoft.com/en-us/library/aa984358(v=vs.71).aspx

Upvotes: 11

Abduhafiz
Abduhafiz

Reputation: 3404

Form f = new Form();
f.ShowDialog(this);

Upvotes: 21

Paul Keister
Paul Keister

Reputation: 13077

Use the ShowDialog() method instead of Show() when you display the child form.

Upvotes: 25

user27414
user27414

Reputation:

Use Form.ShowDialog()

As Bob mentioned, you should set Form.DialogResult accordingly on your modal form.

Upvotes: 62

Related Questions