-3

I don't understand why this property doesn't have a setter. What would happen if you were able to set this property?

Amen Jlili
  • 1,884
  • 4
  • 28
  • 51

3 Answers3

2

As others have said, it depends on how the form is currently being used, I.e. whether it was Show()ed or ShowDialog()ed. Whilst the form is being shown, it doesn't make sense to allow you to change it's modality1.

When the form isn't being shown, it also doesn't make sense to have this be a settable property since what matters is whether the next use of the form is via Show or ShowDialog.

Whilst it's true that most forms will only ever be shown either modally or non-modally, the option remains open to use a single form in both manners.


1Especially because when a form is being shown modally via ShowDialog, the code calling ShowDialog is blocked until it returns with the result of the dialog. If you were to change a form to be modal after it was already Shown, there's nobody waiting to receive the dialog result when it returns.

Damien_The_Unbeliever
  • 234,701
  • 27
  • 340
  • 448
1

The purpose of Form.Modal property, from the documentation:

Gets a value indicating whether this form is displayed modally.

So, it only checks for the modal status of the form. It doesn't make sense for it to have a setter.

In order to open a form modally, you should use the ShowDialog() method, also as explained in the link above. That way when you check for the Modal property, it returns true.

If your question is whether or not you can have a modal form after it's been shown, the answer is no, you can't. To understand why, you need to know how ShowDialog() makes the form modal in the first place. Check this awesome answer by Hans Passant in order to understand how ShowDialog() works at a technical level.

0

I don't understand why this property doesn't have a setter. What would happen if you were able to set this property?

It doesn't have a setter because you cant change its Modal style once the Form has been shown. Although subtle, Modal and and Non Modal forms behave differently in regards to owner, and blocking the caller, and other less obvious things. It seemingly just not worth the confusion of adding this i would imagine.

Further more, users must interact with the Modal/Dialog window before they can return to the parent application. This avoids interrupting the workflow on the main window and by design.

So if you want a Modal Form, call ShowDialog() or else call Show()

TheGeneral
  • 79,002
  • 9
  • 103
  • 141