# Simple condition not working for listbox

**URL:** <https://forum.xojo.com/t/simple-condition-not-working-for-listbox/58001>\
**Category:** General\
**Created:** [October 16, 2020, 5:51pm UTC](https://forum.xojo.com/t/simple-condition-not-working-for-listbox/58001 "2020-10-16T17:51:26Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Niles\_Mitchell](https://forum.xojo.com/letter_avatar_proxy/v4/letter/n/e495f1/32.png) [@Niles\_Mitchell](https://forum.xojo.com/u/Niles_Mitchell)\
**Post date:** [October 16, 2020, 5:51pm UTC](https://forum.xojo.com/t/simple-condition-not-working-for-listbox/58001/1 "2020-10-16T17:51:26Z")

</div>

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
```

---

<div class="post-metadata">

**Author:** ![ChristopheDV](https://forum.xojo.com/letter_avatar_proxy/v4/letter/c/59ef9b/32.png) [@ChristopheDV](https://forum.xojo.com/u/ChristopheDV)\
**Post date:** [October 16, 2020, 6:00pm UTC](https://forum.xojo.com/t/simple-condition-not-working-for-listbox/58001/2 "2020-10-16T18:00:04Z")

</div>

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
```

---

<div class="post-metadata">

**Author:** ![Kem\_Tekinay](https://forum.xojo.com/user_avatar/forum.xojo.com/kem_tekinay/32/154_2.png) [@Kem\_Tekinay](https://forum.xojo.com/u/Kem_Tekinay)\
**Post date:** [October 16, 2020, 6:13pm UTC](https://forum.xojo.com/t/simple-condition-not-working-for-listbox/58001/3 "2020-10-16T18:13:57Z")

</div>

If the file is small enough, try this:

```auto
// ...

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
// ...

```

---

<div class="post-metadata">

**Author:** ![ChristopheDV](https://forum.xojo.com/letter_avatar_proxy/v4/letter/c/59ef9b/32.png) [@ChristopheDV](https://forum.xojo.com/u/ChristopheDV)\
**Post date:** [October 16, 2020, 6:16pm UTC](https://forum.xojo.com/t/simple-condition-not-working-for-listbox/58001/4 "2020-10-16T18:16:33Z")

</div>

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 )
```

---

<div class="post-metadata">

**Author:** ![Niles\_Mitchell](https://forum.xojo.com/letter_avatar_proxy/v4/letter/n/e495f1/32.png) [@Niles\_Mitchell](https://forum.xojo.com/u/Niles_Mitchell)\
**Post date:** [October 16, 2020, 6:20pm UTC](https://forum.xojo.com/t/simple-condition-not-working-for-listbox/58001/5 "2020-10-16T18:20:58Z")

</div>

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

I’m confused.

---

<div class="post-metadata">

**Author:** ![ChristopheDV](https://forum.xojo.com/letter_avatar_proxy/v4/letter/c/59ef9b/32.png) [@ChristopheDV](https://forum.xojo.com/u/ChristopheDV)\
**Post date:** [October 16, 2020, 6:23pm UTC](https://forum.xojo.com/t/simple-condition-not-working-for-listbox/58001/6 "2020-10-16T18:23:27Z")

</div>

> [@Kem\_Tekinay](#):
>
> `contents = contents.ReplaceLineEndings( &uA )`

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  
> // …

---

<div class="post-metadata">

**Author:** ![Kem\_Tekinay](https://forum.xojo.com/user_avatar/forum.xojo.com/kem_tekinay/32/154_2.png) [@Kem\_Tekinay](https://forum.xojo.com/u/Kem_Tekinay)\
**Post date:** [October 16, 2020, 6:24pm UTC](https://forum.xojo.com/t/simple-condition-not-working-for-listbox/58001/7 "2020-10-16T18:24:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ChristopheDV](https://forum.xojo.com/letter_avatar_proxy/v4/letter/c/59ef9b/32.png) [@ChristopheDV](https://forum.xojo.com/u/ChristopheDV)\
**Post date:** [October 16, 2020, 6:25pm UTC](https://forum.xojo.com/t/simple-condition-not-working-for-listbox/58001/8 "2020-10-16T18:25:06Z")

</div>

It does. I always do it this way. 🙂

---

<div class="post-metadata">

**Author:** ![Niles\_Mitchell](https://forum.xojo.com/letter_avatar_proxy/v4/letter/n/e495f1/32.png) [@Niles\_Mitchell](https://forum.xojo.com/u/Niles_Mitchell)\
**Post date:** [October 16, 2020, 6:28pm UTC](https://forum.xojo.com/t/simple-condition-not-working-for-listbox/58001/9 "2020-10-16T18:28:27Z")

</div>

> [@ChristopheDV](#):
>
> 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

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

---

<div class="post-metadata">

**Author:** ![ChristopheDV](https://forum.xojo.com/letter_avatar_proxy/v4/letter/c/59ef9b/32.png) [@ChristopheDV](https://forum.xojo.com/u/ChristopheDV)\
**Post date:** [October 16, 2020, 6:30pm UTC](https://forum.xojo.com/t/simple-condition-not-working-for-listbox/58001/10 "2020-10-16T18:30:11Z")

</div>

Which Xojo version are you using?

---

<div class="post-metadata">

**Author:** ![Niles\_Mitchell](https://forum.xojo.com/letter_avatar_proxy/v4/letter/n/e495f1/32.png) [@Niles\_Mitchell](https://forum.xojo.com/u/Niles_Mitchell)\
**Post date:** [October 16, 2020, 6:30pm UTC](https://forum.xojo.com/t/simple-condition-not-working-for-listbox/58001/11 "2020-10-16T18:30:23Z")

</div>

2020 r1

---

<div class="post-metadata">

**Author:** ![ChristopheDV](https://forum.xojo.com/letter_avatar_proxy/v4/letter/c/59ef9b/32.png) [@ChristopheDV](https://forum.xojo.com/u/ChristopheDV)\
**Post date:** [October 16, 2020, 6:31pm UTC](https://forum.xojo.com/t/simple-condition-not-working-for-listbox/58001/12 "2020-10-16T18:31:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Kem\_Tekinay](https://forum.xojo.com/user_avatar/forum.xojo.com/kem_tekinay/32/154_2.png) [@Kem\_Tekinay](https://forum.xojo.com/u/Kem_Tekinay)\
**Post date:** [October 16, 2020, 6:32pm UTC](https://forum.xojo.com/t/simple-condition-not-working-for-listbox/58001/13 "2020-10-16T18:32:25Z")

</div>

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

My test code on a Mac:

```auto
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._

---

<div class="post-metadata">

**Author:** ![ChristopheDV](https://forum.xojo.com/letter_avatar_proxy/v4/letter/c/59ef9b/32.png) [@ChristopheDV](https://forum.xojo.com/u/ChristopheDV)\
**Post date:** [October 16, 2020, 6:33pm UTC](https://forum.xojo.com/t/simple-condition-not-working-for-listbox/58001/14 "2020-10-16T18:33:24Z")

</div>

> [@Kem\_Tekinay](#):
>
> `s = "1" + EndOfLine.CRLF + "2" + EndOfLine.CRLF`

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

---

<div class="post-metadata">

**Author:** ![Niles\_Mitchell](https://forum.xojo.com/letter_avatar_proxy/v4/letter/n/e495f1/32.png) [@Niles\_Mitchell](https://forum.xojo.com/u/Niles_Mitchell)\
**Post date:** [October 16, 2020, 6:34pm UTC](https://forum.xojo.com/t/simple-condition-not-working-for-listbox/58001/15 "2020-10-16T18:34:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Kem\_Tekinay](https://forum.xojo.com/user_avatar/forum.xojo.com/kem_tekinay/32/154_2.png) [@Kem\_Tekinay](https://forum.xojo.com/u/Kem_Tekinay)\
**Post date:** [October 16, 2020, 6:35pm UTC](https://forum.xojo.com/t/simple-condition-not-working-for-listbox/58001/16 "2020-10-16T18:35:44Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![DerkJ](https://forum.xojo.com/letter_avatar_proxy/v4/letter/d/e495f1/32.png) [@DerkJ](https://forum.xojo.com/u/DerkJ)\
**Post date:** [October 16, 2020, 6:36pm UTC](https://forum.xojo.com/t/simple-condition-not-working-for-listbox/58001/17 "2020-10-16T18:36:46Z")

</div>

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](http://documentation.xojo.com/api/data_types/string.html#string-indexof)

---

<div class="post-metadata">

**Author:** ![ChristopheDV](https://forum.xojo.com/letter_avatar_proxy/v4/letter/c/59ef9b/32.png) [@ChristopheDV](https://forum.xojo.com/u/ChristopheDV)\
**Post date:** [October 16, 2020, 6:36pm UTC](https://forum.xojo.com/t/simple-condition-not-working-for-listbox/58001/18 "2020-10-16T18:36:49Z")

</div>

> [@Niles\_Mitchell](#):
>
> 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

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

---

<div class="post-metadata">

**Author:** ![ChristopheDV](https://forum.xojo.com/letter_avatar_proxy/v4/letter/c/59ef9b/32.png) [@ChristopheDV](https://forum.xojo.com/u/ChristopheDV)\
**Post date:** [October 16, 2020, 6:38pm UTC](https://forum.xojo.com/t/simple-condition-not-working-for-listbox/58001/19 "2020-10-16T18:38:10Z")

</div>

> [@Kem\_Tekinay](#):
>
> 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.

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

---

<div class="post-metadata">

**Author:** ![ChristopheDV](https://forum.xojo.com/letter_avatar_proxy/v4/letter/c/59ef9b/32.png) [@ChristopheDV](https://forum.xojo.com/u/ChristopheDV)\
**Post date:** [October 16, 2020, 6:39pm UTC](https://forum.xojo.com/t/simple-condition-not-working-for-listbox/58001/20 "2020-10-16T18:39:42Z")

</div>

> [@DerkJ](#):
>
> Note that xojo is case insensitive. You can use the additional parameters (mostly) to change that behavior.

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.

[Next page](https://forum.xojo.com/t/simple-condition-not-working-for-listbox/58001.md?page=2)
