StreamReader

SonicBoomAu

Well-known member
Joined
Oct 30, 2003
Messages
179
Location
Australia
Hi All,

I am trying to search through a text file for heading [TYPE1] and then load the information under that heading into a combo box. Is this possible and what method would I use?

Here is an example of the text file:

[TYPE1]
Type A
Type B
Type C
Type D

[TYPE2]
Type A
Type B
Type C
Type D

[TYPE3]
etc....


The only way I can think of is using the following:

[VB]
Dim sr As StreamReader = File.OpenText(strFullPath)
Dim input As String

input = sr.ReadLine
If input = "[TYPE2]" Then
loop through until input = ""
Somehow load the information into a combo box.
End If
[/VB]
 
After a bit of reading and searching the internet I stumbled across that helped and I was able to come up with the following:

[VB]

Dim sr As StreamReader = File.OpenText(strFullPath)
Dim input As String

Do
input = sr.ReadLine()
Loop Until input = "[TYPE2]"

Do
input = sr.ReadLine()
Me.cboSystem.Items.Add(input)
Loop Until input = ""
[/VB]

It works, but is there a better way of doing this?
 
michael_hk said:
Use one loop is enough

Code:
Do
    Me.cboSystem.Items.Add(sr.ReadLine())
Loop Until input = "[TYPE2]"
But wont that populate the combo box with everything before "[TYPE2]".

Where as I require everything after "[TYPE2]" but not after "[TYPE3]", if that makes sense.
 
SonicBoomAu said:
I am trying to search through a text file for heading [TYPE1] and then load the information under that heading into a combo box.
You said you just want everything under [TYPE1].

i.e.
Type A
Type B
Type C
Type D
 
You said you just want everything under [TYPE1].

"I am trying to search through a text file for heading [TYPE1]" [TYPE1] here was an example. Sorry If this got confusing. :) But

In my Code Example.

If input = "[TYPE2]" Then
loop through until input = ""
Somehow load the information into a combo box.
End If
 
The code example looks fine. The only improvement that I can think of would be to have an if statement in the loop.
Code:
Do
  If someboolean Then  is true when your type is found.
    read line into line of text
    If lineoftext.Length = 0 Then
      someboolean = False  stop adding to combobox.
    Else
      add line to combobox
    End If
  Else
    read line of text
    If lineoftext = "[TYPE2]" Then
      someboolean = True  Start adding to combobox.
    End If
  End If
Loop Until SR.Peek = -1
something like that. It looks bigger, but its only one loop.
 
Back
Top