Recent

Author Topic: Problems with ListFilterEdit  (Read 1015 times)

simsee

  • Full Member
  • ***
  • Posts: 184
Problems with ListFilterEdit
« on: January 25, 2020, 03:26:20 pm »
I apologize in advance for the trivial question. Surely I'm missing some stupid thing...

I have a ListFilterEdit1 with ListFilterEdit1.FilteredListBox=TListBox. If I add strings to TListBox.Items using the object inspector editor, the filter woks well. But if I add strings in code, the filter don't works, i.e. when I type some character in the ListFilterEdit1, the listbox is always empty, with or without matches to his items.

This is my code:

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls,
  9.   ListFilterEdit;
  10.  
  11. type
  12.  
  13.   { TForm1 }
  14.  
  15.   TForm1 = class(TForm)
  16.     ListBox1: TListBox;
  17.     ListFilterEdit1: TListFilterEdit;
  18.     procedure FormShow(Sender: TObject);
  19.   private
  20.  
  21.   public
  22.  
  23.   end;
  24.  
  25. var
  26.   Form1: TForm1;
  27.  
  28. implementation
  29.  
  30. {$R *.lfm}
  31.  
  32. { TForm1 }
  33.  
  34. procedure TForm1.FormShow(Sender: TObject);
  35. begin
  36.   ListBox1.Items.Clear;
  37.   ListBox1.Items.Add('aaa');
  38.   ListBox1.Items.Add('abb');
  39.   ListBox1.Items.Add('ccc');
  40. end;
  41.  
  42. end.

(I'm using Lazarus 2.0.6 on Windows 8.1)

gii

  • Jr. Member
  • **
  • Posts: 53
Re: Problems with ListFilterEdit
« Reply #1 on: January 25, 2020, 03:42:30 pm »
When you assign a ListBox to a ListFilterEdit, internally, the ListFilterEdit creates a copy of the ListBox  records.

If you modify the items in the ListBox, the internal list is not updated.

Code: Pascal  [Select][+][-]
  1. ListBox1.Items.Clear;
  2. ListBox1.Items.Add ( 'aaa');
  3. ListBox1.Items.Add ( 'abb');
  4. ListBox1.Items.Add ( 'ccc');
  5.  
  6. ListFilterEdit1.FilteredListbox := nil;
  7. ListFilterEdit1.FilteredListbox := ListBox1;
  8.  

simsee

  • Full Member
  • ***
  • Posts: 184
Re: Problems with ListFilterEdit
« Reply #2 on: January 25, 2020, 06:18:22 pm »
Thanks gii. You are right. Indeed the code in the setter method of SetFilteredListbox property confirms your explanation:

Code: Pascal  [Select][+][-]
  1. procedure TListFilterEdit.SetFilteredListbox(const AValue: TCustomListBox);
  2. begin
  3.   if fFilteredListbox = AValue then Exit;
  4.   fFilteredListbox:=AValue;
  5.   if Assigned(fFilteredListbox) then
  6.   begin
  7.     Filter:=Text;
  8.     fOriginalData.Assign(fFilteredListbox.Items);
  9.     if (fFilteredListbox is TCustomCheckListBox) and not Assigned(fCheckedItems) then
  10.       fCheckedItems:=TStringMap.Create(False);
  11.   end;
  12. end;

I presume that in your fixed code, the first assignment to nil forces the update of fOriginalData.
« Last Edit: January 25, 2020, 09:39:59 pm by simsee »

 

TinyPortal © 2005-2018