Sukesh Rai
Sukesh Rai

Reputation: 3

Form1 cannot be closed before Form2 Closed in windows application C#

I have two forms, Form1 and Form2, after opening Form1 I have to open Form2, if I click on Form1 close dialog that should not be closed before closing the Form2.

Upvotes: 0

Views: 510

Answers (4)

haipham23
haipham23

Reputation: 476

Code below. Maybe this is what you need.

private void btnExit_Click(object sender, EventArgs e)
        {
            this.Visible = false;
        }

Upvotes: 0

Mav
Mav

Reputation: 153

Do you need interact with both windows? If only Form2 gets the attention, you can solve it with the mentioned ShowDialog (). Otherwise you can check with OnClosing if Form2 still open and refuse it. Here you have a nice code snip to disable the close button: http://social.msdn.microsoft.com/Forums/windows/en-US/32148556-2640-4d79-a7b6-f13f9ce3420a/how-to-enabledisable-the-close-buttonx-in-window-form-?forum=winforms

Upvotes: 0

Morix Dev
Morix Dev

Reputation: 2724

You can simply open Form2 using Form2.ShowDialog(): it this is called from Form1 code then Form2 is modal with respect to Form1 (that is you can interact no more with Form1 until you close Form2) and so the user is forced to close Form2 before being able to close Form1.

Please mark this answer as accepted if it responds your question.

Upvotes: 1

Mihai Hantea
Mihai Hantea

Reputation: 1743

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

Upvotes: 1

Related Questions