VB.net:如果用户要关闭未保存数据的程序,我如何提示用户?

发布于 2024-10-03 04:34:35 字数 153 浏览 1 评论 0原文

我使用 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 技术交流群。

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

发布评论

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

评论(5

嘿嘿嘿 2024-10-10 04:34:35

保留一个全局布尔值(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 to True and then on the Form_Closing() check that boolean and throw a message box.

谁的新欢旧爱 2024-10-10 04:34:35

我认为您还应该提供取消,以便用户可以取消关闭,而不必保存数据或丢失他们已经所做的更改。像这样的东西:

    Private Sub frmMain_FormClosing(ByVal sender As Object, _
        ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing

    Dim Answer As DialogResult = MessageBox.Show("Save Data before close?", _
        "Data Check", MessageBoxButtons.YesNoCancel)

    Select Case Answer
        Case Windows.Forms.DialogResult.Yes
            SaveRecords()
        Case Windows.Forms.DialogResult.No
            Exit Sub
        Case Windows.Forms.DialogResult.Cancel
            e.Cancel = True
        Case Else
            Exit Sub
    End Select

End Sub

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:

    Private Sub frmMain_FormClosing(ByVal sender As Object, _
        ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing

    Dim Answer As DialogResult = MessageBox.Show("Save Data before close?", _
        "Data Check", MessageBoxButtons.YesNoCancel)

    Select Case Answer
        Case Windows.Forms.DialogResult.Yes
            SaveRecords()
        Case Windows.Forms.DialogResult.No
            Exit Sub
        Case Windows.Forms.DialogResult.Cancel
            e.Cancel = True
        Case Else
            Exit Sub
    End Select

End Sub
一直在等你来 2024-10-10 04:34:35

我会使用表单的 OnClosing 事件来提示他们。像这样的事情应该可以解决问题:

Private Sub YourDataGridViewForm_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
    If MessageBox.Show(Me, "Do you want to save your changes?", "Unsaved Changes!", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = Windows.Forms.DialogResult.Yes Then
        SaveChanges()
    End If
End Sub

Private Sub SaveChanges()
    MessageBox.Show("Changes saved...")
End Sub

I'd prompt them using your Form's OnClosing event. Something like this should do the trick:

Private Sub YourDataGridViewForm_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
    If MessageBox.Show(Me, "Do you want to save your changes?", "Unsaved Changes!", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = Windows.Forms.DialogResult.Yes Then
        SaveChanges()
    End If
End Sub

Private Sub SaveChanges()
    MessageBox.Show("Changes saved...")
End Sub
暗恋未遂 2024-10-10 04:34:35

捕获应用程序关闭事件。您要做的第一件事是提示用户。如果他们说是,那就保存。然后执行正常关机。

您还可以保留一个标志来跟踪是否存在任何未保存的数据。当用户保存时该值被清除,但当用户进行更改时则被设置。

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.

廻憶裏菂餘溫 2024-10-10 04:34:35

我建议在主窗口中使用 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.

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