Public Class Form1
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
If e.CloseReason = CloseReason.UserClosing Then
Dim NeedsSave As Boolean = False
'Perform contextual validations here and (re)set NeedsSave accordingly...
NeedsSave = True '<-- Testing value.
If NeedsSave Then
Dim result As DialogResult = MessageBox.Show("You have some unsaved changes. " _
& "Do you want to save them now?", "Unsaved Changes", MessageBoxButtons.YesNoCancel, _
MessageBoxIcon.Information)
Select Case result
Case Windows.Forms.DialogResult.Cancel
'Cancel the form closing.
e.Cancel = True
Return
Case Windows.Forms.DialogResult.Yes
If SaveChanges() = False Then
'The user should still be able to cancel out here so
'that hanges are not lost (as long as the user can
'perform the changes.
If MessageBox.Show("The save failed. Do you still want close?", _
"Save Failed", MessageBoxButtons.OKCancel) = Windows.Forms.DialogResult.Cancel Then
e.Cancel = True
Return
Else
'Allow the form to close.
End If
End If
Case Else
'Allow the form to close.
End Select
End If
End If
End Sub
Private Function SaveChanges() As Boolean
'Place save algorithm here...
'True = success, False means the save failed.
Return False '<--For testing.
End Function
End Class
Ето на някой ако му трябва в случай, че има желание да си го направи да го пита дали иска да се запазват промените преди изход от програмата.