C# CancelButton closes dialog?

Rawling picture Rawling · Nov 20, 2009 · Viewed 32.1k times · Source

(VS2005, .Net 2.0)

I have a form that is displayed as a dialog using the ShowDialog() method. The form's CancelButton property is set to a button on the form. Even if I set that button's DialogResult to None, clicking the button still closes the dialog. I don't want this to happen - I want to be able to control whether the dialog closes or not.

This issue doesn't occur with the form's AcceptButton - with that button's DialogResult set to none, I can do what processing is necessary, then decide whether or not to manually set the form's DialogResult to cause it to close.

I thought the CancelButton property was meant soley to indicate the button that should be "clicked" if Escape is pressed (much as the AcceptButton is only supposed to indicate the button to "click" when Enter is pressed). Am I wrong in this? Have I missed some other reason my form is closing? Or is this a bug?

Edit: Code added. This is the dialog form (form 2) with the cancel button (button 1). The cancel button is only the form's CancelButton, it does not have DialogResult set to Cancel, but pressing the button still closes the form

    private void InitializeComponent()
    {
        this.button1 = new System.Windows.Forms.Button();
        this.SuspendLayout();
        // 
        // button1
        // 
        this.button1.Name = "button1";
        this.button1.Text = "button1";
        // 
        // Form2
        // 
        this.CancelButton = this.button1;
        this.Controls.Add( this.button1 );
        this.Name = "Form2";
        this.Text = "Form2";
        this.ResumeLayout( false );
    }

Answer

João Angelo picture João Angelo · Nov 20, 2009

Also beware that the form may be closed by pressing Alt+F4 and pressing the X button and both ways will not trigger the cancel button click event.

Unless you are also handling these situations wouldn't it be best to follow the advice of slurdge and prevent the form from closing in the FormClosing event.

Edit: Also note that if you change the DialogResult of the button back to None in the Properties windows, you are changing it to the default value. If the value is the default value of the property then it will not be persisted in the *.Designer.cs. Even if it were persisted the form initialization code is placed last in the *.Designer.cs and would override the None with Cancel because of the line:

this.CancelButton = this.button1;

As you can check in Reflector the previous line does this:

public void set_CancelButton(IButtonControl value)
{
    base.Properties.SetObject(PropCancelButton, value);
    if ((value != null) && (value.DialogResult == DialogResult.None))
    {
        value.DialogResult = DialogResult.Cancel;
    }
}

You can change it back to None in the constructor after the InitializeComponent() call.