Public Class Form1 Dim currentFile As String Dim changed As Boolean = False Private Sub InsertToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles InsertToolStripMenuItem.Click 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 DeleteItem() Handles DeleteToolStripMenuItem.Click 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 'DeleteItem 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 changed = False 'we just saved, so there are no changes since last save FileClose(1) MsgBox("Items saved: " & ListBox1.Items.Count) End If End Sub 'SaveList Private Sub SaveToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles SaveToolStripMenuItem.Click SaveList() End Sub 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 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 mnuExit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click Dim okToClose As Boolean = Changed If Changed Then okToClose = AskToSave() End If If okToClose Then End End If End Sub 'mnuExit_Click Private Sub Form1_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing 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 'Form1_FormClosing Private Sub FontToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles FontToolStripMenuItem.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 System.Object, e As System.EventArgs) Handles MyBase.Load ListBox1.Width = Me.Width - 30 'leave room for the scroll bars ListBox1.Height = Me.Height - ListBox1.Location.Y - 30 'below the menu End Sub Private Sub Form1_Resize(sender As Object, e As System.EventArgs) Handles Me.Resize ListBox1.Width = Me.Width - 30 'leave room for the scroll bars ListBox1.Height = Me.Height - ListBox1.Location.Y - 30 'below the menu End Sub Private Sub PrintDocument1_PrintPage(sender As System.Object, e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage Dim page As Graphics = e.Graphics 'the page will be drawn on the page Dim xpos As Integer = 50 '50 pixel margin on left Dim ypos As Integer = 80 '80 pixel margin at top Dim myFont As New Font("Arial", 12, FontStyle.Regular) 'You have to provide values when you create a New instance Dim myBrush As New SolidBrush(Me.ListBox1.ForeColor) 'Use the color of the font in the list box myFont = FontDialog1.Font 'Use the same font that was selected for the list box page.DrawString(currentFile, myFont, myBrush, xpos, ypos) 'draw the name of the file ypos = ypos + myFont.Unit * 10 'move down the y position before drawing the next line Dim i As Integer For i = 0 To ListBox1.Items.Count - 1 'each item in the list box page.DrawString(ListBox1.Items(i), myFont, myBrush, xpos, ypos) ypos = ypos + myFont.Unit * 10 'move down the y position before drawing the next line Next End Sub Private Sub PrintPreviewToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) _ Handles PrintPreviewToolStripMenuItem.Click Me.PrintPreviewDialog1.Document = PrintDocument1 Me.PrintPreviewDialog1.ShowDialog() End Sub Private Sub PrintToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles PrintToolStripMenuItem.Click If PrintDialog1.ShowDialog() = DialogResult.OK Then 'They didn't cancel PrintDocument1.PrinterSettings = PrintDialog1.PrinterSettings 'set up the output PrintDocument1.Print() 'send to the printer End If End Sub Private Sub AboutListManagerToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles AboutListManagerToolStripMenuItem.Click FrmAbout.ShowDialog() End Sub End Class