Winforms Custom Dialog hang parent execution
I'm attempting to create a custom dialog (using WinForms) that, much like a ColorDialog
or OpenFileDialog
, opens and accepts some input from the user, then returns execution to the parent form once input has been recieved.
I attempted to accomplish this by just creating a custom form that had a Show()
method, then calling it like this:
custom_dialog.Show();
var results = custom_dialog.Property;
As you could imagine, this did not work as the second line was executed before any input was selected.
My question is: How can I create a custom dialog that will hang the execution of the parent form, as a ColorDialog
or OpenFileDialog
does, so that I can force the user to input somethi开发者_StackOverflow中文版ng before execution continues?
you should call ShowDialog()
, this will open the dialog as modal and you will continue execution in the calling form only after dialog has been closed.
It is a good practice to use the using
block around the modal form so it gets disposed immediately once running out of such block.
You want a modal dialog. It will pop up and halt all other execution until it is closed.
Something like
if(custom_dialog.ShowDialog() == DialogResult.Ok)
do something here
else
do something different
Use Form.ShowDialog Method.
You can use this method to display a modal dialog box in your application. When this method is called, the code following it is not executed until after the dialog box is closed.
The dialog box can be assigned one of the values of the DialogResult enumeration by assigning it to the DialogResult property of a Button on the form or by setting the DialogResult property of the form in code. This value is then returned by this method.
You can use this return value to determine how to process the actions that occurred in the dialog box.
精彩评论