I am a student finishing a class in Visual Basic, and would like some assistance with a program I am working on. I need to read the contents of a file (showing makes of cars on one line and a daily rental fee on the following line) into an array and then display the file contents in a list box. The user then chooses the car he/she wants to rent. I then need to obtain the rental fee from the array and use that value to calculate the total rentals. I have the file writing to an array, and the array going into the listbox, but the display shows blank lines before the first entry. The last file entry is followed by more blank lines, and then a display of system string() on following lines. I cannot seem to find where these extra lines etc are coming from - am sure there is an error in my code somewhere but cannot find it. My code is as follows:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim sFileToCheck As String = (Application.StartupPath & "\vehicles.doc")
Dim word As String
Dim items As String()
Dim i As Integer
Dim upperBound As Integer
LstRent.Items.Clear()
If Not File.Exists(Application.StartupPath & "\vehicles.doc") Then
MessageBox.Show("Cannot find the file. Please try again!", "File Error Warning", _
MessageBoxButtons.OK, MessageBoxIcon.Warning)
End If
Dim sr As IO.StreamReader = IO.File.OpenText(Application.StartupPath & "\vehicles.doc")
upperBound = 0
Do While (sr.Peek <> -1)
LstRent.Items.Add(sr.ReadLine)
upperBound += 1
Loop
sr.Close()
ReDim items(upperBound) ' states size of array and adds entries from the file
sr = IO.File.OpenText(Application.StartupPath & "\vehicles.doc")
For i = 1 To upperBound
items(i) = sr.ReadLine
Next
sr.Close()
LstRent.Items.Add(items)
DateRange1.begindate = "Pick-up Date:"
DateRange1.enddate = "Return Date:"
End Sub
I know I need to get the index of the car the user selected, and then get the index of the price by using adding one to the car index, but am not sure how to do that part of the code. Any suggestions would be welcome!!