The List Manager Project: When to Ask User if he wants to save


We have a function that ask the user if he wants to save. If he says yes we save, then we return to the calling function whether it is ok to clear.

Menu New was clicked:

Private Sub NewToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles NewToolStripMenuItem.Click
   Dim canClear As Boolean
   If Changed Then
      canClear = AskToSave()
      If canClear = True Then
         ListBox1.Items.Clear()
         currentFile = ""
      End If
    End If
End Sub

Menu Open was clicked (changed!):

   Private Sub OpenToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles OpenToolStripMenuItem.Click
        Dim oktoClear As Boolean = True 'this will be the value if there has been no change
        If Changed Then
            oktoClear = AskToSave()
        End If
        If oktoClear Then
            ListBox1.Items.Clear()
            If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
                currentFile = OpenFileDialog1.FileName
                Dim S As String = ""
                Me.Text = currentFile
                FileOpen(1, currentFile, OpenMode.Input) 'open the file for input
                While Not EOF(1) 'EOF=End of file, this loops to read all recordds
                    S = LineInput(1) 'read from file 1
                    Me.ListBox1.Items.Add(S) 'add the item read to the combo box
                End While
                FileClose(1) 'close file 1
                If Me.ListBox1.Items.Count > 0 Then 'make sure the file was read
                    Me.ListBox1.SelectedIndex = 0
                End If
                Changed = False
            End If
        End If
    End Sub
See the complete code at this point.
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.

 
 

When to ask the user if he wants to save