在一个WinForms应用程序窗口关闭事件

在一个WinForms应用程序窗口关闭事件

本文介绍了在一个WinForms应用程序窗口关闭事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我期待提示用户保存数据时,他们关闭窗体窗口WinForms应用程序。我无法弄清楚如何触发提示用户,应点击红色框,表格右上角。

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.

我的应用程序目前有一个布尔标志,它被设置为True TextChanged事件。所以我只需要检查布尔值的任何事件是触发了红色框。

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.

任何意见?

推荐答案

您需要处理的<$c$c>FormClosing事件。:该事件引发的形式就在即将被关闭,无论是因为用户点击标题栏中的X按钮,或通过任何其他方式。

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.

由于该事件引发的的封闭形式,它为您提供了机会的取消的close事件。您传递的<$c$c>FormClosingEventArgs类中的电子参数。通过设置<$c$c>e.Cancel物业为True,则可以取消即将关闭的事件。

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.

例如:

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

这篇关于在一个WinForms应用程序窗口关闭事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 06:15