Recent

Author Topic: TListBox Search For More Than One Variable  (Read 2058 times)

Paloski

  • Newbie
  • Posts: 4
TListBox Search For More Than One Variable
« on: July 09, 2017, 10:25:40 pm »
Hi I have created a search application. It works well searching for one variable however, I would like to search for more then one variable in the TEdit box.
How can I do this? perhaps there is a search package that will do this for me?

Eample:

I have added a Edit1, ListBox1, Listbox2 and Button1 to my Form1.
I search for "Firstline" and it returns, I search for "1random" and it returns.
However when I search for "Firstline, 1random" I get nothing.
How can I make this possible? I was thinking of using comma delimited somehow but if there is an easier way i'll be happy.

Any suggestions or improvements will be greatly appreciated.

Windows 10 x64
Lazarus IDE v1.6.4

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Button1: TButton;
  16.     Edit1: TEdit;
  17.     Label1: TLabel;
  18.     Label2: TLabel;
  19.     Label3: TLabel;
  20.     ListBox1: TListBox;
  21.     ListBox2: TListBox;
  22.     procedure Button1Click(Sender: TObject);
  23.     procedure Edit1Change(Sender: TObject);
  24.     procedure Edit1KeyPress(Sender: TObject; var Key: char);
  25.     procedure FormCreate(Sender: TObject);
  26.     procedure ListBox1Click(Sender: TObject);
  27.     procedure ListBox2Click(Sender: TObject);
  28.   private
  29.     { private declarations }
  30.   public
  31.     { public declarations }
  32.   end;
  33.  
  34. var
  35.   Form1: TForm1;
  36.   i: Integer;
  37.  
  38. implementation
  39.  
  40. {$R *.lfm}
  41.  
  42. { TForm1 }
  43.  
  44. procedure TForm1.Button1Click(Sender: TObject);
  45. begin
  46.   ListBox2.Clear;
  47.   for i:= 0 to ListBox1.Items.Count-1 do begin
  48.     if UpperCase(ListBox1.Items[i]) = UpperCase(Edit1.Text) then
  49.       ListBox2.Items.Add(ListBox1.Items[i]);
  50.   end;
  51. end;
  52.  
  53. procedure TForm1.Edit1Change(Sender: TObject);
  54. begin
  55.  
  56. end;
  57.  
  58. procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: char);
  59. begin
  60.   if (Key = chr(10)) or (Key = chr(13)) then begin
  61.     Button1Click(Sender);
  62.   end;
  63. end;
  64.  
  65. procedure TForm1.FormCreate(Sender: TObject);
  66. begin
  67.   ListBox1.Items.Clear;
  68.   ListBox1.Items.Add('Firstline');
  69.   ListBox1.Items.Add('1random');
  70.   ListBox1.Items.Add('Third');
  71.   ListBox1.Items.Add('2random');
  72. end;
  73.  
  74. procedure TForm1.ListBox1Click(Sender: TObject);
  75. begin
  76.  
  77. end;
  78.  
  79. procedure TForm1.ListBox2Click(Sender: TObject);
  80. begin
  81.  
  82. end;
  83.  
  84. end.
  85.      

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: TListBox Search For More Than One Variable
« Reply #1 on: July 09, 2017, 11:06:03 pm »
You could change your ButtonClick procedure to this sort of thing:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   i, j: Integer;
  4.   sArray: array of string;
  5.   upper: string;
  6.  
  7.   procedure AddToArray(const aText: string);
  8.   begin
  9.     SetLength(sArray, Length(sArray)+1);
  10.     sArray[High(sArray)]:=aText;
  11.   end;
  12.  
  13.   procedure EditTextToArray;
  14.   var
  15.     s: string;
  16.     p: Integer;
  17.   begin
  18.     s:=Edit1.Text;
  19.     if (Length(s) > 0) then
  20.     begin
  21.       SetLength(sArray, 0);
  22.       repeat
  23.         p:=Pos(',', s);
  24.         case p of
  25.           0: AddToArray(UpperCase(Trim(s)));
  26.           else begin
  27.             AddToArray(UpperCase(Trim(Copy(s, 1, p-1))));
  28.             Delete(s, 1, p);
  29.           end;
  30.         end;
  31.       until (p = 0);
  32.     end;
  33.   end;
  34.  
  35. begin
  36.   ListBox2.Clear;
  37.   EditTextToArray;
  38.   for i:=0 to ListBox1.Items.Count-1 do begin
  39.     upper:=UpperCase(ListBox1.Items[i]);
  40.     for j:=0 to High(sArray) do
  41.       if (upper = sArray[j]) then
  42.         ListBox2.Items.Add(ListBox1.Items[i]);
  43.   end;
  44. end;

Best to remove 'i' as a global variable, and make it local to the procedure where it is used.
There are strutils routines that can help with this sort of parsing, and the SameText() function avoids putting Uppercase() all over the place.

Paloski

  • Newbie
  • Posts: 4
Re: TListBox Search For More Than One Variable
« Reply #2 on: July 10, 2017, 01:58:13 pm »
Thanks for the input!

This is very useful, hopefully it will help someone else as well.

 

TinyPortal © 2005-2018