winforms 应用程序中的窗口关闭事件
我希望在用户关闭 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您需要处理
FormClosing
事件。该事件在表单即将关闭之前引发,无论是因为用户单击了标题栏中的“X”按钮还是通过任何其他方式。由于该事件是在表单关闭之前引发的,因此您可以取消关闭事件。您将传递一个
FormClosingEventArgse
参数中的 code> 类。通过设置e.Cancel
属性设置为True,您可以取消待处理的关闭事件。例如:
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 thee
parameter. By setting thee.Cancel
property to True, you can cancel a pending close event.For example:
如果您重写表单的 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.
我为 C# 实现了这段代码,希望对你有用
I implent this code for C# hope so it useful to you
您需要 FormClosing 事件
You need the FormClosing Event