Get indexes of selected items in listbox lazarus / 21-General ---------------------------------------------------------------------- Hansvb · 2022-11-20 14:05 Hi, I have a list box with some values. aa bb ee cc bb aa With an EditBox you can search in the listbox and if a part of the search string occurs, the relevant line in the listbox is selected. So if I enter “b” in the textbox then 2 lines in the listbox are selected. I want to put the index of the two found lines in an array. See test below. I expect 1 and 4 but get 0,0 in the array. 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 function TForm1.ListBoxSearchItem(Listbox: TListBox; SearchText: String) : Integer;var  i, iArr, counter : Integer;  found : Bool;begin  Listbox.ClearSelection;  Listbox.MultiSelect:=True;  counter := 0;  Found := False;   for i := 0 to Listbox.Items.Capacity-1 do    begin    if Pos(SearchText, Listbox.Items.Strings[i]) > 0 then        begin          ListBox.Selected[I] := true;          SetLength(FFoundItems, counter+1);          Inc(Counter);           for iArr := 0 to Length(FFoundItems) -1 do            begin              if Listbox.ItemIndex = iArr then                begin                Found := true;                end;              end;        end;      if Found then        begin          FFoundItems[i] := Listbox.ItemIndex;        end;    end;  Result := Counter;end;     i also tried selected index and then the result 4,4 ? (the last selected listbox item) 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 function TForm1.ListBoxSearchItem(Listbox: TListBox; SearchText: String) : Integer;var  i, iArr, counter : Integer;  found : Bool;begin  Listbox.ClearSelection;  Listbox.MultiSelect:=True;  counter := 0;  for i := 0 to Listbox.Items.Capacity-1 do    begin      if Pos(SearchText, Listbox.Items.Strings[i]) > 0 then        begin        ListBox.Selected[I] := true;        end;    end;   for i := 0 to Listbox.Items.Count-1 do    begin      if Listbox.Selected[i] then      begin          Inc(counter);          SetLength(FFoundItems, counter);          FFoundItems[counter-1] := Listbox.ItemIndex;      end;    end;   Result := Counter;end; ---------------------------------------------------------------------- paweld · 2022-11-20 14:34 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 function TForm1.ListBoxSearchItem(Listbox: TListBox; SearchText: String): Integer;var  i, counter: Integer;begin  Listbox.ClearSelection;  SetLength(FFoundItems, 0);  Listbox.MultiSelect := True;  counter := 0;  for i := 0 to Listbox.Items.Count - 1 do  begin    if Pos(SearchText, Listbox.Items[i]) > 0 then    begin      ListBox.Selected[i] := True;      SetLength(FFoundItems, counter + 1);        FFoundItems[counter] := i;      Inc(Counter);    end;  end;  Result := Counter;end; ---------------------------------------------------------------------- jamie · 2022-11-20 14:35 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 Function ReturnFoundIndexes(Search:String;AListBox:TListBox):specialize TArray;Var  I:Integer;Begin Search := UpperCase(Search); SetLength(Result, 0); For I := 0 To AlistBox.Count-1 do   Begin     If UpperCase(AListBox.Items[I]) =Search Then      Begin        SetLength(result, Length(Result)+1);      Result[Length(result)-1] := I;        AListBox.Selected[I]:= True;      end;   end;end; procedure TForm1.Button1Click(Sender: TObject);begin  Caption := Length(ReturnFoundIndexes('bb',ListBox1)).Tostring;end;                   Something Like this? The returned array contains the indexes of the found locations too... You can change the search style, this was just to let you see how you can use the Length within a length without all the other crap. ---------------------------------------------------------------------- Hansvb · 2022-11-20 15:10 Thanks, both solutions are interesting. I'm going to check them out.