By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,554 Members | 1,146 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,554 IT Pros & Developers. It's quick & easy.

How do I modify this code to read additional lines

P: 11
I have a car.txt file and the first line is vin no., second line is model, third line is mfg, and 4th line is year. I may have an unlimited no. of vin numbers and I need the code to read the vin no. (1st line,5th line, etc.) and display the vin number in a combo box. Once I check the vin no. the other 3 labels fill with the corresponding info for that vin. I got the code to read one vin but everytime I put in the Do While/loop or do/loop while, I am missing something and I either get nothing or an error. Please advise on how to adjust this. This code reads the second line of vin no. and info. Thanks.


Private Sub DisplayRecord()
'Read and display the next record.
Dim vinString As String
Do While vinStreamreader.Peek <> -1
vinString = vinStreamreader.ReadLine()
Me.ComboBox1.Text = (vinString)

Me.Label4.Text = vinStreamreader.ReadLine()
Me.Label5.Text = vinStreamreader.ReadLine()
Me.Label6.Text = vinStreamreader.ReadLine()

Loop


End Sub
Nov 26 '07 #1
Share this Question
Share on Google+
4 Replies


Expert 5K+
P: 8,434
This won't solve your problems, but one step along the way would be to insert the VIN as another item in the list attached to the combo box, rather than just placing it in the current text. Try the .Add or .AddItem method on the combo box (I forget what it's called in the .Net flavour of VB).
Nov 26 '07 #2

P: 11
Thanks for the help. That did list all 3 vin numbers in the combo box. Any suggestions on how to get the labels below to change when I change the vin numbers up above? I have only one set of labels for labels 4,5, and 6. They need to change when the vin number is selected in the combo box.
Attached is the revised code as you suggested. Jo Ann


Expand|Select|Wrap|Line Numbers
  1. Private Sub DisplayRecord()
  2.         'Read and display the next record.
  3.         Dim vinString As String
  4.         Do While vinStreamreader.Peek <> -1
  5.             vinString = vinStreamreader.ReadLine()
  6.             Me.ComboBox1.Text = vinString
  7.             Me.ComboBox1.Items.Add(ComboBox1.Text)
  8.  
  9.             Me.Label4.Text = vinStreamreader.ReadLine()
  10.             Me.Label5.Text = vinStreamreader.ReadLine()
  11.             Me.Label6.Text = vinStreamreader.ReadLine()
  12.  
  13.         Loop
  14.  
  15.  
  16. End Sub
Nov 26 '07 #3

Expert 5K+
P: 8,434
Well, I don't want to go into too much detail. After all, it's best if you think about it and figure it out, as you'll get a much better understanding of how it all works. But in general terms, I'd say the two most obvious ways to approach this would be...
  1. When an entry is selected from the combobox (actually, a listbox might be preferable, but that's up to you) just re-read the file and look through the entries until you find the one you want, then copy the subsequent lines into the textboxes. This way is fairly simple to code, but re-processing a file like this would probably be considered a very inefficient technique.
  2. As you read the file the first time, place all the values into an array (either a separate array for each field, or a slightly more complex structure). When user selects an entry in the list, look it up in the array (or just use the index number from the list) and copy the values form the array(s) to the textboxes. This technique may be slightly more complex to code and requires more RAM, but only requires the file to be read once.
Though I suspect it won't matter in this particular case, in the real worl you might be dealing with huge quantities of data. As the developer you would normally need to weight up various factors such as speed of execution and the amount of resources (RAM) used.
Nov 26 '07 #4

P: 1
To rephrase Killer42's 1st suggestion:

Assign the clicked list item to a string variable.
Then use an If statement as the loop control, to compare the vin number variable that is being read to the read line statement.

Set it up so the loop only outputs the information to the text boxes when the clicked item and the read vin number matches (are =).
Nov 26 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.