A practice exercise requires the creation of a text file with 4 lines of info for cars, VIN, model, manufacturer, and year repeated over and over for different vehicles. This I do with StreamWriter and have a simple multiline text file as follows:
111111
Echo
Toyota
2005
222222
Accord
Honda
2001
etc.(VINs simplified)
.....each line being read from a specific textBox on the form with .WriteLine.
This is simple and straight forward.
The next part of the exercise requires a form on which the VIN numbers are loaded into a comboBox and when a certain VIN is selected, the rest of the car info is displayed in a control on the form.
I cannot grasp the programming logic required to do this.
The best I've come up with is a StreamReader to read the saved file into an array.
For just the two simple entries above I use...
StreamReader fileReader = new StreamReader("C:\\car.txt");
string[,] carArray = new string[2,4];
// Load the array.
for(int rowi = 0; rowi < 2; rowi++)
{
for(int coli = 0; coli < 4; coli++)
{
carArray[rowi, coli] = fileReader.ReadLine();
}
}
I then load my combobox list of the VINs directly from the subscript like so:
comboBox1.Items.Add(carArray[0,0]);
comboBox1.Items.Add(carArray[1,0]);
I can then use the selectedItem.ToString() literal text value of the combo box to load
a list box to display the other relevent info pertaining to that VIN, again by direct subscript reference.
if(comboBox1.SelectedItem.ToString() == "111111");
{
listBox1.Items.Add(array[0, 1]);
listBox1.Items.Add(array[0, 2]);
listBox1.Items.Add(array[0, 3]);
}
I do a similar thing, with a listBox1.Clear(); preceeding the .Add statements to display the next array row for the Accord.
This code works fine for two cars but is very impractical for a long listing of cars.
Of course, a single dimension array of [8] will work as well when done this way.
I would very much appreciate suggestions on a better approach to this exercise bearing in mind the single line structure of the text file. the VINs must be in the comboBox list, and the related car info is to be displayed in a listBox for the selected VIN of the comboBox. Thanks to anyone with any suggestions.