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

Solution 1:

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.

Solution 2:

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.