Question: Visual Basic ch7 array Create a Visual Basic program that will display car inventory information obtained from a text file called C:VB2012CarData.txt. When the form
Visual Basic ch7 array
Create a Visual Basic program that will display car inventory information obtained from a text file called C:\VB2012\CarData.txt. When the form loads, read all lines of the text file in to a string array called lines(). This will be a properly formatted text file in CSV format No data validation will be necessary. I will make the file available to you you should put it into a folder called C:\VB2012 on your machine. If at run time that file does not exist your program should say so and quit. Each CSV line of that file will hold: a VIN number (String), a Make (String), a Model (String), a Year (Integer), a Purchase Price (Decimal), and a Sale Price (Decimal). Sample line: 5674,VW,Squareback,1969,500,700.
Your program MUST create a structure called Cardata that holds all of the above information about a single car and MUST use an array called cars() that holds all of our car-records (i.e., structures). Split each line in the lines array and store each data item from the split to the correct car and fieldname in the cars array. Use LINQ to display all of the cars from this array to a DataGridView, showing the Make (String), Model (String), Year (Integer), and Age (2015 Year) of each car. Format the DataGridView by renaming the column headings so that each one is in all caps (MAKE, MODEL, YEAR, and AGE), removing the row headers, and resizing DGV (design view) and column widths (at runtime) so that there are no scroll arrows.
Note: Display includes calculated column Age and does not include columns for Purchase Price and Sale Price. Initially show the cars in the order that they appeared in the data file. Age of car from sample would be 46 (2015 1969). The form should look like this after it is loaded:
Additionally, your form will have 3 buttons that support the following activities: 1) Sort by Make This uses LINQ to update the DataGrid view to display the car data sorted by Make then by Model in ascending alphabetical order. When this button is clicked, it should be disabled while the other view is enabled (it should be enabled when the other view is clicked).
2) Sort by Age This uses LINQ to update the DataGrid view to display the car data sorted by Age in descending (oldest to newest) order. When this button is clicked, it should be disabled while the other view is enabled (it should be enabled when the other view is clicked).
this is smiller to my programs
Public Class CarDataForm
' Authors: ISC/ITE 285 Faculty
' Purpose: This code demonstrates several things.
' (1) How to declare a structure (Car)
' (2) How to create an array of structures.
' (3) How to use a CSV file to initialize an array of structures.
' (4) How to use a LINQ query to select multiple elements
' (5) How to set up a DataGridView (in code) to alter (a) its headers
' (b) the formatting of a column, (c) if row-headers show or not.
' (6) How to use DataSource to load results of a LINQ query into a DGV.
Structure Car
Dim name As String
Dim year As Integer
Dim weight As Double
Dim retailCost As Decimal
End Structure
Dim myArrayOfCars() As Car ' Will size it later based on file read in.
Dim firstTime As Boolean = True
Private Sub doSetUp() ' Executed only once.
' We can convert field names to other phrases for column headers
DataGridView1.Columns("name").HeaderText = "Car Name"
DataGridView1.Columns("year").HeaderText = "Model Year"
DataGridView1.Columns("weight").HeaderText = "Weight"
DataGridView1.Columns("retailCost").HeaderText = "Retail Cost"
' Below we set our DGV to show money (currency) in this column.
' Another potential value to use here is: "d" (for date).
DataGridView1.Columns("retailCost").DefaultCellStyle.Format = "c"
DataGridView1.RowHeadersVisible = False
firstTime = False
End Sub
' The following is a utility function that returns a Car given
' the car data in CSV format: name,year,weight,retailCost [per line].
Private Function CSVtoCar(ByRef oneLine As String) As Car
Dim aNewCar As Car
Dim parts() As String
parts = oneLine.Split(","c)
aNewCar.name = parts(0)
aNewCar.year = CInt(parts(1))
aNewCar.weight = CDbl(parts(2))
aNewCar.retailCost = CDec(parts(3))
Return aNewCar
End Function
' The following is a utility function that returns a CSV line for a
' car. Output is in format: name,year,weight,retailCost. Note:
' this method is not actually used in this program, but would be useful
' if you were saving an array of cars out to a CSV file.
Private Function CarToCSV(ByRef oneCar As Car) As String
Return oneCar.name & "," & oneCar.year & "," &
oneCar.weight & "," & oneCar.retailCost
End Function
Private Sub CarDataForm_Load(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Load
' Confirm that the data file exists. Exit if it does not.
If Not IO.File.Exists("C:\VB2010\CarData.txt") Then
MessageBox.Show("Failed to find input file. Program Terminates.")
Me.Close()
End If
Dim lines() As String
lines = IO.File.ReadAllLines("C:\VB2010\CarData.txt")
' Make myArrayOfCars be the same size as lines.
ReDim myArrayOfCars(lines.Count - 1)
' Break lines apart and use parts to fill a Car Record in array.
For i As Integer = 0 To lines.Count - 1
myArrayOfCars(i) = CSVtoCar(lines(i)) ' line to Car conversion.
Next
End Sub
Private Sub ByYearButton_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles ByYearButton.Click
Dim byYearQuery = From aCar In myArrayOfCars
Order By aCar.year
Select aCar.name, aCar.year, aCar.weight, aCar.retailCost
' NOTE: in the above Select clause we cannot replace the list with "aCar".
DataGridView1.DataSource = byYearQuery.ToList
DataGridView1.CurrentCell = Nothing ' Make no cell "selected"
If firstTime Then
doSetUp()
End If
End Sub
Private Sub ByNameButton_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles ByNameButton.Click
Dim byNameQuery = From aCar In myArrayOfCars
Order By aCar.name
Select aCar.name, aCar.year, aCar.weight, aCar.retailCost
' Note: above line selects multiple elements with a comma to separate them.
DataGridView1.DataSource = byNameQuery.ToList
DataGridView1.CurrentCell = Nothing ' Make no cell "selected"
If firstTime Then
doSetUp()
End If
End Sub
End Class ' CarDataForm
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
