Hi. I have few Ideas how to behave when user closes form and material is edited. Example is from my text editor and has few innovations. Just enough to read comments.
Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
Dim s As String = Me.Paper.Text
If s <> Nothing AndAlso Len(s) > 0 Then 'If text is empty don't ask, simply shutdown
If FilePath <> Nothing Then
If IO.File.Exists(FilePath) Then
If ReadF(FilePath) = s Then 'If text and file opened are identical simply shutdown
Exit Sub
End If
End If
End If
'Text and file are different, offer user to forget changes, save them somehow, or cancel shutdown
Select Case MsgBox("You have unsaved work opened. Save it?", MsgBoxStyle.YesNoCancel, "Warning")
Case MsgBoxResult.Yes 'User vishes to save work
If FilePath <> Nothing Then 'If we are editing some file
If IO.File.Exists(FilePath) Then
If lFile.rw.FileSize(FilePath, -1) = 0 Then
WriteF(FilePath, s) 'If opened file is empty and text is full, ovw auto
ElseIf lFile.rw.StartsWith(s, FilePath, False) Then
WriteF(FilePath, s) 'If opened file is older version of text ovw
Else
Select Case (New dlgFileExists).ShowDialog 'Offer Save,Append,Save As and cancel
Case DialogResult.OK : WriteF(FilePath, s)
Case DialogResult.Yes : AppendF(FilePath, s)
Case DialogResult.Abort
fmiSaveAs.PerformClick()
'If in saveas was submitted selectfiledialog[sfd] (and file saved)
'FilePath was filled with path
'no filepath indices cancel of sfd so rather ask to really shutdown application
If FilePath = Nothing Then CancelExitPrompt(e)
Exit Sub
Case DialogResult.Cancel 'this is second place for slower user to cancel saving changes
CancelExitPrompt(e) 'so rather ask to continue shutdown
Exit Sub
End Select
End If
Else 'file vanished while editing, for possible reasons of such operation
fmiSaveAs.PerformClick() 'rather offer Save As instead Save
If FilePath = Nothing Then CancelExitPrompt(e)
End If
Else 'no file was edited, so this is classical time to offer SaveAs
fmiSaveAs.PerformClick()
If FilePath = Nothing Then CancelExitPrompt(e)
End If
Case MsgBoxResult.Cancel 'For first "Shutdown-modified dialog" it is normal to cancel shutdown on cancel
e.Cancel = True
End Select
End If
End Sub
No one has replied yet! Why not be the first?
Sign in or Join us (it's free).