0

I have a winform in which I call this method:

frmIntegrationConfig frm = new frmIntegrationConfig();
DialogResult res = frm.ShowDialog();

The res is always returned as "Canceld". how can I change it according to if the user click "Save" button o r just closed the form int ther close button ("X") ?

Liran Friedman
  • 4,027
  • 13
  • 53
  • 96

2 Answers2

1

When you define your frmIntegrationConfig() window, you should set the "AcceptButton" and "CancelButton" properties on the form to the buttons you want to trigger the accept and cancel behaviours for your dialog.

You should also set the "DialogResult" properties on your buttons to control the specific DialogResult value that the button causes the dialog to return.

EG, amongst all the other stuff in your designer file for the dialog you need to end up with something like:

    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);
JermDavis
  • 1,155
  • 12
  • 22
1

You need to set the frmIntegrationConfig's DialogResult.

You can do this by setting the DialogResult of the Save button to DialogResult.OK or alternatively, which I prefer as it is evident what is happening, set the form's DialogResult to DialogResult.OK in the btnSave_Click() method:

private void btnSave_Click(object sender, EventArgs e)
{ 
     // closes form and returns value to ShowDialog
     this.DialogResult = DialogResult.OK;       
}
stuartd
  • 70,509
  • 14
  • 132
  • 163