Sometimes when the user tries to exit a program you want something else to happen instead of just closing the program which it will try to do. I think the most common reason why you want to do this is when asking the users if they want to save their works, and if the user clicks cancel we want the program to stop the shutdown. To do this we'll use the event FromClosing like this:
[highlight=VB.NET] Private Sub frmMain_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
e.Cancel = True
End Sub[/highlight]
We use e.Cancel to choose if the closing action will be canceled, this is by default False. Note that since we don't cancel the action only when a condition is True this program will be impossible to shut down. We could therefor add a messagebox asking the user if he/she wants to save his/her unsaved work(not that we won't check if the worked is saved or not, we won't actually have anything to save at all).
[highlight=VB.NET] Private Sub frmMain_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
MessageBox.Show("You haven't saved your current work. All unsaved progress will be lost if you close, do you want to save?", "Exit", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1)
End Sub[/highlight]
So now we ask the user if it wants to save the work and gives it the options Yes, No and Cancel. When the user clicks one of these button the form will just close since we don't use the info we receives to do anything, we could do it something like this:
[highlight=VB.NET] Private Sub frmMain_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Dim choice As DialogResult = MessageBox.Show("You haven't saved your current work. All unsaved progress will be lost if you close, do you want to save?", "Exit", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1)
If choice = Windows.Forms.DialogResult.Cancel Then
e.Cancel = True
End If
End Sub[/highlight]
Now it will check if the user pressed cancel, if it did the form won't close since we've used e.Cancel = True just as we did before. We may also want it to save the file if we press yes, to do this we will use a SaveFileDialog to ask the user where it want to save the file:
[highlight=VB.NET] Private Sub frmMain_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Dim choice As DialogResult = MessageBox.Show("You haven't saved your current work. All unsaved progress will be lost if you close, do you want to save?", "Exit", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1)
If choice = Windows.Forms.DialogResult.Yes Then
Dim Save As New SaveFileDialog()
Save.ShowDialog()
If Save.FileName <> "" Then
IO.File.WriteAllText(Save.FileName, "Save this String")
Else
e.Cancel = True
End If
ElseIf choice = Windows.Forms.DialogResult.Cancel Then
e.Cancel = True
End If
End Sub[/highlight]
Now if the user clicks Yes we will show a SaveFileDialog so the user can select where to save the file, if the SaveFileDialog's file name is empty afterwards it means the user clicked cancel on the SaveFileDialog, then we should take the user back to the program by cancel the closing action using e.Cancel = True as always. If the user choice a file path we saves the string "Save this String" at that location, of course you'll replace this if you want to use it. Note that all this was only a simple example showing a good way to cancel the closing of a program/form.
Close Reason
We can also check the close reason, i.e. what ordered the program to close. We could use this if we want different things to happen depending on if the user closed the program on his/her own or if the program were closed since the computer was shut down. The different reasons' name talks pretty much for themselves, and they are:
[highlight=VB.NET] CloseReason.ApplicationExitCall
CloseReason.FormOwnerClosing
CloseReason.MdiFormClosing
CloseReason.None
CloseReason.TaskManagerClosing
CloseReason.UserClosing
CloseReason.WindowsShutDown[/highlight]
Do use it you use e.CloseRason in a condition, it could look something like this:
[highlight=VB.NET] If e.CloseReason = CloseReason.UserClosing Then
e.Cancel = True
End If[/highlight]
If we continue on the example I used before we can specify the close reason to make another thing happen if the computer is shutting down while the program is open:
[highlight=VB.NET] Private Sub frmMain_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
If e.CloseReason = CloseReason.UserClosing Then
Dim choice As DialogResult = MessageBox.Show("You haven't saved your current work. All unsaved progress will be lost if you close, do you want to save?", "Exit", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1)
If choice = Windows.Forms.DialogResult.Yes Then
Dim Save As New SaveFileDialog()
Save.ShowDialog()
If Save.FileName <> "" Then
IO.File.WriteAllText(Save.FileName, "Save this String")
Else
e.Cancel = True
End If
ElseIf choice = Windows.Forms.DialogResult.Cancel Then
e.Cancel = True
End If
ElseIf e.CloseReason = CloseReason.WindowsShutDown Then
IO.File.WriteAllText("AutoSave.txt", "Save this String")
End If
End Sub[/highlight]
So now we checks if the user tried to close the program, if so we will ask it if it wants to save. But if it closes since the computer is shutting down all applications to be able to shut down we just quickly autosaves all data in another file, then we can allow the user to retrieve the autosaved data next time it will start the program. If the reason is something else the program will just be closed as normal. And that was pretty much it, hope you learned anything new and finds this useful![]()
Nicely done, +rep!
Nice +rep
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks