Zebra0.com

visual-basic listmanager

When the user clicks the open menu item, we will open a file for input. We will read the file line by line and add each item to the list box.

From the toolbox, add the openOpenFileDialog control to the project. (It will be named OpenFileDialog1 and appear below the form because it is not visible at run time.) Double Click on the "Open" menu item and add the code as shown below:

Code for Opening a File

Private Sub mnuOpen_Click(sender As Object, e As EventArgs) Handles mnuOpen.Click
'Opens selected file and adds to list box
  If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
    currentFile = OpenFileDialog1.FileName
    Dim S As String = ""
    Me.Text = currentFile
    ListBox1.Items.Clear() 'remove everything from previous list
    FileOpen(1, currentFile, OpenMode.Input) 'open the file for input
    While Not EOF(1) 'EOF=End of file, this loops to read all lines in the file
      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 'select the first itme
    End If
   End If
End Sub
See the complete code at this point.

NEXT: Build the Menu