Yes, this is homework, I'm not asking for a cut/past answer, just a pointer. I think I've developed brain lock form staring at this issue for too long. I'm using VB.Net 2003.

I've created half the assignment, using a form to add records to a text file. The second half is to read from that text file. You do so by selecting the id number from a combo box which then displays the remainder of the information for the record. I'm stuck on how to create an array, filled with values from the text file. Not sure how to proceed with reading only the id number for the combo, and then reading the remainder once the combo is used. Yes, a database would be easier, but this assignment requires the use of a text file. I understand the use of StreamReader, I guess I'm stuck on the loop function to read data into an array, and then displaying the items from the array.

The data in the text file looks like this:

Record ID 1
Data 1
Data 1
Data 1
Record ID 2
Data 2
Data 2
Data2

Thanks to anyone who takes a stab at giving me a few pointers.

Dani AI

Generated

A simple, robust pattern is: parse the file once into an in-memory map keyed by the record ID, then bind the map keys to the ComboBox. On selection, pull the value (the list of lines for that ID) and populate the textboxes. This avoids re-reading the file on every selection and avoids pre-sizing arrays. As showed, reading into memory is the right direction, but a fixed 999-element array wastes work; use a growable collection and a lookup table instead.

Example parsing approach (VB.NET 2003-friendly): read line by line, treat lines that start the record (e.g. "Record ID...") as the key, and collect following lines into an ArrayList until the next key or EOF. Store each record as Hashtable(key -> ArrayList).

Dim records As New System.Collections.Hashtable()
Using sr As New System.IO.StreamReader("C:\path\records.txt")
    Dim currentKey As String = Nothing
    Dim currentList As System.Collections.ArrayList = Nothing
    While Not sr.EndOfStream
        Dim line As String = sr.ReadLine().Trim()
        If line = "" Then Continue While
        If line.ToLower().StartsWith("record id") Then
            currentKey = line
            currentList = New System.Collections.ArrayList()
            If Not records.ContainsKey(currentKey) Then records.Add(currentKey, currentList)
        ElseIf currentList IsNot Nothing Then
            currentList.Add(line)
        End If
    End While
End Using

Populate the ComboBox from records.Keys, and in the SelectedIndexChanged handler retrieve the ArrayList and copy elements into labels/textboxes (check .Count before indexing). Handle duplicate IDs (decide overwrite vs. append), trim whitespace, and skip empty lines.

For newer frameworks use File.ReadAllLines for a one-call read. See the .NET StreamReader documentation and ArrayList/Hashtable docs for details.

Recommended Answers

All 2 Replies

Here's a little tip that should be enough to get you going (but not enough to do it for you ;))

dim data(999) as string 'initialise the array
open "filename" for input as 1 'open the file
for d% = 0 to 999 'start the loop
if eof(1) = false then line input #1, data(d%) 'if the file hasn't ended read in the line
next d% 'end the loop
close 1 'close the file, it's messy to have it open

That's reading stuff into an array. To display it, perhaps you could use the ItemIndex (or whatever it's called) property and read data from the array like data(list.ItemIndex)?

Hope that makes sense,

That was one point I wasn't sure about, now to handle an unknown length for the array. It's variable as there will be no telling how many lines and records will be in the text file. You used 999 to initialize, but the loop checks for end of file, so the array begins at 999 elements, but could actuall only have 20 if that's all the text file contains, correct?

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.