Simple condition not working for listbox

Hey everyone!

I’m reading the contents of a text file and putting each line into a listbox. I’m trying to make it not put the text “WORKSTATION” into the listbox if that text is detected. I’ve tried a few variations of the following code, but the results are always the same. It finds “WORKSTATION” in the file, but still puts it into the list box. What am I doing wrong? Everything else is working fine, it just doesn’t skip the “WORKSTATION” text when it finds it.

 Dim vendors As FolderItem = SpecialFolder.Temporary.child("WK_Vend.txt")
 Dim t as TextInputStream

 dim A as integer = 0
 while A = 0
 if vendors.Exists then
 t = TextInputStream.Open(vendors)
 t.Encoding = Encodings.UTF16
dim x as string
While Not t.EndOfFile
  x = t.ReadLine
  if x <> "WORKSTATION" then
    VENDORS_LISTBOX.addrow(x)
  end
Wend

t.Close
vendors.Delete
A = A + 1
end
wend

First I would read the entire file into an array.
Then you can walk thru the array and use instr to see if the ‘workstation’ is found.

BTW the reason why it doesn’t work is that x probably has een endofline - so just checking =“Workstation” will not work.

if instr(x,"Workstation") = 0 then
    VENDORS_LISTBOX.addrow(x)
end

If the file is small enough, try this:

// ...

var contents as string = t.ReadAll
t.Close

contents = contents.ConvertEncoding( Encodings.UTF8 )
contents = contents.ReplaceLineEndings( &uA )
var lines()  as string = contents.Split( &uA )

for each line as string in lines
  if line.IndexOf( "WORKSTATION" ) = -1 then
    VENDORS_LISTBOX.AddRow( line )
  end
next line
// ...
1 Like

Yeah, Kem is right … you should use the new API2 IndexOf instead of Instr. Both will work fine though.

But I think one line is not needed.

contents = contents.ConvertEncoding( Encodings.UTF8 )
var lines()  as string = contents.Split( endofline )

You said one line is not needed, but you changed the line, not removed it.

I’m confused.

This line is not needed:
contents = contents.ReplaceLineEndings( &uA )

Basically Kems code could be:

// …

var contents as string = t.ReadAll
t.Close

contents = contents.ConvertEncoding( Encodings.UTF8 )
var lines() as string = contents.Split( endofline )

for each line as string in lines
if line.IndexOf( “WORKSTATION” ) = -1 then
VENDORS_LISTBOX.AddRow( line )
end
next line
// …

Will reading a text file convert the line endings for you? I’ve never tried.

It does. I always do it this way. :slight_smile:

1 Like

Doesn’t work.

I get

VENDOR_LOOKUP.PushButton1.Action, line 67
This item does not exist
if line.IndexOf( “WORKSTATION” ) = -1 then

and

VENDOR_LOOKUP.PushButton1.Action, line 67
There is more than one method with this name but this does not match any of the available signatures.
if line.IndexOf( “WORKSTATION” ) = -1 then

Which Xojo version are you using?

2020 r1

Can you show your complete code in the button action event?

I just did a quick test, and it did not.

My test code on a Mac:

var s as string
s = "1" + EndOfLine.CRLF + "2" + EndOfLine.CRLF

var t as FolderItem = FolderItem.TemporaryFile
var b as BinaryStream = BinaryStream.Open( t, true )
b.Write s
b.Close
b = nil

var tis as TextInputStream = TextInputStream.Open( t )
tis.Encoding = Encodings.UTF8
var contents as string = tis.ReadAll
tis.Close

t.Remove

return

Edit: I set a breakpoint at the return to check the contents through the debugger.

s = "1" + EndOfLine + "2" + EndOfLine ?

Dim vendors As FolderItem = SpecialFolder.Temporary.child(“WK_Vend.txt”)

Dim t as TextInputStream
t = TextInputStream.Open(vendors)
dim contents as string = t.ReadAll
t.Close

contents = contents.ConvertEncoding( Encodings.UTF8 )
dim lines() as string = contents.Split( endofline )

for each line as string in lines
if line.IndexOf( “WORKSTATION” ) = -1 then
VENDORS_LISTBOX.AddRow( line )
end
next line

The point of the test is to see if a file created with different line endings would automatically convert to the ones used by the current platform. EndOfLine always returns the current platform’s line endings.

Note that xojo is case insensitive. You can use the additional parameters (mostly) to change that behavior.

http://documentation.xojo.com/api/data_types/string.html#string-indexof

1 Like

Works fine here. I think the quotes " are not correct. Make sure they are.

I see what you mean. Maybe replacing the endofline is a good idea after all. :slight_smile:
Thanks for the hint.

That’s a plus for the new IndexOf. That may be why it is slower than Instr though. But in most cases that’s no issue.