C# CancelButton 关闭对话框?

发布于 2024-08-12 06:57:31 字数 1018 浏览 3 评论 0原文

(VS2005,.Net 2.0)

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

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

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

编辑:添加代码。这是带有取消按钮(按钮 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 );
    }

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

花想c 2024-08-19 06:57:31

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

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

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

this.CancelButton = this.button1;

As you can check in 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在 InitializeComponent() 调用之后的构造函数中。

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.

万劫不复 2024-08-19 06:57:31

您似乎想要执行表单关闭的验证。
FormClosing 事件非常适合此目的。这将使您能够使用相同的代码来控制关闭与否。
只需将 Cancel 属性设置为 true 即可防止关闭。
请参阅FormClosing

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.

记忆消瘦 2024-08-19 06:57:31

这是默认行为。

来自 MSDN :

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

CancelButton 属性

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

默嘫て 2024-08-19 06:57:31

不要尝试在button_click事件中处理这个问题,而是在form_close事件中处理它。

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文