Zebra0.com

visual-basic files

We have not written any code for Exit yet. The exit button would be fairly simple, except that we would like to ask if they want to save. We would only ask this if there have been changes to the combo list. Instead of just using End for this procedure, we will give the user a chance to save if they haven’t saved since the last change. Almost every program will remind you if you didn’t save. The additions below will implement the "Save Reminder". A variable Changed will be used to keep track of whether there have been changes since the last save In order to know if there have been any changes, we need to do the following;

  1. Declare a global variable Changed as Boolean: Dim Changed As Boolean = False (under Public Class Form1)
  2. Set changed to true if we add or delete. Changed = True (in DeleteItem and in AddItem if the item is not blank.)
  3. Set changed to false whenever we save. Changed = False (in SaveList)

After adding changed, write the code for exit as shown below:

Private Sub mnuExit_Click(ByVal sender As Object, ByVal e As System.EventArgs) _
    Handles MnuExit.Click
  AskToSave()
End Sub 'mnuExit_Click

Private Sub AskToSave()
  Dim Answer As MsgBoxResult
  If Changed Then
     Answer = MsgBox("Do you want to save?", _
       MsgBoxStyle.YesNoCancel + MsgBoxStyle.Question, "Save")
     If Answer = MsgBoxResult.Yes Then
        SaveList()
        End 'end the program after saving
     End If
     If Answer = MsgBoxResult.No Then
        End 'end the program without saving
     End If
     'Do NOT end if they cancel!
  Else
     End 'end the program if no changes
  End If
End Sub 'AskToSave 
Test all of the options and save all of the files in the application.

NEXT: Browse for Folder