Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#: How do you send OK or Cancel return messages of dialogs when not using buttons?

Tags:

c#

Set the form's DialogResult:

this.DialogResult = DialogResult.OK;
this.Close();

This would cause any opener that opened this form with ShowDialog() to get the given DialogResult as the result.


I assume you're using Windows Forms...

A couple of ways.

For OK - set AcceptButton on the form to the OK button. For Cancel - set Cancelbutton on the form to the cancel button.

OR, you can manually set the forms DialogResult to DialogResult.OK or DialogResult.Cancel and then close the form programatically.


Directly, in the properties of the button itself, there is the DialogResult property that can be set to OK/Cancel/Yes/No/etc... As the others have said, this can also be set programmatically.

In the properties of the form the button is on, set the AcceptButton property to your button. This will also do things like trigger the button when you hit the enter key, and highlight the button.