ComboBox that reads a text file

Hi, guys,

After some pain and suffering, I created a code that reads a text file and passes its content into a ComboBox as its InitialValue.

Here is the code


Dim t As TextInputStream
Dim f As FolderItem
Dim dlg As OpenDialog
Var s As String
Var last As Integer
Var stream As BinaryStream



dlg = New OpenDialog

f = dlg.ShowModal() // open open dialog for selecting a file to load
If f <> Nil Then
  stream = BinaryStream.Open(f, False)
  Do
    Window1.TextArea2.AddText(stream.Read(1000, Encodings.UTF8))
  Loop Until stream.EndOfFile
  stream.Close
End If

s=Window1.TextArea2.Text



last = CountFields(Window1.TextArea2.Text, ",")

for i As Integer = 1 to last
  Window1.ComboBox1.AddRow(NthField(s, "," , i))
next

Return True

I need just a minor modification. I want the text file that supplies the content to have fields separated not by the comas but by the end of line.

So, I do not want the text to be a one string with coma separated entries but rather a list where each line is a new entry for the ComboBox.

Can somebody help please?

Thank you,

Val

docs.xojo.com/String.Split
docs.xojo.com/Replace
i used this to read a file row by row in to a string array Messages
xojo 2019r3

  Var f As FolderItem = SpecialFolder.Documents.Child("List.txt",False)
  Messages.RemoveAllRows
  If f.Exists Then
    Var t As TextInputStream = TextInputStream.Open(f)
    While Not t.EndOfFile
      Var m As String = t.ReadLine(Encodings.UTF8)
      If m.Length>0 Then Messages.AddRow m
    Wend
    t.Close
  Else
    Messages.AddRow "File Not Found"
  End If

Hi, Markus,

Thank you for your reply.

For some reason, the following line is causing the problem


t = TextInputStream.Open(f)

Any suggestions?

http://docs.xojo.com/TextInputStream

In the example, read the Comment and read the line just before yours in the example (the one with try)

Edit: Relevant code added:

If f <> Nil Then If f.Exists Then // Be aware that TextInputStream.Open could raise an exception Var t As TextInputStream Try t = TextInputStream.Open(f) t.Encoding = Encodings.UTF8

Here is the final code. Maybe it is useful for someone.


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

ComboBox1.DeleteAllRows


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