Reverse line reading order from text file into combobox

K

kimiraikkonen

Hello,
I was working on a project that a combobox reads each line from a text
file using this code:

Dim reader As String
reader = My.Computer.FileSystem.ReadAllText("history.txt")

Dim strs() As String
strs = Split(reader, Environment.NewLine)

For Each s As String In strs
combobox1.Items.Add(s)
Next

The problem is: I want to see the last line of text file ordered/
sorted as the "first" item in my combobox like the philosophy "the
last text line must be the most recent item for my combobox"? How can
i do this?

Regards.
 
O

\(O\)enone

kimiraikkonen said:
The problem is: I want to see the last line of text file ordered/
sorted as the "first" item in my combobox like the philosophy "the
last text line must be the most recent item for my combobox"? How can
i do this?

The way I would solve this is to use the IO.File.ReadAllLines() function to
read the text file into memory. This returns an array of strings, each one
of which will correspond to a line within the file. (This is effectively the
same as your ReadAllText() and then Split() calls, but in less code). Then
you can simply loop through the array backwards and add each line to the
textbox as you read it:

\\\
Dim i As Integer
Dim strs() As String

strs = IO.File.ReadAllLines("history.txt")

For i = UBound(strs) To 0 Step -1
combobox1.Items.Add(strs(i))
Next
///

HTH,
 
K

kimiraikkonen

The way I would solve this is to use the IO.File.ReadAllLines() function to
read the text file into memory. This returns an array of strings, each one
of which will correspond to a line within the file. (This is effectively the
same as your ReadAllText() and then Split() calls, but in less code). Then
you can simply loop through the array backwards and add each line to the
textbox as you read it:

\\\
Dim i As Integer
Dim strs() As String

strs = IO.File.ReadAllLines("history.txt")

For i = UBound(strs) To 0 Step -1
combobox1.Items.Add(strs(i))
Next
///

HTH,

(O)enone, that worked. However it was an interesting idea to sort
items by creating a string array, then the key point was to use
"UBound" and "Step" inside a for-next loop. Thank you.
 
S

SurturZ

I am fascinated by this thread.

It implies that kimiraikkonen routinely uses "For Each..." rather than
iterating through array indices.

I started programming on early forms of BASIC that pre-dated the inclusion
of "For Each... ". In fact I think they pre-dated UBOUND for that matter :)

I find it intriguing that newer programmers would start with "For Each" and
then go back to learning an index-based method. A bit like learning to drive
on an automatic transmission car then moving to a manual, I suppose :)
 
T

Tom Shelton

The way I would solve this is to use the IO.File.ReadAllLines() function to
read the text file into memory. This returns an array of strings, each one
of which will correspond to a line within the file. (This is effectively the
same as your ReadAllText() and then Split() calls, but in less code). Then
you can simply loop through the array backwards and add each line to the
textbox as you read it:

\\\
Dim i As Integer
Dim strs() As String

strs = IO.File.ReadAllLines("history.txt")

For i = UBound(strs) To 0 Step -1
combobox1.Items.Add(strs(i))
Next
///

HTH,

I would simplify this even farther.... You can completely do away with
the loop....

Dim strs() As String = IO.File.ReadAllLines("history.txt")
Array.Reverse (strs)
combobox1.Items.AddRange (strs)


HTH
 
O

\(O\)enone

Tom said:
I would simplify this even farther.... You can completely do away
with the loop....

Dim strs() As String = IO.File.ReadAllLines("history.txt")
Array.Reverse (strs)
combobox1.Items.AddRange (strs)

Aha, very nice! :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top