Files in Visual Basic
The "To-Do List" Program
This program reads and writes to a sequential file. Each line in the file is added to the ComboBox
- Using Notepad, create a short list of about 5 items and save it as C:\todo.txt.
- Start a new Windows application, name it ToDo.
- Add a ComboBox to the form. Name it CboToDo.
- Add the following code to form load:
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim S As String = ""
FileOpen(1, "C:\todo.txt", 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.CboTodo.Items.Add(S) 'add the item read to the combo box
FileClose(1) 'close file 1
If Me.CboTodo.Items.Count > 0 Then 'make sure the file was read
Me.CboTodo.SelectedIndex = 0 'display 1st item in list
End Sub 'Form1_Load