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

编程入门 行业动态 更新时间:2024-10-09 10:26:29
本文介绍了在一个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.

任何意见?

推荐答案

您需要处理的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事件。您传递的FormClosingEventArgs类中的电子参数。通过设置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应用程序窗口关闭事件

本文发布于:2023-11-03 05:07:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1554312.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:应用程序   窗口   事件   WinForms

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!