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.
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
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.