6

I open a form as a modal dialog using ShowDialog. This dialog in turn allows another form to be opened as a modal dialog again using ShowDialog.

When the innermost dialog is closed, this causes its parent dialog to close as well. Why does this occur and how can I prevent it?

I have created a hello world version of the problem to illustrate this.

Form 1:

Form 1

private void OpenForm2Button_Click(object sender, EventArgs e)
{
    Form2 testForm = new Form2();
    DialogResult dialogResult = new DialogResult();
    dialogResult = testForm.ShowDialog();
    MessageBox.Show("Form 2 returned: " + Convert.ToString(dialogResult));
}

Form 2:

Form 2

...
this.Form2OKButton.DialogResult = System.Windows.Forms.DialogResult.OK;
this.Form2CancelButton.DialogResult = System.Windows.Forms.DialogResult.Cancel;
...
this.AcceptButton = this.Form2OKButton;
this.CancelButton = this.Form2CancelButton;
...
private void OpenForm3Button_Click(object sender, EventArgs e)
{
    Form3 testForm = new Form3();
    DialogResult dialogResult = new DialogResult();
    dialogResult = testForm.ShowDialog();
    MessageBox.Show("Form 3 returned: " + Convert.ToString(dialogResult));
}

Form 3:

Form 3

...
this.Form3OKButton.DialogResult = System.Windows.Forms.DialogResult.OK;
this.Form3CancelButton.DialogResult = System.Windows.Forms.DialogResult.Cancel;
...
this.AcceptButton = this.Form3OKButton;
this.CancelButton = this.Form3CancelButton;

Steps to reproduce:

  • Click "Open Form 2"
  • Click "Open Form 3"
  • Click "Cancel"

Form 3 closes with DialogResult == Cancel as expected, but Form 2 also closes with DialogResult == Cancel (not expected).

Warren Blumenow
  • 673
  • 2
  • 11
  • 17
  • Mmh, I tried and it [works fine on my machine](http://codinghorror.typepad.com/.a/6a0120a85dcdae970b0128776ff992970c-pi). We should see the whole (real) code to understand the problem. You can use http://pastebin.com/ to paste long pieces of code... – digEmAll Mar 20 '12 at 18:04
  • I have pasted the code here: http://pastebin.com/Dj81gAze – Warren Blumenow Mar 20 '12 at 18:34

1 Answers1

16

EDIT :

the problem is this one (file: Form2.Designer.cs):

this.OpenForm3Button.DialogResult = System.Windows.Forms.DialogResult.Cancel;

when you click the OpenForm3Button, after the end of the OpenForm3Button_Click event handler, the form.DialogResult is automatically set to Cancel and it is closed.

Reset the DialogResult property of OpenForm3Button and it will work as expected :)

digEmAll
  • 56,430
  • 9
  • 115
  • 140
  • No idea how that got set. The weird thing is that it did that in my main project and did the same thing in the hello world one that I set up. I must've made the same mistake both times. Works perfectly now. Thanks very much. I will check for things like that more carefully next time. – Warren Blumenow Mar 20 '12 at 23:28
  • 4
    @WarrenBlumenow: If you added the cancel button first, and then copied it (using CTRL-C or CTRL-mousedrag) to create the open button, all the properties of the former are copied in the latter. It happens very often. :) – digEmAll Mar 21 '12 at 08:23
  • 1
    Oh, wow. Seems I had this bug because I first set a CancelButton on the form, and then copied that button to make a second button. It seems setting the form's CancelButton automatically set the button's DialogResult. Good to know. – Nyerguds Jun 01 '16 at 08:44
  • 1
    @Nyerguds I've just discovered that if you dbl-click on the `CancelButton` property of your form in VS, with the aim of cycling through the available buttons until you get to the one you want, it will apply `DialogResult = System.Windows.Forms.DialogResult.Cancel` to *all* the button you've cycled through. A big bug indeed. – Steve Smith Aug 10 '17 at 15:22