[英]Advantage of using DialogResult.Cancel
What would the advantage of using: 使用的优点是什么:
DialogResult.Cancel
Over using: 过度使用:
this.Close()
Would it be just so that you would be able to determine that the user has selected Cancel
? 是否只是为了能够确定用户已选择
Cancel
?
Those consuming your interface, if showing the form with ShowDialog
, would get a result. 那些使用你的界面的人,如果用
ShowDialog
显示表单,就会得到一个结果。 They would know the user pressed Cancel
. 他们会知道用户按下
Cancel
。
As an aside. 作为旁白。 You can set the
Form
properties AcceptButton
and CancelButton
. 您可以设置
Form
属性AcceptButton
和CancelButton
。 Then, if the form is shown with ShowDialog
, when they are clicked the base Form
will set the DialogResult
and thus the Form
will close on its own. 然后,如果表单显示为
ShowDialog
,当单击它们时,基本Form
将设置DialogResult
,因此Form
将自行关闭。
You can use it value for this kind of purpose 您可以将其用于此类目的
var result = form.ShowDialog();
if (result == DialogResult.OK)
{
}
else if (result == DialogResult.Cancel)
{
//perform soem operation
}
So if you want to perform some operation on the result of dialog you can use this. 因此,如果您想对对话框的结果执行某些操作,您可以使用它。
Note : 注意 :
Both of this ok and cancel operation on dialog close your dialog, and by catching ok and cancel value you can perform extra task as given in above example. 对话框上的ok和cancel操作都会关闭对话框,通过捕获ok和取消值,您可以执行上面示例中给出的额外任务。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.