3

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!

Harry Johnston
  • 35,639
  • 6
  • 68
  • 158
daveomcd
  • 6,367
  • 14
  • 83
  • 137

5 Answers5

6

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.

Jim
  • 3,425
  • 9
  • 32
  • 49
  • You should allow them to cancel the closing event using e.cancel. – xpda Nov 18 '10 at 18:53
  • @xpda - you are correct, although this was just a rough idea of how to set this up and not full code for the scenario. – Jim Nov 18 '10 at 20:57
  • However, this change event may not change value at all. For example, change a value to a new value and then change it back. – David.Chu.ca Dec 05 '11 at 23:47
4

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
Buck Hicks
  • 1,534
  • 16
  • 27
2

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
Aaron Daniels
  • 9,563
  • 6
  • 45
  • 58
1

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.

winwaed
  • 7,645
  • 6
  • 36
  • 81
1

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.

John Alexiou
  • 28,472
  • 11
  • 77
  • 133