Deselect/Select items programmatically in a ListBox lazarus / 31-LCL ---------------------------------------------------------------------- maurobio · 2025-04-09 11:55 Dear ALL, In a form I have a multi-select listbox and an edit control. I want to programmatically select an item in the listbox corresponding to the item number typed in the edit control, but I also want to select more than one item if a '/' is typed in the edit control. so for example if I type 1/2 the first two items in the list box should be selected. I have not been able to get the latter part done; is this not possible with multi-select listboxes? My test code is attached, could someone out there give me a hand? Many thanks in advance! With best regards, ---------------------------------------------------------------------- Zvoni · 2025-04-09 12:11 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 procedure TForm1.Edit1Change(Sender: TObject);Var  s:String;  a:Array Of String;  i:Integer;begin  If Edit1.Text<>'' Then    Begin      s:=Edit1.Text;      a:=s.Split('/');      ListBox1.ClearSelection;    For i:=Low(a) To High(a) Do If a[i]<>'' Then ListBox1.Selected[a[i].ToInteger-1]:=True;    end;end;               No defense whatsoever. No checks on only numeric entries and "/" Your Job ---------------------------------------------------------------------- maurobio · 2025-04-09 12:29 Hi, Zvoni! Thanks, but this produces the same results I obtained with my flawed test code. I want to have all items selected which correspond to the numbers typed in the listbox: so, for example, if I type 1/2, the first two items should be selected and so on. Currently, both my code and yours select only the last item whose number is typed in the edit control. With best regards, ---------------------------------------------------------------------- Zvoni · 2025-04-09 12:33 Quote from: maurobio on April 09, 2025, 02:29:01 pmHi, Zvoni! Thanks, but this produces the same results I obtained with my flawed test code. I want to have all items selected which correspond to the numbers typed in the listbox: so, for example, if I type 1/2, the first two items should be selected and so on. Currently, both my code and yours select only the last item whose number is typed in the edit control. With best regards, Err.... no? I tested it with 5 Items in my Testproject. Entering"2/3/5" exactly those items were selected, and any other items selected before were "deselected" And as i said: No defense whatsoever if User enters crap ---------------------------------------------------------------------- maurobio · 2025-04-09 12:43 Hi, Zvoni! Strange, really... but after compiling and running your test project, it worked fine! I will implement myself the defences against bad inputs (in fact part of them are already implemented in my test code). Thank you very much! With warmest regards, ---------------------------------------------------------------------- Zvoni · 2025-04-09 13:19 Quote from: maurobio on April 09, 2025, 02:43:33 pmHi, Zvoni! Strange, really... but after compiling and running your test project, it worked fine! I will implement myself the defences against bad inputs (in fact part of them are already implemented in my test code). Thank you very much! With warmest regards, Beware: My logic is not fully formulated. It works, if you only have 9 or less items in the Listbox. The Moment you have 10 or more my sample above doesn't work anymore. Think about it: How will the Edit-Box know, if you enter "1" (My Code executes and selects Item1) that there is still a "3" coming (because you want to select Item 13, in that case DE-Selecting Item1 and selecting Item13)? To fully and correctly process the entry,  you'll probably have to switch from OnChange to OnKeyDown or to OnKeyPress (or a combination of all 3) legal Characters: Digits and "/" in OnKeyDown/OnKeyPress you "wait" for the "/" being entered before jumping into my code (split, for-loop, if-then-else). And even that is not bullet-proof, since your Edit-Box will never know when you're done. There is still more thinking to do