I am writing a project that stores vehicle information such as the model,manuf., year, etc.
I have to create a second project that loads the data from the file into memory and loads a drop down combo box with the VIN numbers. When the number is selected from the box, it is supposed to display the appropriate information in labels. Im having problems storing my information and creating this project because the book doesn't explain it very well. Can anyone point me in the right direction?
cassie_sanford 1 Junior Poster in Training
Teme64 215 Veteran Poster
You didn't mention, how the information is stored. But if you have numbers in your combo box and you can fetch the information with that number, you need to trap SelectedIndexChanged for the combo box:
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
' Get selected VIN number
Dim ThisVINNumber As Integer
Try
ThisVINNumber = CInt(ComboBox1.Items(ComboBox1.SelectedIndex))
' Fetch information for this VIN and place it in the labels
Catch ex As Exception
' Handle exception
End Try
End Sub
Hope this gets you started. If you still have a problem, you need to tell more details about how you store your information (database, a file...)
cassie_sanford 1 Junior Poster in Training
Since i'm storing my information in a file, do i need to make a .txt file first and then write the program? Also, Since i'm creating two projects under the same thing, how would i go about making my forms? My first idea was to have a form for the user to enter in the data and then have another form that loads that information from the .txt file. I have in mind what I want to do but I just dont know the right way to execute it.
cassie_sanford 1 Junior Poster in Training
Here is the code that i have so far, but my .txt file won't execute:
Imports System.IO
Public Class createForm
'Declare module level variables.
Dim autoStreamWriter As New StreamWriter("Vehicles.txt")
Private Sub saveButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles saveButton.Click
If modelTextBox.Text <> "" And manufacturerTextBox.Text <> "" And yearTextBox.Text <> "" And vinNumTextBox.Text <> "" Then
autoStreamWriter = New StreamWriter("Vehicles.txt", True)
autoStreamWriter.WriteLine(modelTextBox.Text)
autoStreamWriter.WriteLine(manufacturerTextBox.Text)
autoStreamWriter.WriteLine(yearTextBox.Text)
autoStreamWriter.WriteLine(vinNumTextBox.Text)
autoStreamWriter.Close()
Else
MessageBox.Show("All fields must be entered before the information can be saved.")
End If
With Me
autoStreamWriter.WriteLine(modelTextBox.Text)
autoStreamWriter.WriteLine(manufacturerTextBox.Text)
autoStreamWriter.WriteLine(yearTextBox.Text)
autoStreamWriter.WriteLine(vinNumTextBox.Text)
With .modelTextBox
.Clear()
.Focus()
End With
.manufacturerTextBox.Clear()
.yearTextBox.Clear()
.vinNumTextBox.Clear()
End With
End Sub
Private Sub exitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles exitButton.Click
'Close the file and the form.
autoStreamWriter.Close()
Me.Close()
End Sub
End Class
Teme64 215 Veteran Poster
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 :)
cassie_sanford 1 Junior Poster in Training
Since i'm using two forms, how would i get the streamwriter to appear first? My plan was to have the createForm (streamwriter) to have a menu strip so that when the user selects like Open Summary or something, the other form will display for the user to go through the records using the vin number combo box
Teme64 215 Veteran Poster
You mean the data entry form is the "main" form? You can set menu item to disabled (Enabled = False).
Or you don't even have to do that. If the user selects "Open Summary" (and the file is empty) the user gets an empty summary form.
Here's a fixed "Read Data"-button, which checks first that the file exists. It also closes the StreamReader, a minor bug in my previous posting:
Private Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
'
Dim i As Integer
AutoCount = -1
If Not My.Computer.FileSystem.FileExists(FileName) Then
Exit Sub
End If
Try
autoStreamReader = New StreamReader(FileName)
Do Until autoStreamReader.EndOfStream
AutoCount += 1
ReDim Preserve Autos(AutoCount)
Autos(AutoCount).Model = autoStreamReader.ReadLine
Autos(AutoCount).Manufacturer = autoStreamReader.ReadLine
Autos(AutoCount).Year = autoStreamReader.ReadLine
Autos(AutoCount).VIN = CInt(autoStreamReader.ReadLine)
Loop
Catch ex As Exception
' Handle exception
Finally
autoStreamReader.Close()
End Try
'
For i = 0 To AutoCount
ComboBox1.Items.Add(Autos(i).VIN)
Next i
'
End Sub
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.