(semi-beginner) StreamReader Help

M. Nicholas

New member
Joined
May 7, 2006
Messages
2
Programming Experience
Beginner
Hello,

I’m trying to create a program will supply information for variables from a text file, so that as the possibilities change, a simple text file will need to be amended, rather than rewriting the original code. I am reading about FileStream and StreamReader, which seem like just the ticket.

For instance, if I wanted to populate a TextBox with items I wrote in a text file, such as:

Tom
Mike
Jim
Jean

I threw together a sub like this:
------------------------------
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click

Dim objFS As FileStream
Dim objSR As StreamReader
Dim TextSays As String

objFS = New FileStream("C:\test\test.txt", FileMode.Open, FileAccess.Read)
objSR = New StreamReader(objFS)
TextSays = objSR.ReadToEnd
objSR.Close()
TextBox1.Text = TextSays

End Sub
------------------------------

This works perfect. Now, I need the ability to categorize my text file. For instance:

[my family start]
Tom
Mike
Jim
Jean
[my family end]

[your family start]
Bob
Dave
Ted
Steve
[your family end]

I am trying to figure out how to tell the streamreader to start reading in one category, and finish reading at the end of that category. My end result will be a form that I design to have every possible category available on the form, but the items listed for each category to be easily changed or added to by the end user via a simple text file.

Any suggestions would be appreciated
 
I have a solution, and thought perhaps someone might be interested in my solution…or even pointing out potential issues with my solution.

…but it does work.

In the interest of full disclosure, I did receive a suggestion on another forum (thank you, ”spotty”). I deconstructed it, then rebuilt it, and came up with a similar approach.

(test.txt file that is similar (but smaller) than the one I might use to populate some variables)

-----------------
[Start family:A]
Mike
Jim
Tom
Jean
[End family:A]

[Start family:B]
Steve
Dave
Ted
Bob
[End family:B]
-----------------

The solution:

Imports System.IO

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim objFS As New FileStream("C:\test\test.txt", FileMode.Open, FileAccess.Read)
Dim objSR As New StreamReader(objFS)
Dim TextSays As String
Dim StartString As String = "[Start family:B]" 'start marker
Dim EndString As String = "[End family:B]" 'end marker

Do While Not objSR.EndOfStream
TextSays = objSR.ReadLine 'reads first (or next) line
ListBox1.Items.Add(TextSays) 'adds to ListBox1
If TextSays = StartString Then 'category start marker
ListBox1.Items.Clear() 'clears it, and everything before
ElseIf TextSays = EndString Then 'finds end marker
ListBox1.Items.Remove(TextSays) 'removes end marker
Exit Do 'exits do loop before entering another category
End If
Loop
objFS.Close()

End Sub


So, of course, for each particular category I might make on this text file, I would need a new button. But, the goal is to have a way to easily change or add to any given category without changing the program code.

Mike
 
Back
Top