Reading text file into a ListBox

Hi, everyone,

I am trying to read a text file into ListBox. I envision that the code should be like this


Dim t As TextInputStream
Dim f As FolderItem
Dim dlg As OpenDialog
Dim x as String

dlg = New OpenDialog

f = dlg.ShowModal() // open open dialog for selecting a file to load

if f<> nil then
  t = f.OpenAsTextFile // open selected text file

x=t.ReadLine // some code  that adds a row to the ListBox1 needs to go here
Listbox1.AddRow(x)

  t.Close
else
  //user cancelled
end

I need to cycle through all the lines in the text file but I do not remember how to do it. Could someone help please?

Thank you in advance,

Val

While Not t.EndOfFile x = textInput.ReadLine Listbox1.AddRow(x) Next

If you get a syntax error…replace “Next” with “Wend”

While…

Wend

Hi, guys,

Thank you for your help. Its a good start.

For some reason, this line returns an error:


x = str(textInput.ReadLine)

The error message is “This item does not exist”

The entire code looks like the following


Dim t As TextInputStream
Dim f As FolderItem
Dim dlg As OpenDialog
Dim x as String

dlg = New OpenDialog

f = dlg.ShowModal() // open open dialog for selecting a file to load

if f<> nil then
  t = f.OpenAsTextFile // open selected text file
  
  While Not t.EndOfFile
    x = textInput.ReadLine
    Listbox1.AddRow(x)
  Wend
  
  t.Close
else
  //user cancelled
end
x = str(textInput.ReadLine)

STR converts a NUMERIC value into a String. READLINE is already a string, hence the error

just use

x = textInput.ReadLine

…and change “x = textInput.ReadLine”
to
x = t.ReadLine

You have defined a variable “t” as a “textInputStream” for this purpose… “textInput” does not exist

Thank you, everyone,

Changing the x=textInput.Readline to t.Readline fixed the final error.

In the language reference, I found the following code sample:


Var f As FolderItem
Var textInput As TextInputStream
Var rowFromFile, oneCell As String
Var i As Integer
f = FolderItem.ShowOpenFileDialog("text/plain") // defined as a FileType
If f <> Nil And f.Exists Then
  Var tab As String = ChrB(9)
  textInput = TextInputStream.Open(f)
  textInput.Encoding = Encodings.MacRoman // strings are MacRoman
  While Not textInput.EndOfFile
    rowFromFile = textInput.ReadLine
    
    // Set
    If ListBox1.ColumnCount < rowFromFile.CountFields(tab) Then
      ListBox1.ColumnCount = rowFromFile.CountFields(tab)
    End If
    
    ListBox1.AddRow("")
    For i = 1 To rowFromFile.CountFields(tab)
      oneCell = rowFromFile.NthField(tab, i)
      ListBox1.CellValueAt(ListBox1.LastAddedRowIndex, i - 1) = oneCell
    Next
  Wend
  textInput.Close
End If

That code does almost the same thing as the code that we all created. Below is the final version of our code, and it works as intended. Thank you everyone for your help.


Dim t As TextInputStream
Dim f As FolderItem
Dim dlg As OpenDialog
Dim x as String

dlg = New OpenDialog

f = dlg.ShowModal() // open open dialog for selecting a file to load

if f<> nil then
  t = f.OpenAsTextFile // open selected text file
  
  While Not t.EndOfFile
    x = t.ReadLine
    Listbox1.AddRow(x)
  Wend
  
  t.Close
else
  //user cancelled
end

One more question: how to make the first row selected automatically after the ListBox is populated with the data from the text file?

Listbox1.SelectedRowIndex = 0

Hi @Val Kozmenko

Just in case you want to try it, sometimes I found this to a be a bit faster:

[code]Var f As FolderItem = FolderItem.ShowOpenFileDialog("")

Try

Var t As TextInputStream = TextInputStream.Open(f)
Var a() As String = t.ReadAll.Split(EndOfLine)

For Each item As String In a
Listbox1.AddRow item
Next

Catch e As NilObjectException
MessageBox (“Nil FolderItem”)
End Try[/code]