'Programmer: Janet Joy 'List manager program with menu Imports System.ComponentModel Public Class Form1 Dim currentFile As String Dim changed As Boolean = False Private Sub ExitToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles mnuExit.Click 'Exit if there are no changes or the user doesn't want to savr Dim okToClose As Boolean = changed If changed Then okToClose = AskToSave() End If If okToClose Then End End If End Sub Private Sub mnuInsert_Click(sender As Object, e As EventArgs) Handles mnuInsert.Click 'Add an item to the list box Dim s As String s = InputBox("Enter item", "Add Item") If s <> "" Then ListBox1.Items.Add(s) changed = True End If End Sub Private Sub mnuDelete_Click(sender As Object, e As EventArgs) Handles mnuDelete.Click 'Delete if there are items and one is selected If ListBox1.Items.Count > 0 And ListBox1.SelectedIndex >= 0 Then ListBox1.Items.RemoveAt(ListBox1.SelectedIndex) changed = True Else ListBox1.Text = "" End If If ListBox1.Items.Count > 0 Then ListBox1.SelectedIndex = 0 End If End Sub Private Sub SaveList() Dim S As String Dim Num As Integer If currentFile = "" Then SaveFileDialog1.ShowDialog() currentFile = SaveFileDialog1.FileName End If If currentFile <> "" Then FileOpen(1, currentFile, OpenMode.Output) For Num = 0 To ListBox1.Items.Count - 1 S = ListBox1.Items.Item(Num) PrintLine(1, S) Next Num FileClose(1) MsgBox("Items saved: " & ListBox1.Items.Count) End If End Sub 'SaveList Private Sub mnuSave_Click(sender As Object, e As EventArgs) Handles mnuSave.Click SaveList() changed = False End Sub 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 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 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 Private Sub Form1_Closing(sender As Object, e As CancelEventArgs) Handles Me.Closing 'The usre clicked the X in top right corner, remind to save Dim closeApp As Boolean If changed Then closeApp = AskToSave() If closeApp = False Then e.Cancel = True 'Don't close the app! End If End If End Sub Private Sub mnuFONT_Click(sender As Object, e As EventArgs) Handles mnuFONT.Click On Error Resume Next 'There are some fonts that can't be used in a list box FontDialog1.ShowDialog() ListBox1.Font = FontDialog1.Font ListBox1.ForeColor = FontDialog1.Color End Sub Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load FontDialog1.Font = Me.Font 'Same font as the form resizeListBox() End Sub Private Sub mnuViewHelp_Click(sender As Object, e As EventArgs) Handles mnuViewHelp.Click 'Open the webpage where help can be found System.Diagnostics.Process.Start("www.zebra0.com") 'Webpage where the help info can be found End Sub Private Sub Form1_Resize(sender As Object, e As EventArgs) Handles Me.Resize resizeListBox() End Sub Private Sub resizeListBox() 'On form load and when the form is resized the list box fills the form ListBox1.Width = Me.Width - 20 'leave room for the scroll bars on listbox ListBox1.Height = Me.Height - ListBox1.Location.Y - 30 'below the menu End Sub End Class