【问题标题】: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


    【解决方案1】:

    您需要设置frmIntegrationConfig 的DialogResult

    您可以通过在 btnSave_Click() 方法中设置 DialogResult of the Save button to DialogResult.OK 或我更喜欢的方式来做到这一点,因为很明显发生了什么,set the form's DialogResult 到 DialogResult.OK 在 btnSave_Click() 方法中:

    private void btnSave_Click(object sender, EventArgs e)
    { 
         // closes form and returns value to ShowDialog
         this.DialogResult = DialogResult.OK;       
    }
    

    【讨论】:

      【解决方案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);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-04-30
        • 1970-01-01
        • 2014-12-14
        • 1970-01-01
        • 2022-07-06
        • 2021-02-03
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多