0

I have a FormA from which I open FormB like so:

FormB B = new FormB();
FormB.ShowDialog();

In FormB, I have some code in a try catch block and when it throws the exception, FormB is closed.

private void func()
{
  try
  {
     // some code
     DialogResult = DialogResult.Ok;
     throw new Exception("Test exception")
  } 
  catch (Exception ex)
  {
    MessageBox.Show(ex.Message);
  }
}

Take look at that two lines of code. When DialogResult assignment is above exception throw, form closes after exception.

Vice versa, the form is not closing. So can someone explain that behavior?

a-ctor
  • 3,568
  • 27
  • 41
Flo Rida
  • 55
  • 6
  • When you say closed, do you mean 'it disappeared from view'? If so, that is what I would expect - when you set the `DialogResult` then you are asking for the form to be hidden as per the docs - https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.form.dialogresult?view=netframework-4.7.2 . Why are you **not** expecting that to act that way? – mjwills Sep 28 '18 at 11:17
  • 2
    Because `DialogResult=DialogResult.Ok` will close the form. if it is below the exception throw, it won't get executed. Seems like normal behaviour. – Jeroen van Langen Sep 28 '18 at 11:19
  • I think technically it hides the form @J.vanLangen. – mjwills Sep 28 '18 at 11:20
  • In your code example the form should always close because you set the DialogResult before you throw the exception. Are you saying that with this code the form does not closes ? – GuidoG Sep 28 '18 at 12:48
  • You could move the `DialogResult` related code and checks to the `Finally` block. It'll get executed in any case. – Jimi Sep 28 '18 at 17:28

1 Answers1

-1

When you are changing the DialogResult property of a Form (which is shown with ShowDialog()), it will be closed. When you raise an exception before setting the property, the property won't be changed, so it won't close the form. When a form isn't displayed as a modal dialog box, clicking the Close button (the button with an X in the top-right corner of the form) causes the form to be hidden.


I'll give some more info. Like the documentation says:

The dialog result of a form is the value that is returned from the form when it is displayed as a modal dialog box. If the form is displayed as a dialog box, setting this property with a value from the DialogResult enumeration sets the value of the dialog box result for the form, hides the modal dialog box, and returns control to the calling form. This property is typically set by the DialogResult property of a Button control on the form. When the user clicks the Button control, the value assigned to the DialogResult property of the Button is assigned to the DialogResult property of the form.

Source

So if you set the property before the exception. It will trigger the dialog to close. (it probably sends a WM_CLOSE message to the form, that's why it doesn't close directly)

E_net4
  • 27,810
  • 13
  • 101
  • 139
Jeroen van Langen
  • 21,446
  • 3
  • 42
  • 57