I have a winforms project. I open a child MDI form from base form / parent form. Then I call form2.ShowDialog(), this freezes parent form and all other forms until the ShowDialog() is completed.
I want only the child form from which the ShowDialog() is called to freeze. I want other forms to be available and accessible.
You have to be aware that there are two forms of Dialog boxes: modal dialog boxes and modeless dialog boxes
From the description above, you can gather that you are showing a modal dialog box. To be able to switch the focus to the other forms, you should use
Form.Show, instead ofForm.ShowDialog.There is something extra in your code: you only want the Child form to freeze, not the other forms of your application. The standard method is to Disable the child form before showing the modeless dialog box, and to Enable the child form after the modeless dialog box has been closed.
You forgot to give us some code, so I don't know which form will show the modeless dialog box. Usually the dialog box is shown by clicking on something in the child form that must be disabled. Code would look like this:
Consider to add a check in the beginning of this method to be sure that MyModelessDialogBox does not exist yet.
When the modeless dialog box has been closed, the event handler will be called:
Things to consider
If your modeless dialog box is shown, the operator could decide to close your application, or shutdown windows. In that case, you'll have to close MyModelessDialog.
However, the operator might just have been started something very important on the Modeless dialog box. Standard behaviour would be to warn the operator and to give him the opportunity to cancel the shutdown. Compare this to closing an application while you forgot to save the file.
If your application is about to be closed, event Form.Closing is raised. Consider to add an event handler for this event. In the event handler you can check if MyModelessDialogBox exists, and if so, you can ask it to close itself. The modeless dialog box will do the standard things when being closed, for instance ask the operator to save the data, or something else. If the modeless dialog box can't be closed, the closing program should be cancelled.
Code in the form that created the modeless dialog box would be like this:
It is up to your modeless dialog box to decide what to do when it is to be closed. Maybe throw away all input data, or save it for the next time? Ask the operator if closing is ok, or if data must be saved? The modeless dialog box can check why the dialog box is closed via property FormClosingEventArgs.CloseReason