Jump to content
robertjohns

Filtering ListBox Items

Recommended Posts

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

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

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

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

You have two options:

  1. Test if the list is empty. If so, put the original list
  2. Create an auxiliary list. Work with it. If it is not empty, put it in listBox1

Share this post


Link to post
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 by JohnLM

Share this post


Link to post
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

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
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
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
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 by robertjohns

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×