[Gambas-user] Filtering listbox list

d4t4full at gmail.com d4t4full at gmail.com
Sun Mar 11 22:38:49 CET 2018


Shannon,

Did not actuallt try it but I guess that you could change the line:

If ListBox1[i].Text Like TextBox4.Text & "*" Then ' found another

To this to allow looking for letters/words not at the beginning:

If (ListBox1[i].Text Like ("*" & TextBox4.Text & "*")) Then ' found another

That is, prefix the search term with another asterisk. Parentheses are optional, of course, but they do clarify my own mind.

Risking being pedantic, I'd also suggest you use meaningful control and variable names. ListBox1 says nothing but lstAllOptions does. Same for TextBox4, it could be txtSearch, and ListBox6 could be lstFiltered. You'll thank me later when you revisit code some time from now. Been there, done that, regretted it big time.

Hope it helps,
zxMarce.

On Mar 11, 2018, 18:04, at 18:04, Shannon Kuchler <shannon at inflecto.org> wrote:
>
>
>This code works
>Public Sub TextBox4_KeyRelease()Dim i As Integer
>Dim l As Integer
>i = (-1)
>
>ListBox6.Clear
>
>If TextBox4.Text = "" Then ListBox6.List = ListBox1.List
> If TextBox4.Text <= " " Then Return ' not searchable
> For l = 0 To ListBox1.List.Max
> Inc i
> If ListBox1[i].Text Like TextBox4.Text & "*" Then ' found another
> ListBox6.Add(ListBox1[i].Text) 
> Endif
> Next
>
>end
>I could not get it to work with out the "Inc i" perhaps I'm missing
>something there. Before it wasn't catching listbox1.index(0)  so I had
>to add "i = (-1)"However it still need something because it only 1/2
>works
>
>for instance this is the data in listbox1
>
>The Dying Lands
>Bad Company
>Valmod Expansion Pack
>Valmod Overhaul Pack
>Darkness Falls (BBP)
>Starvation
>War Of The Walkers 5.2 (BBP)
>if I type "v" in textbox4 I get this output to listbox6 which I
>expected 
>
>Valmod Expansion Pack
>Valmod Overhaul Pack
>However if I type "t" in textbox4 I get this output to listbox6 
>
>The Dying Lands
>I expected 't" to give me everything that had a 't' like this
>
>The Dying LandsStarvationWar Of The Walkers 5.2 (BBP)
> 
>
>
>
>
>
>Good advice on using ListBox.List.Max.
>
>I have found the KeyRelease event to be more reliable in regards to
>having access to the actual, current keystroke value.
>
>And, instead of looping through List, I personally would simply use
>ListBox.Find.
>
>
>-- 
>Lee
>
>On 03/11/2018 09:37 AM, Gianluigi wrote:
>> Maybe you could use the KeyPress event with "If Key.Code = Key.Return
>Then ...",
>> "Inc i" is useless and you should use ListBox1.List.Max (or
>ListBox1.count -1) instead of ListBox1.Count to avoid the 'out of
>> bound' error.
>> 
>> Regards
>> Gianluigi
>> 
>> 2018-03-11 10:33 GMT+01:00 Shannon Kuchler <shannon at inflecto.org
><mailto:shannon at inflecto.org>>:
>> 
>>     __
>> 
>>     
>> 
>>         -I think I figured it out
>> 
>> 
>>         Public Sub TextBox4_KeyRelease()
>>         Dim i As Integer
>>         Dim l As Integer
>> 
>>         ListBox6.Clear
>>               
>> 
>>               If TextBox4.text > "z" Or TextBox4.text < " " Then
>Return
>>               For l = 0 To ListBox1.Count
>>                 Inc i
>>                   If ListBox1[i].Text Like TextBox4.Text & "*" Then '
>found another
>>                   ListBox6.Add(ListBox1[i].Text)
>>               Endif
>>               Next
>> 
>>         End
>> 
>> 
>> 
>
>--------------------------------------------------
>
>This is the Gambas Mailing List:
>https://lists.gambas-basic.org/listinfo/user
>
>Search the list:
>https://lists.gambas-basic.org/cgi-bin/search.cgi
>
>Hosted by https://www.hostsharing.net
>
>
>
>------------------------------------------------------------------------
>
>
>--------------------------------------------------
>
>This is the Gambas Mailing List:
>https://lists.gambas-basic.org/listinfo/user
>
>Search the list:
>https://lists.gambas-basic.org/cgi-bin/search.cgi
>
>Hosted by https://www.hostsharing.net
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.gambas-basic.org/pipermail/user/attachments/20180311/38d60db5/attachment.html>


More information about the User mailing list