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!
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With