C# CancelButton closes dialog?
Asked Answered
D

4

4

(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 );
    }
Damiandamiani answered 20/11, 2009 at 11:42 Comment(1)
I think you need to post some codeDiscoloration
P
7

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.

Parnell answered 20/11, 2009 at 12:7 Comment(2)
Hm. I'd hidden the X button, but not realised that Alt-F4 would close the dialog. Indeed, slurdge's practical solution seems the best, but I'm still curious as to why/when the button's DialogResult is being set to Cancel when I've changed it to None in the designer.Damiandamiani
I think your edit comes closest to explaining what's going on, with the CancelButton property being set at runtime also setting the button's DialogResult. I'm still in the dark as to why this happens, but without asking someone at Microsoft I'm not sure I'll get an answer to that.Damiandamiani
V
5

It seems that you want to perform validation of Form closing.
The FormClosing event is a perfect fit for this. This would enable you to have the same code to control close or not.
Just set the Cancel property to true to prevent closing. See FormClosing.

Volsci answered 20/11, 2009 at 11:51 Comment(1)
This is handy, and probably what I'll end up doing. What I'm really concerned with, though, is why the button is causing the form to close in the first place, when I've not told it to.Damiandamiani
A
3

It is the default behavior.

From MSDN :

This property allows you to designate a default action to occur when the user presses the ESC key in your application. You can use this property to allow the user to quickly navigate a simple form by allowing them to simply press the ESC key to close a window without committing changes instead of manually clicking the cancel button with their mouse.

CancelButton Property

Alesha answered 20/11, 2009 at 11:46 Comment(5)
I read that as "this button is pressed when Esc is pressed - and the button press may involve closing the form", not "this button is pressed when Esc is pressed, and we make the button press itself close the form".Damiandamiani
Did you checked the URL, it also contains a Community content.[I didn't noticed that ;) ] Setting a button as a CancelButton in the Visual Studio designer also has the side effect of setting the DialogResult of the button to Cancel. For a modal dialog, this will automatically cause the form to close when the button is clicked. To prevent the form from closing either clear the DialogResult property or use this code in your click handler: this.DialogResult = DialogResult.None;Alesha
Hmm. I'd noticed that setting the CancelButton in the designer also sets the button's DialogResult to Cancel. However, even if you then change the button's DialogResult in the Designer to None, it still closes the window. In fact, when debugging, I noticed that -even having set the button's DialogResult to none in the Designer- the button's DialogResult is still Cancel. What -is- going on here?Damiandamiani
Yes, it is working like that only. I resolved it like I modified the Window.Designer.cs and set the dialogResult of button to None. Also I set it to none on the FormLoad. Now it is working fine for me :)Alesha
Yes... I see. If I set the button's DialogResult -back- to None on FormLoad then it sticks (but not if I just set it in the designer code). So somewhere between running the program and the form being loaded, the button has its DialogResult set to Cancel purely because it is the form's Cancel button...Damiandamiani
T
1

Instead of trying to handle this in the button_click event, handle it in the form_closing event.

MyForm_FormClosing(object sender, FormClosingEventArgs e)
{
   if (DialogResult == DialogResult.Cancel)
   {
      // do my processing ...
      if (false)
         e.Cancel = true;   // stop the form from closing
   }
}

I handle my Accept button code here also, I assume anything != DialogResult.OK is a cancel, but you can do whatever is comfortable for you.

Thionate answered 21/2, 2011 at 20:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.