VB.net:如果用户要关闭未保存数据的程序,我如何提示用户?
我使用 Visual Basic 2010 Express 进行工作。我有一个 DataGridView,它与我创建的本地 SQL 数据库链接。我目前拥有用户可以单击按钮将数据保存到数据库的位置,但我不确定如果他们在不保存的情况下关闭程序,如何提示他们保存或放弃更改。
谢谢!
I working working with Visual Basic 2010 Express. I have a DataGridView that is linked up with a local SQL database I've created. I have it currently where the user can click a button to save their data to the db, but am unsure how to prompt them to save or discard changes if they are closing the program without saving.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
保留一个全局布尔值(
Dim _bDocumentChanged as boolean
),当触发任何 DataGridView 事件时,将布尔值设置为True
,然后在 Form_Closing() 上检查该布尔值并抛出一条消息盒子。Keep a global boolean (
Dim _bDocumentChanged as boolean
) and when any DataGridView events are fired set your boolean toTrue
and then on the Form_Closing() check that boolean and throw a message box.我认为您还应该提供取消,以便用户可以取消关闭,而不必保存数据或丢失他们已经所做的更改。像这样的东西:
I think you should also provide a cancel so the user can cancel the close without having to save the data or lose the changes they already made. Something like this:
我会使用表单的 OnClosing 事件来提示他们。像这样的事情应该可以解决问题:
I'd prompt them using your Form's OnClosing event. Something like this should do the trick:
捕获应用程序关闭事件。您要做的第一件事是提示用户。如果他们说是,那就保存。然后执行正常关机。
您还可以保留一个标志来跟踪是否存在任何未保存的数据。当用户保存时该值被清除,但当用户进行更改时则被设置。
Trap the application close event. First thing you do, is to prompt the user. if they say yes, then save. Then perform the normal shutdown.
You could also keep a flag to keep track of if there's any unsaved data present. This is cleared when the user saves, but set when they make a change.
我建议在主窗口中使用 FormClosing 事件进行陷阱。请记住,当主窗口关闭时,应用程序将终止。如果这是一个 MDI 应用程序,您必须在允许应用程序结束之前检查每个子窗口是否可以保存。
I would suggest trap with FormClosing event in the main window. Remember when the main window closes the application terminates. If this is an MDI application you have to check that each child window is ok to save before allowing the application to end.