We have a function that asks 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 mnuNew_Click(sender As Object, e As EventArgs) Handles mnuNew.Click
'Start a new list if not changed or user doesn't want to save
Dim canClear As Boolean = True
If changed Then canClear = AskToSave()
If canClear = True Then
ListBox1.Items.Clear()
currentFile = ""
changed = False
End If
End Sub
Menu Open was clicked (changed!):
Private Sub mnuOpen_Click(sender As Object, e As EventArgs) Handles mnuOpen.Click
'Opens selected file and adds to list box if not changed or user doesn't want to save
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.