C# CancelButton 关闭对话框?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1769951/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 20:40:13  来源:igfitidea点击:

C# CancelButton closes dialog?

c#modal-dialogcancel-button

提问by Rawling

(VS2005, .Net 2.0)

(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.

我有一个使用 ShowDialog() 方法显示为对话框的表单。窗体的 CancelButton 属性设置为窗体上的按钮。即使我将该按钮的 DialogResult 设置为 None,单击该按钮仍会关闭对话框。我不希望这种情况发生 - 我希望能够控制对话框是否关闭。

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.

表单的 AcceptButton 不会发生此问题 - 将该按钮的 DialogResult 设置为 none,我可以进行必要的处理,然后决定是否手动设置表单的 DialogResult 以使其关闭。

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?

我认为 CancelButton 属性仅用于指示按下 Escape 时应“单击”的按钮(就像 AcceptButton 仅应指示按下 Enter 时“单击”的按钮一样)。我错了吗?我是否错过了我的表单关闭的其他原因?或者这是一个错误?

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

编辑:添加了代码。这是带有取消按钮(按钮 1)的对话框窗体(窗体 2)。取消按钮只是窗体的 CancelButton,它没有将 DialogResult 设置为 Cancel,但按下按钮仍然关闭窗体

    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 );
    }

采纳答案by Jo?o Angelo

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.

另请注意,按 Alt+F4 和按 X 按钮可能会关闭表单,这两种方式都不会触发取消按钮单击事件。

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.

除非您也在处理这些情况,否则最好遵循 slurdge 的建议并防止表单在 FormClosing 事件中关闭。

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:

编辑:另请注意,如果您在“属性”窗口中将按钮的 DialogResult 更改回无,则将其更改为默认值。如果该值是属性的默认值,则它不会保留在 *.Designer.cs 中。即使它被持久化,表单初始化代码也放在 *.Designer.cs 的最后,并且会因为以下行而用 Cancel 覆盖 None :

this.CancelButton = this.button1;

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

正如您可以在 Reflector 中签入的那样,前一行是这样做的:

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.

您可以在 InitializeComponent() 调用后在构造函数中将其改回 None 。

回答by Anuraj

It is the default behavior.

这是默认行为。

From MSDN :

从 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.

此属性允许您指定当用户在应用程序中按下 ESC 键时发生的默认操作。您可以使用此属性来允许用户通过简单地按 ESC 键关闭窗口而不提交更改而不是用鼠标手动单击取消按钮来快速导航简单的表单。

CancelButton Property

CancelButton 属性

回答by slurdge

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.

看来您要执行表单关闭的验证。
FormClosing 事件非常适合这种情况。这将使您能够使用相同的代码来控制关闭与否。
只需将 Cancel 属性设置为 true 即可防止关闭。见FormClosing

回答by jac

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

不要尝试在 button_click 事件中处理它,而是在 form_ending 事件中处理它。

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.

我也在这里处理我的接受按钮代码,我假设任何事情 != DialogResult.OK 都是取消,但你可以做任何对你来说舒服的事情。