English 中文(简体)
C# CancelButton closes dialog?
原标题:

(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 );
    }
最佳回答

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.

问题回答

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.

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

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.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签