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

Viewed 96688

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

How would you return the OK message in the condition of a textbox that will proceed when the user presses Enter, and will send Cancel when the user presses Ctrl+Q?

Disregard: solution- this.dialogresult = dialogresult.ok or dialogresult.cancel.

3 Answers

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.

Related