The List Manager Project: Asking the User if he wants to save


Save Changes?

Open notepad and then close it. It does not ask you if you want to save. However if you type something, then either click new or open, it will ask if you want to save.

If you try to close the program you will also get the same message.

In order to know hwether there has been a change we need a global variable changed that we will set to true ifwe add or delete from the lsit box.

We will set changed to false when we save.

Add the variable changed to the global variables:

Public Class Form1
    Dim currentFile As String
    Dim Changed As Boolean = False

When an item is inserted in the list set changed to true: changed = True

When an item is deleted, set changed to true: changed = True

Because we will ask if they want to save from more than one place (when we remind them to save) we have written asktosave as a general function. That is, it is not a procedure that receives some event arguments as its parameters. It must be a function so that it can return that inforamtion to the calling procedure.

Code for AskToSave

Private Function AskToSave() As Boolean
Dim Answer As MsgBoxResult
Dim okToClear As Boolean = False 'will be the return value
If Changed Then
Answer = MsgBox("Do you want to save?", _
MsgBoxStyle.YesNoCancel + MsgBoxStyle.Question, "Save")
If Answer = MsgBoxResult.Yes Then
SaveList()
okToClear = True
End If
If Answer = MsgBoxResult.No Then
okToClear = True 'clear the list box without saving
End If
If Answer = MsgBoxResult.Cancel Then
okToClear = False
End If
End If 'if changed is false, then false is returned
Return okToClear
End Function 'AskToSave

INDEX, Introduction, Build the Menu, Add items to a listbox, Delete items from the listbox, Save: Write the contents of the list box to a file, Open File: Read file and add to list box, Ask the user if he wants to save, When to ask the user if he wants to save, The User closes the program
Next lesson: List Manager application Finishing Touches

Copyright © Zebra0.com
All rights reserved worldwide.

 
 

Ask the user if he wants to save