' Data reader
Private autoStreamReader As StreamReader
Const FileName As String = "C:\Vehicles.txt"
' Structure to hold information for a one Auto object
Private Structure Auto
Dim Model As String
Dim Manufacturer As String
Dim Year As String
Dim VIN As Integer ' I did assume VIN is an integer number
End Structure
' Array of Auto objects
Private Autos() As Auto
' Number of items
Private AutoCount As Integer
' This "Read Data"-button
Private Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
' Add similar Try...Catch...End Try here too (file can be missing etc.)
Dim i As Integer
' Set count to -1 (no data)
AutoCount = -1
autoStreamReader = New StreamReader(FileName)
' Read the whole file
Do Until autoStreamReader.EndOfStream
' Increase counter
AutoCount += 1
' Redim array
ReDim Preserve Autos(AutoCount)
' Read one Auto object
Autos(AutoCount).Model = autoStreamReader.ReadLine
Autos(AutoCount).Manufacturer = autoStreamReader.ReadLine
Autos(AutoCount).Year = autoStreamReader.ReadLine
Autos(AutoCount).VIN = CInt(autoStreamReader.ReadLine)
Loop
' Fill a combo box with VINs
For i = 0 To AutoCount
ComboBox1.Items.Add(Autos(i).VIN)
Next i
'
End Sub
' A modified SelectedIndexChanged to select an Auto object and show its data
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
' Get selected Auto object
Dim ThisAuto As Auto
Try
ThisAuto = Autos(ComboBox1.SelectedIndex)
' Place information in the labels
Label1.Text = ThisAuto.Model
' and so on...
Catch ex As Exception
' Handle exception
End Try
End Sub