robertjohns 0 Posted January 31 Is there any way to Filter TMS HTMListBox Items like TMS AdvListBox Share this post Link to post
Sherlock 663 Posted February 1 Isn't it up to you which items you put in the list? "Any way" could also imply to just filter yourself. Share this post Link to post
robertjohns 0 Posted February 1 May be my request is not properly explained Is there any way to Search/Filter TMS HTMListBox Items with EditBox like TMS AdvListBox Share this post Link to post
Stano 143 Posted February 1 In my opinion, the question belongs to TMS https://support.tmssoftware.com/categories Share this post Link to post
Lajos Juhász 295 Posted February 1 33 minutes ago, Stano said: In my opinion, the question belongs to TMS https://support.tmssoftware.com/categories It is a general algorithm. For search you loop to the items. While for search you have to maintain the data in order to insert into the components the values that match the criteria. Share this post Link to post
robertjohns 0 Posted February 1 Here I have tried the filter method with listbox and so far it works well procedure TForm1.FormCreate(Sender: TObject); Begin list_global := TStringList.Create; list_global.Assign(listBox1.Items); End; procedure TForm1.Edit1Change(Sender: TObject); Var list_filter: TStringList; i: Integer; Begin list_filter := TStringList.Create; list_filter.Assign(listBox1.Items); listBox1.Clear; for i := 0 to list_filter.Count - 1 do if pos(Edit1.text, list_filter[i]) > 0 then listBox1.Items.Add(list_filter[i]); End; How can I switch off the filter so that I can get back the items in listbox incase serach is not matched or clearing EditBox listBox1.Items := list_global; Share this post Link to post
Lajos Juhász 295 Posted February 1 procedure TForm1.Edit1Change(Sender: TObject); Var i: Integer; Begin listBox1.Clear; if trim(Edit1.text)='' then listbox1.items.Assign(list_global) else begin for i := 0 to list_global.Count - 1 do if pos(Edit1.text, list_global[i]) > 0 then listBox1.Items.Add(list_global[i]); end; End; Share this post Link to post
Sherlock 663 Posted February 1 Every single line of code you need is in your example...just remix it to do what you want. Edit: Or just take @Lajos Juhász code. But I strongly suggest trying to understand, what is being done there. Share this post Link to post
robertjohns 0 Posted February 1 1 hour ago, Lajos Juhász said: procedure TForm1.Edit1Change(Sender: TObject); Var i: Integer; Begin listBox1.Clear; if trim(Edit1.text)='' then listbox1.items.Assign(list_global) else begin for i := 0 to list_global.Count - 1 do if pos(Edit1.text, list_global[i]) > 0 then listBox1.Items.Add(list_global[i]); end; End; Thanks but There is still issue If we enter any unmatched letter in Editbox lisbox items gets cleared whereas if not matched listbox should not get clear Example ListBox Items Dog Rat Cat and If in EditBox I Enter z or x , listbox gets cleared , how to solve this Share this post Link to post
Stano 143 Posted February 1 You have two options: Test if the list is empty. If so, put the original list Create an auxiliary list. Work with it. If it is not empty, put it in listBox1 Share this post Link to post
JohnLM 22 Posted February 1 (edited) Quote and If in EditBox I Enter z or x , listbox gets cleared , how to solve this I played around with this myself and came up with the following that works for me: procedure TForm1.Edit1Change(Sender: TObject); Var i: Integer; Begin if trim(Edit1.text)='' then listbox1.items.Assign(list_global) else begin for i := 0 to list_global.Count - 1 do if pos(Edit1.text, list_global[i]) > 0 then begin listbox1.Clear; listBox1.Items.Add(list_global[i]); end; end; End; If any items in the listbox starting with the first char of those items, those item(s) will show in the listbox. Sometimes other items will show if having the same char. And, other times will not. In other words, its a little buggy. Edited February 1 by JohnLM Share this post Link to post
robertjohns 0 Posted February 1 1 hour ago, JohnLM said: I played around with this myself and came up with the following that works for me: procedure TForm1.Edit1Change(Sender: TObject); Var i: Integer; Begin if trim(Edit1.text)='' then listbox1.items.Assign(list_global) else begin for i := 0 to list_global.Count - 1 do if pos(Edit1.text, list_global[i]) > 0 then begin listbox1.Clear; listBox1.Items.Add(list_global[i]); end; end; End; If any items in the listbox starting with the first char of those items, those item(s) will show in the listbox. Sometimes other items will show if having the same char. And, other times will not. In other words, its a little buggy. How to Solve if any of the ListBox Items does not contain even first char of EditBox , ListBox items should not get clear , ListBox items must remain as it is because search criteria does not match any of the ListBox item. Sorry for my bad English Share this post Link to post
JohnLM 22 Posted February 1 As I mentioned in my previous response, the code snippet I provided is buggy and will not work for every case, and definitely not for a larger list of items, so I do not recommend it. As for a solution, I don't have the TMS component you mentioned, so I don't know what it can/cannot filter as you stated. For your request, it will require a more custom algorithm for that type of filter search to make it work the way you want. And for that, you will have to figure that part out or search google for one if one exists. Share this post Link to post
robertjohns 0 Posted February 1 1 minute ago, JohnLM said: As I mentioned in my previous response, the code snippet I provided is buggy and will not work for every case, and definitely not for a larger list of items, so I do not recommend it. As for a solution, I don't have the TMS component you mentioned, so I don't know what it can/cannot filter as you stated. For your request, it will require a more custom algorithm for that type of filter search to make it work the way you want. And for that, you will have to figure that part out or search google for one if one exists. I am not now talking about now TMS Component , I am talking about general ListBox Component Share this post Link to post
Lajos Juhász 295 Posted February 2 5 hours ago, robertjohns said: How to Solve if any of the ListBox Items does not contain even first char of EditBox , ListBox items should not get clear , ListBox items must remain as it is because search criteria does not match any of the ListBox item. procedure TForm1.Edit1Change(Sender: TObject); Var i: Integer; lFilteredList: TStringList; Begin lFilteredList:=TStringList.create; try if trim(Edit1.text)<>'' then begin for i := 0 to list_global.Count - 1 do if pos(Edit1.text, list_global[i]) > 0 then lFilteredList.Add(list_global[i]); end; if lFilteredList.count=0 then listbox1.items.assign(list_global) else listbox1.items.Assign(lFilteredList); finally lFilteredList.Free; end; End; Share this post Link to post
robertjohns 0 Posted February 2 (edited) 1 hour ago, Lajos Juhász said: procedure TForm1.Edit1Change(Sender: TObject); Var i: Integer; lFilteredList: TStringList; Begin lFilteredList:=TStringList.create; try if trim(Edit1.text)<>'' then begin for i := 0 to list_global.Count - 1 do if pos(Edit1.text, list_global[i]) > 0 then lFilteredList.Add(list_global[i]); end; if lFilteredList.count=0 then listbox1.items.assign(list_global) else listbox1.items.Assign(lFilteredList); finally lFilteredList.Free; end; End; Thanks for reply Edited February 2 by robertjohns Share this post Link to post