+ Reply to Thread
Results 1 to 3 of 3

Thread: Prevent programs from closing - VB.NET

  1. #1
    Join Date
    Apr 2009
    Location
    Uppsala, Sweden
    Posts
    9,547
    Blog Entries
    5
    Rep Power
    98

    Prevent programs from closing - VB.NET

    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

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Jordan Guest

    Re: Prevent programs from closing - VB.NET

    Nicely done, +rep!

  4. #3
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: Prevent programs from closing - VB.NET

    Nice +rep
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Prevent XSS in ASP.NET
    By Showstopper in forum Security Tutorials
    Replies: 1
    Last Post: 07-22-2009, 04:46 PM
  2. Form Closing
    By AlexanderM in forum C# Programming
    Replies: 1
    Last Post: 03-31-2008, 09:42 AM
  3. Closing Ports! - Help
    By travy92 in forum Visual Basic Programming
    Replies: 20
    Last Post: 09-17-2007, 04:30 PM
  4. Closing Form without gap
    By NeedHelp in forum HTML Programming
    Replies: 1
    Last Post: 02-15-2007, 02:14 PM
  5. closing the cd rom
    By chinni in forum C# Programming
    Replies: 2
    Last Post: 10-16-2006, 02:38 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts