How can i search a string in a listbox?[SOLVED] lazarus / 21-General ---------------------------------------------------------------------- Robert W.B. · 2022-07-15 18:01 Hi. I find the solution. Here it is: procedure TForm1.Button1Click(Sender: TObject); var   I: Integer; begin   for I := 0 to ListBox1.Items.Count - 1 do     ListBox1.Selected := False;   for I := 0 to ListBox1.Items.Count - 1 do      if ListBox1.Items.Strings.Contains(Edit1.Text) then         ListBox1.Selected := True; end;            Have a great day Old Bob ---------------------------------------------------------------------- alaa123456789 · 2022-07-15 18:24 Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i // From LazPlanet by Adnan Shameem// http://LazPlanet.blogspot.comunit frm1; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls; type   { TForm1 }   TForm1 = class(TForm)    Edit1: TEdit;    GroupBox1: TGroupBox;    GroupBox2: TGroupBox;    Label1: TLabel;    ListBox1: TListBox;    Memo1: TMemo;    Memo2: TMemo;    procedure Edit1Change(Sender: TObject);    procedure FormCreate(Sender: TObject);    procedure ListBox1Click(Sender: TObject);  private    procedure Split(Delimiter: Char; Str: string; ListOfStrings: TStrings);    { private declarations }  public    { public declarations }  end; var  Form1: TForm1; implementation {$R *.lfm} { TForm1 } procedure TForm1.FormCreate(Sender: TObject);var  myLine: String;  Lines, Parts: TStringList;begin   if FileExistsUTF8('Dictionary.txt') then begin     try          Parts := TStringList.Create;          Lines := TStringList.Create;          Lines.LoadFromFile('Dictionary.txt');        // in case the word lines are in bad order          Lines.Sort;           for myLine in Lines do begin              if Trim(myLine) <> '' then begin // we ignore empty lines                  Split(#9, myLine, Parts);                  ListBox1.Items.Add(Parts.Strings[0]);                  Memo2.Lines.Add(Parts.Strings[1]);              end;          end;     finally          if Parts <> nil then FreeAndNil(Parts);          if Lines <> nil then FreeAndNil(Lines);      end;   end else begin      ShowMessage('Dictionary file not found!');      Close;  end; end; procedure TForm1.Edit1Change(Sender: TObject);var  i: Integer;  query, searchitem: string;begin  // We would search case insensitive to make  // it easy for the user. That's why uppercase.  query := UpperCase(Edit1.Text);   for i := 0 to ListBox1.Count-1 do begin    // Same. Uppercase because we are searching    // Case insensitive.    searchitem := UpperCase(ListBox1.Items[i]);     if LeftStr(searchitem, Length(query)) = query then begin      ListBox1.ItemIndex := i;      ListBox1Click(Sender);      // We selected the first found item, so we don't need      // to search more... so we quit searching      Exit;    end;   end;end; procedure TForm1.ListBox1Click(Sender: TObject);begin  if ListBox1.ItemIndex >= 0 then    Memo1.Text := Memo2.Lines[ListBox1.ItemIndex];end; // http://stackoverflow.com/questions/2625707/split-a-string-into-an- array-of-strings-based-on-a-delimiterprocedure TForm1.Split(Delimiter: Char; Str: string; ListOfStrings: TStrings);begin   ListOfStrings.Clear;   ListOfStrings.Delimiter       := Delimiter;   ListOfStrings.StrictDelimiter := True;   ListOfStrings.DelimitedText := Str;end;  end. ---------------------------------------------------------------------- Nicole · 2022-07-15 18:32 Well it is German commented, but Google will translate for you. Here are 2 methods: One gives you the string back, the other one the line number. The first one was under the mouse and it is useful: it deletes one line. Mind the various "finds": one time case sensitive, the other time not. Just pass your StringList to the methode //******************************************************************** ********** // löscht eine Zeile aus einem StringList procedure TmyStringList.StringlistZeileLoeschen(SL: TStringList; nummer:integer); begin   SL.Delete(nummer); end; //******************************************************************** ********** // sucht findet die Zeile, die einem String entspricht function TmyStringList.StringlistSucheIn(SL: TStringList; gesucht:string):integer; var i: integer;     s: string;       //#13#10 and #$D#$A same and used to add new line to a string where the difference is that #13 is ASCII but #$D is Hexadecimal begin   result:=-1;   gesucht:=trim(gesucht);   if gesucht='' then exit;   for i := 0 to SL.Count-1 do     begin        s:=SL;  // das Problem ist das Datum, es wird gesucht mit 26.07.2020 und eingetragen ist 26.7.2020, also ohne Null        s:=trim(s);  // wenn es an den alten Dateien lag, so habe ich das jetzt manuell ausgebessert        if s=gesucht then begin  // die gesuchte Zeile wird zurückgegeben          result:=i;    // findet nur das erste Auftreten und nur ganze Zeile          break;        end;     end; end; //******************************************************************** ********** // sucht das übergebene String in einer Listbox und findet die Zeile function TmyStringList.ListBoxSucheIn(LB: TListBox; s:string; suche_ab:integer = -1):integer; var  i, gewaehlte_Zeile: integer;      zeile: string; begin   result:=-1;   s:=trim(s);   if s='' then begin     ShowMessage('Text eingeben, nach dem gesucht werden soll');     exit;   end;   gewaehlte_Zeile:=LB.ItemIndex; // das wird aus der aufrufenden Listbox erkannt: dort steht der Cursor   if (gewaehlte_Zeile = -1) then gewaehlte_Zeile:=0; // wenn keine Zeile gewählt ist   gewaehlte_Zeile:=gewaehlte_Zeile+1; // damit er nicht in der einmal gefundenen hängen bleibt   for i := gewaehlte_Zeile to LB.Count-1 do  // es wird immer ab     begin                                     // der markieren Zeile gesucht       //if Pos(s, LB.Items) > 0 then  => unterscheidet zwischen Groß- und Kleinschreibung!       // lt. Manual AnsiContainsStr NICHT case-sensitive, hier jedoch SCHON?!       s:=UpperCase(s);       zeile:=UpperCase(LB.Items);       if AnsiContainsStr(zeile,s) then          begin             result:=i;             exit;         end;     end;   for i := 0 to gewaehlte_Zeile do  // wurde nichts gefunden, beginnt er von oben     begin       s:=UpperCase(s);       zeile:=UpperCase(LB.Items);       if AnsiContainsStr(zeile,s) then          begin             result:=i;             exit;          end;     end; end; //******************************************************************** ********** ---------------------------------------------------------------------- PascalDragon · 2022-07-16 13:50 Quote from: Nicole on July 15, 2022, 08:32:22 pmWell it is German commented, but Google will translate for you. Here are 2 methods: One gives you the string back, the other one the line number. The first one was under the mouse and it is useful: it deletes one line. Mind the various "finds": one time case sensitive, the other time not. Please use the [ code ] tags (without spaces; preferrably as [ code = pascal ], again without spaces) as that will make the code better readable and also avoid the forum software interpreting your code (e.g. [ i ]) ---------------------------------------------------------------------- Nicole · 2022-07-18 13:59 Thank you for the hint! I searched for it, but have not found it before. ---------------------------------------------------------------------- Nicole · 2022-07-18 14:05 Hi Adnan, thank you for the posting. What does this line do exactly? Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i  Split(#9, myLine, Parts); Thanks. ---------------------------------------------------------------------- Thaddy · 2022-07-18 14:25 It splits myline with a tab (#9) as separator and put the result in parts, which is a dynamic array of string.