【问题标题】:How to return other values from from show dialog method?如何从表单 showdialog 方法返回其他值?
【发布时间】:2014-07-10 03:48:39
【问题描述】:
我有一个调用此方法的 winform:
frmIntegrationConfig frm = new frmIntegrationConfig();
DialogResult res = frm.ShowDialog();
res 总是以“Canceld”的形式返回。
如果用户单击“保存”按钮或只是关闭表单并关闭按钮(“X”),我该如何更改它?
【问题讨论】:
标签:
winforms
showdialog
dialogresult
【解决方案2】:
定义 frmIntegrationConfig() 窗口时,应将表单上的“AcceptButton”和“CancelButton”属性设置为要触发对话框接受和取消行为的按钮。
您还应该在按钮上设置“DialogResult”属性,以控制按钮导致对话框返回的特定 DialogResult 值。
EG,在您的设计器文件中的所有其他内容中,您需要以如下方式结束:
this.accept = new System.Windows.Forms.Button();
this.cancel = new System.Windows.Forms.Button();
this.other = new System.Windows.Forms.Button();
//
// accept
//
this.accept.DialogResult = System.Windows.Forms.DialogResult.OK;
//
// cancel
//
this.cancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
//
// other
//
this.other.DialogResult = System.Windows.Forms.DialogResult.Ignore;
//
// Form2
//
this.AcceptButton = this.accept;
this.CancelButton = this.cancel;
this.Controls.Add(this.other);
this.Controls.Add(this.cancel);
this.Controls.Add(this.accept);