Listbox Check Words

Hello,

I have a question, I have a listbox with multiple cells, each with a different word (Ex: Monalisa, Blister, Cars, etc …), and a textfield where I write text and put in the same listbox, I wish there was one and check if the name you entered already exists in any cell of the listbox that was not duplicated there, would do this?

Tanks !

[quote=110383:@Paulo Vargas]Hello,

I have a question, I have a listbox with multiple cells, each with a different word (Ex: Monalisa, Blister, Cars, etc …), and a textfield where I write text and put in the same listbox, I wish there was one and check if the name you entered already exists in any cell of the listbox that was not duplicated there, would do this?
[/quote]

For xcoord = 0 to ListBox.ColumnCount-1 For YCoord = 0 to ListBox.ListCount-1 if ListBox.Cell(Ycoord, xcoord) = TextField.Text then Return True end if Return False

I am sure you thought of that.

Another way is to keep a string containing all cells content which you build when you populate the ListBox and add to when you add to the ListBox. Then all you got to do is :

Return (Instr(String,TextField.Text)>0)

It requires a bit more overhead, but should be much faster to search.

I just realized my second suggestion works only if you never delete anything from the ListBox. The alternative is to use an array that mirrors the structure of the listbox (insert, remove according to the ListBox modifications), and to use :

Return (Instr(Join(myarray, ","),TextField.Text)>0)

Or receive a tab delimited string of a column, row or the whole listbox by calling either
Listbox.cell(-1, columnnumber)
Listbox.cell(rownumber, -1)
Listbox.cell(-1, -1)

And than use instr() to check any instances of a particular search string.

[quote=110413:@Alex von Siebenthal]Or receive a tab delimited string of a column, row or the whole listbox by calling either
Listbox.cell(-1, columnnumber)
Listbox.cell(rownumber, -1)
Listbox.cell(-1, -1)

And than use instr() to check any instances of a particular search string.[/quote]

Brilliant. Thanks Alex. I consulted the LR for Cell but did not realize the example about copying from one LB to another could be used to copy to a string.

This is the best solution :slight_smile:

Personally I would do as Michel suggests and iterate through the cells checking each one. Might be worth checking the lower or upper case version of the strings though to ensure Car and car come out as a match.

If you do decide to create a string that contains all the cells, make sure you separate each word with something that will never be in the strings. For example if you have the words CAT and ANT together in a string without a separator, instr would find the word TAN in the string even though it’s not in the listbox. So be sure to separate with something that won’t ever be in a cell. E.g. CAT~ANT.

As I say though, your best bet is to check each cell in the listbox as Michel first mentioned.

[quote=110417:@Richard Vivash]Personally I would do as Michel suggests and iterate through the cells checking each one. Might be worth checking the lower or upper case version of the strings though to ensure Car and car come out as a match.

If you do decide to create a string that contains all the cells, make sure you separate each word with something that will never be in the strings. For example if you have the words CAT and ANT together in a string without a separator, instr would find the word TAN in the string even though it’s not in the listbox. So be sure to separate with something that won’t ever be in a cell. E.g. CAT~ANT.

As I say though, your best bet is to check each cell in the listbox as Michel first mentioned.[/quote]

Actually, Alex method does separate items with Tab characters. So it does prevent inopportune concatenations. And Instr is case sensitive, so car<>Car. His approach is indeed the most elegant one.

Michel Bujardet , Alex von Siebenthal , Richard Vivash Thank you very much for the tips and the support is very important to have people like you on the forum !

Not necessary… XOJO compares are by default NOT case sensitive

(“CAR”=“car”) is TRUE

You need to use STRCOMP function to do a case senstive compare

No it is not… msgbox str(instr(“CAR”,“car”)) returns 1… if it were senstive it would return 0

[quote=110460:@Dave S]And Instr is case sensitive, so car<>Car.
No it is not… msgbox str(instr(“CAR”,“car”)) returns 1… if it were senstive it would return 0[/quote]

My bad. I actually read it was case sensitive in the LR, that’s was I get for reading too fast :frowning:

InStr is case-insensitive, even with accented Roman characters and non-Roman alphabets. If you need to find the byte position of the find string within the source string or need a case-sensitive function, use the InStrB function.

So InstrB is should be. Thanks for spotting it, Dave.