Recent

Author Topic: Combo box with search  (Read 7381 times)

Packs

  • Sr. Member
  • ****
  • Posts: 496
Combo box with search
« on: October 27, 2023, 08:59:37 pm »
I need combo box with search or filter functionality.

It should filter character from anywhere from string.


KodeZwerg

  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Combo box with search
« Reply #1 on: October 27, 2023, 09:50:54 pm »
Cool but too less information.
So what have you tried on your own?
Where is the problem?
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

bobby100

  • Sr. Member
  • ****
  • Posts: 301
    • Malzilla
Re: Combo box with search
« Reply #2 on: October 27, 2023, 10:38:08 pm »
Create two TStringLists, one with all the items, the other filled with just the items matching the filter, fill the combobox just with the items from the 2nd list.

jamie

  • Hero Member
  • *****
  • Posts: 7776
Re: Combo box with search
« Reply #3 on: October 28, 2023, 01:04:55 am »
Depending on where you need to go with this, there is a "TFilerCombobox" in the Misc tab.

It uses a TshellListview as an auxiliary component.
The only true wisdom is knowing you know nothing

Packs

  • Sr. Member
  • ****
  • Posts: 496
Re: Combo box with search
« Reply #4 on: October 31, 2023, 04:35:07 pm »
It is searching or filtering from first character.

I want it filter or search word any sentance.


Example : I like Lazarus
I like Lazarus programming
Best programming language is Lazarus
2023 is best language is golang
Delphi language

If I search language. It should show 3,4,5 as a output

How to implement this logic in combo box

Handoko

  • Hero Member
  • *****
  • Posts: 5551
  • My goal: build my own game engine using Lazarus
Re: Combo box with search
« Reply #5 on: October 31, 2023, 05:41:53 pm »
bobby100 already gave you the secret.
Haven't you tried to do what he said?

I did it in the past, very similar to what he said. So I can say it will work for sure. I believe you know how to do string comparison, so what you need to do is comparing the user input with the 1st stringlist. If the condition is satisfied then copy that item in the 1st stringlist to the second stringlist (or combobox). Just that simple.

But for my use case, I need it to be able to handle huge amount of items. String comparing and copying are expensive tasks. So I had to optimized the code properly. Mine is capable to handle up to 100 thousands items without performance issue. It only starts to feel sluggish if the items reach about 300k items.

Mine is a part of a closed source project, it has many cool features, I shouldn't share it here. Try to do it yourself first. If you have problem later, post the issue here, many users will help you. If we give you the ready-to-use code, you will learn nothing. But if you're lazy or need it a hurry, post a bounty in the jobs section.  But hey, I ever posted a simplified version of mine years ago.

Packs

  • Sr. Member
  • ****
  • Posts: 496
Re: Combo box with search
« Reply #6 on: October 31, 2023, 08:35:02 pm »
Thanks. I got your idea.

How I can implement in combo box.

Should I write custom component.

Or any third-party library is available.

Joanna

  • Hero Member
  • *****
  • Posts: 1461
Re: Combo box with search
« Reply #7 on: October 31, 2023, 10:43:57 pm »
Is the string Example : I like Lazarus
One of the combobox strings ? If so you might need to use substrings for each word in the combobox item when comparing it to other things.
So parsing each word between spaces and seeing if it is a substring of what you are comparing it to..perhaps.

In any case this is not standard combobox behavior, you would need to make a tcombobox descendant.
« Last Edit: October 31, 2023, 10:47:21 pm by Joanna »

Handoko

  • Hero Member
  • *****
  • Posts: 5551
  • My goal: build my own game engine using Lazarus
Re: Combo box with search
« Reply #8 on: November 01, 2023, 04:12:03 am »
Basically there are 2 ways to create a new component:
- Sub-classing a ready made component (TComboBox)
- Combining several components

In this case, I think it will be easier to not do sub-classing TComboBox. Try to think creatively. You can get what you want by using a TEdit and a TList.

You can use TEdit.OnKeyPress or TEdit.OnEditingDown events to detect user's inputs. I personally prefer OnKeyPress for instant search result. And then use a TList is for showing the search result.

Isn't that easy? But the next issue would be, how to make it to popup.
Try this:
https://forum.lazarus.freepascal.org/index.php/topic,37369.msg250939.html#msg250939

The custom popup in the link above is not the final product you want. But if you add a TEdit and some searching features, that would be very close you what you want.

Writing a new component may sound too challenging for some programmers. But, it's fun. Once, you can do it, you would want to challenge yourself to create more complex components.

paweld

  • Hero Member
  • *****
  • Posts: 1645
Re: Combo box with search
« Reply #9 on: November 01, 2023, 05:53:24 am »
attached is my example from a few years ago. you would definitely need to add case-sensitivity skipping.
Best regards / Pozdrawiam
paweld

Packs

  • Sr. Member
  • ****
  • Posts: 496
Re: Combo box with search
« Reply #10 on: November 01, 2023, 06:34:19 pm »
Nice idea. It is working.

There is one issue it is flickering.i have 20k records

dbannon

  • Hero Member
  • *****
  • Posts: 3826
    • tomboy-ng, a rewrite of the classic Tomboy
Re: Combo box with search
« Reply #11 on: November 03, 2023, 10:34:12 am »
There is one issue it is flickering.i have 20k records

Hmm, Prakash, many components have a .lockupdates method that stops the component updating while you update its data. But TComboBox does not seem to have this, not sure why not. Maybe someone else has better knowledge ?  Anyway, that is probably your flickering problem.

Maybe another solution is to write your desired content to a separate, standalone TStrings thingo and, when finished, assign it to the ComboBox.Items. That assignment should be a lot faster. Not sure, some experimenting would seem in order.....

Davo
Lazarus 4, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

Bart

  • Hero Member
  • *****
  • Posts: 5731
    • Bart en Mariska's Webstek
Re: Combo box with search
« Reply #12 on: November 03, 2023, 10:43:19 am »
(From the top of my head) Maybe: ComboBox.Items.BeginUpdate/EndUpdate?

Bart

dbannon

  • Hero Member
  • *****
  • Posts: 3826
    • tomboy-ng, a rewrite of the classic Tomboy
Re: Combo box with search
« Reply #13 on: November 04, 2023, 12:14:34 am »
Yes, Bart, of course it is, my brain was not working yesterday !  Sorry.

Prakash, you will do something like this -

Code: Pascal  [Select][+][-]
  1. YourComboBox.Beginupdate;
  2. // do all the updating to the combo, perhaps clear it
  3. // and then write the output of your filter to its .items property.
  4. YourComboBox.Endupdate;
Make sure you include that 'endupdate' EVERY time, try..finally probably best approach. Very, very important.

Sigh ....
(been woking with something that used .lockupdate and I could not get that syntax out of my head.)
Davo
Lazarus 4, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

 

TinyPortal © 2005-2018