'_________________________________________________________________________________ ' S T R E A M R E A D E R / W R I T E R D E M O ' Created 11/9/06 ' Ron Kessler ' C# 2005 Version '_________________________________________________________________________________ 'Last updated 11/9/06 'FEATURES: ' 1. Shows how to read/write to a sequential text file ' 2. Lets user create file when program first loads ' 3. Items can be added/removed from list box ' 4. Items in list box can be saved to disk ' 5. Shows how to use For-Each loop with list box items ' 6. Uses structured error handling and the Using Structure block to close the readers automatically! Imports System.IO Public Class frmMain Dim diskFile As String = "..\..\inventory.txt" 'our datafile remember to include the ..\ bit! '---this next flag variable keeps them from loading LB more than once. Is set in DoReload and 'reset in btnClear_Click Dim listBoxAlreadyLoaded As Boolean = False Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load '---fill the list box from file DoReload() End Sub Private Sub btnReload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReload.Click DoReload() End Sub Private Sub DoReload() If listBoxAlreadyLoaded Then Exit Sub 'keeps them from duplicating stuff '---define our scratch variables here Dim dataFromFile As String = "" 'holds the line of text from the file '---if file does not exist lets create it for them if they want to. If File.Exists(diskFile) = False Then If MessageBox.Show _ ("Cannot locate the inventory file" & Err.Description & "Create It?", _ "Ron's Train Store", MessageBoxButtons.YesNo, MessageBoxIcon.Error) = _ Windows.Forms.DialogResult.Yes Then File.Create(diskFile) Else Me.Close() End If Else Try Using myReader = New StreamReader(diskFile) 'this will close/dispose of reader automatically Do Until myReader.EndOfStream 'or Do Until dataFromFile Is Nothing dataFromFile = myReader.ReadLine lstItems.Items.Add(dataFromFile) Loop End Using listBoxAlreadyLoaded = True 'keeps them from duplicating items Catch ex As IOException MsgBox("An error occurred while reading...." & Err.Description, MsgBoxStyle.Critical) Me.Close() End Try End If End Sub Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click Dim lbItem As String = "" Try '---if file does not exist, the SW will create it Using myWriter = New StreamWriter(diskFile, False) 'false= do not append True= append '---now cycle through the items in the listbox and save them to disk For Each lbItem In lstItems.Items myWriter.WriteLine(lbItem) Next End Using MsgBox("Your data has been updated", MsgBoxStyle.Information, "Ron's Train Hut") Catch ex As IOException MsgBox("An error occurred while saving your data...." & Err.Description, MsgBoxStyle.Critical) Me.Close() End Try End Sub Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click '---add new item Dim myNewItem As String = txtNewItem.Text.ToString.Trim lstItems.Items.Add(myNewItem) End Sub Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click '---clear listbox and reset flag so they can read file again lstItems.Items.Clear() listBoxAlreadyLoaded = False End Sub Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click '---if they selected an item then SelectedIndex =>0 so if it is -1 they didn't select one to delete yet! If lstItems.SelectedIndex <> -1 Then lstItems.Items.Remove(lstItems.SelectedItem) End If End Sub Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click Me.Close() End Sub End Class