I think its a good idea to keep data entry and data viewing as separate forms.
Here's a bit fixing to data entry form (saveButton_Click). Replace control names (TextBox1 <- modelTextBox etc.) and add missing boxes.
' Declare writer
Private autoStreamWriter As StreamWriter
' Set file name as a constant value
Const FileName As String = "C:\Vehicles.txt"
' I use ValidateForm as a separate "form validator"
Private Function ValidateForm() As Boolean
'
If TextBox1.Text.Length = 0 Then
MessageBox.Show("Model is empty. All fields must be entered before the information can be saved.")
TextBox1.Focus() ' Move focus to missing field
Return False
End If
' Next Textboxes here
' Validation is Ok
Return True
End Function
' Save button
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
'
' Check fields
If Not ValidateForm() Then
Exit Sub
End If
Try
' Create writer
autoStreamWriter = New StreamWriter(FileName, True)
autoStreamWriter.WriteLine(TextBox1.Text)
autoStreamWriter.WriteLine(TextBox2.Text)
' Add missing WriteLine... here
autoStreamWriter.Close()
TextBox1.Clear()
TextBox2.Clear()
' Add missing Clear... here
' Set focus to Model box
TextBox1.Focus()
Catch ex As Exception
' Handle exception
' Can't open file etc. can occur, so trap exceptions
End Try
End Sub
And here's the code to read data. You may create an Auto class object but I used structure and array to hold data. This code goes in your "View data"-form.
' 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
Hope this gets you started
Reputation Points: 218
Solved Threads: 201
Veteran Poster
Offline 1,024 posts
since Aug 2008