winforms 应用程序中的窗口关闭事件

发布于 2024-10-14 20:40:49 字数 168 浏览 2 评论 0原文

我希望在用户关闭 winforms 应用程序中的表单窗口时提示用户保存数据。我不知道如果用户单击表单右上角的红色框,如何触发向用户的提示。

我的应用程序当前有一个布尔标志,在 textchanged 事件上设置为 True。因此,我只需要在红色框触发的任何事件中检查布尔值。

有什么建议吗?

I am looking to prompt the user to save data when they close a form window in a winforms application. I can't figure out how to trigger the prompt to the user, should they click the red box at the top right corner of the form.

My application currently has a boolean flag, that is set to True on textchanged event. So I will only need to check for the boolean value in whatever event is trigger by the red box.

Any advice?

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

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

发布评论

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

评论(4

半窗疏影 2024-10-21 20:40:49

您需要处理FormClosing 事件该事件在表单即将关闭之前引发,无论是因为用户单击了标题栏中的“X”按钮还是通过任何其他方式。

由于该事件是在表单关闭之前引发的,因此您可以取消关闭事件。您将传递一个 FormClosingEventArgse 参数中的 code> 类。通过设置 e.Cancel属性设置为True,您可以取消待处理的关闭事件。

例如:

Private Sub Form_Closing(ByVal sender As Object, ByVal e As FormClosingEventArgs)
    If Not isDataSaved Then
        ' The user has unsaved data, so prompt to save
        Dim retVal As DialogResult
        retVal = MessageBox.Show("Save Changes?", YesNoCancel)
        If retVal = DialogResult.Yes Then
            ' They chose to save, so save the changes
            ' ...
        ElseIf retVal = DialogResult.Cancel Then
            ' They chose to cancel, so cancel the form closing
            e.Cancel = True
        End If
        ' (Otherwise, we just fall through and let the form continue closing)
    End If
End Sub

You need to handle the FormClosing event. This event is raised just before the form is about to be closed, whether because the user clicked the "X" button in the title bar or through any other means.

Because the event is raised before the form is closed, it provides you with the opportunity to cancel the close event. You are passed an instance of the FormClosingEventArgs class in the e parameter. By setting the e.Cancel property to True, you can cancel a pending close event.

For example:

Private Sub Form_Closing(ByVal sender As Object, ByVal e As FormClosingEventArgs)
    If Not isDataSaved Then
        ' The user has unsaved data, so prompt to save
        Dim retVal As DialogResult
        retVal = MessageBox.Show("Save Changes?", YesNoCancel)
        If retVal = DialogResult.Yes Then
            ' They chose to save, so save the changes
            ' ...
        ElseIf retVal = DialogResult.Cancel Then
            ' They chose to cancel, so cancel the form closing
            e.Cancel = True
        End If
        ' (Otherwise, we just fall through and let the form continue closing)
    End If
End Sub
等数载,海棠开 2024-10-21 20:40:49

如果您重写表单的 OnFormClosing 方法,您有机会通知用户已进行更改,并有机会取消关闭表单。

该事件为您提供了一个 FormClosingEventArgs 实例,该实例有一个 CloseReason 属性(它告诉窗体关闭的原因)以及 Cancel 属性,您可以将其设置为 True 以阻止窗体关闭。

If you override the form's OnFormClosing method, you have the chance to notify the user that changes have been made, and the opportunity to cancel closing the form.

The event provides you with a FormClosingEventArgs instance which has a CloseReason property (which tells you why the form is closing) as well as a Cancel property, which you can set to True to stop the form from closing.

小…红帽 2024-10-21 20:40:49

我为 C# 实现了这段代码,希望对你有用

protected override void OnFormClosing(FormClosingEventArgs e)
            {            
                base.OnFormClosing(e);
                if (PreClosingConfirmation() == System.Windows.Forms.DialogResult.Yes)
                {
                    Dispose(true);
                    Application.Exit();
                }
                else
                {
                    e.Cancel = true;
                }
            }

        private DialogResult PreClosingConfirmation()
        {
            DialogResult res = System.Windows.Forms.MessageBox.Show(" Do you want to quit?          ", "Quit...", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            return res;
        }

I implent this code for C# hope so it useful to you

protected override void OnFormClosing(FormClosingEventArgs e)
            {            
                base.OnFormClosing(e);
                if (PreClosingConfirmation() == System.Windows.Forms.DialogResult.Yes)
                {
                    Dispose(true);
                    Application.Exit();
                }
                else
                {
                    e.Cancel = true;
                }
            }

        private DialogResult PreClosingConfirmation()
        {
            DialogResult res = System.Windows.Forms.MessageBox.Show(" Do you want to quit?          ", "Quit...", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            return res;
        }
凝望流年 2024-10-21 20:40:49

您需要 FormClosing 事件

You need the FormClosing Event

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