Recent

Author Topic: (Solved)I need help  (Read 491 times)

JLWest

  • Hero Member
  • *****
  • Posts: 934
(Solved)I need help
« on: May 21, 2020, 09:56:07 am »
if have a list box with an edit under neath it.
The items are sorted in the list box. and I'm trying to do an incremental search of the listbox when I type in the edit.

Can't seem to make it work. Has any one done this?

Thanks.
« Last Edit: October 23, 2020, 01:10:16 am by JLWest »
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

howardpc

  • Hero Member
  • *****
  • Posts: 3583
Re: I need help
« Reply #1 on: May 21, 2020, 10:38:41 am »
There are different approaches. This is not the fastest, but here's a quick and dirty one:
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}{$H+}
  2.  
  3. interface
  4.  
  5. uses
  6.   Forms, StdCtrls;
  7.  
  8. type
  9.   TForm1 = class(TForm)
  10.     edit: TEdit;
  11.     lb: TListBox;
  12.     procedure editChange(Sender: TObject);
  13.   private
  14.     procedure HighlightLBItem(const aText: String; aListBox: TListBox);
  15.   end;
  16.  
  17. var
  18.   Form1: TForm1;
  19.  
  20. implementation
  21.  
  22. procedure TForm1.editChange(Sender: TObject);
  23. var
  24.   ed: TEdit absolute Sender;
  25. begin
  26.   if not (Sender is TEdit) then
  27.     Exit;
  28.   HighlightLBItem(ed.Text, lb);
  29. end;
  30.  
  31. procedure TForm1.HighlightLBItem(const aText: String; aListBox: TListBox);
  32. var
  33.   find, possible: String;
  34.   i: Integer;
  35. begin
  36.   aListBox.ItemIndex := -1;
  37.   find := LowerCase(aText);
  38.   for i := 0 to aListBox.Items.Count-1 do
  39.     begin
  40.     possible := Lowercase(Copy(aListBox.Items[i], 1, Length(find)));
  41.     if Pos(find, possible) > 0 then
  42.       begin
  43.         aListBox.ItemIndex := i;
  44.         Exit;
  45.       end;
  46.     end;
  47. end;

JLWest

  • Hero Member
  • *****
  • Posts: 934
Re: I need help
« Reply #2 on: May 21, 2020, 06:57:37 pm »
@howardpc

That's works pretty good.
Thank You.
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

 

TinyPortal © 2005-2018