C#:不使用按钮时如何发送对话框的 OK 或 Cancel 返回消息?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/600107/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C#: How do you send OK or Cancel return messages of dialogs when not using buttons?
提问by
C#: How do you send OK or Cancel return messages of dialogs when not using buttons?
C#:不使用按钮时如何发送对话框的 OK 或 Cancel 返回消息?
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?
当用户按下 Enter 时,文本框将继续执行,并在用户按下 Ctrl+Q 时发送 Cancel,您将如何在文本框的条件下返回 OK 消息?
Disregard: solution- this.dialogresult = dialogresult.ok or dialogresult.cancel.
忽略:解决方案- this.dialogresult = dialogresult.ok 或 dialogresult.cancel。
回答by Dave Markle
I assume you're using Windows Forms...
我假设您正在使用 Windows 窗体...
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.
对于 OK - 将表单上的 AcceptButton 设置为 OK 按钮。对于取消 - 将表单上的取消按钮设置为取消按钮。
OR, you can manually set the forms DialogResult to DialogResult.OK or DialogResult.Cancel and then close the form programatically.
或者,您可以手动将表单 DialogResult 设置为 DialogResult.OK 或 DialogResult.Cancel,然后以编程方式关闭该表单。
回答by configurator
Set the form's DialogResult
:
设置表单的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.
这将导致打开此表单的任何开启者ShowDialog()
都获得给定DialogResult
的结果。
回答by mmmdreg
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.
直接,在按钮本身的属性中,有DialogResult属性,可以设置为OK/Cancel/Yes/No/etc...正如其他人所说,这也可以通过编程方式设置。
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.
在按钮所在窗体的属性中,将 AcceptButton 属性设置为您的按钮。这也会做一些事情,比如当你按下回车键时触发按钮,并突出显示按钮。