Reputation: 81
I have a main form. When a user presses a button a modal form opens to perform some operations, when this form closes I want the Main form (that instantiated the modal form) to recieve a value, a bool to be exact. How would I do this?
Quick overview of what I want:
MainForm Starts => Button pressed on MainForm => Instantiate new modal form with ShowDialog() => Modal form closes, returning a bool
Upvotes: 0
Views: 1907
Reputation: 3513
A modal form returns a DialogResult. If the user closes the dialogform with the OKbutton the result will be DialogResult.OK. However by default when the user closes the dialog with the cross, the result will be DialogResult.Cancel.
Note:
You can override the value assigned to the DialogResult property when the user clicks the Close button by setting the DialogResult property in an event handler for the Closing event of the form.
Example:
using ( var dialogResult = form1.ShowDialog() )
{
var isDialogResultOK = dialogResult == DialogResult.OK
if ( isDialogResultOK )
{
}
}
Upvotes: 2