Recent

Author Topic: Deselect/Select items programmatically in a ListBox  (Read 384 times)

maurobio

  • Hero Member
  • *****
  • Posts: 640
  • Ecology is everything.
    • GitHub
Deselect/Select items programmatically in a ListBox
« on: April 09, 2025, 01:55:11 pm »
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,
UCSD Pascal / Burroughs 6700 / Master Control Program
Delphi 7.0 Personal Edition
Lazarus 3.8 - FPC 3.2.2 on GNU/Linux Mint 19.1/20.3, Windows XP SP3, Windows 7 Professional, Windows 10 Home

Zvoni

  • Hero Member
  • *****
  • Posts: 2963
Re: Deselect/Select items programmatically in a ListBox
« Reply #1 on: April 09, 2025, 02:11:57 pm »
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Edit1Change(Sender: TObject);
  2. Var
  3.   s:String;
  4.   a:Array Of String;
  5.   i:Integer;
  6. begin
  7.   If Edit1.Text<>'' Then
  8.     Begin
  9.       s:=Edit1.Text;
  10.       a:=s.Split('/');
  11.       ListBox1.ClearSelection;
  12.       For i:=Low(a) To High(a) Do If a[i]<>'' Then ListBox1.Selected[a[i].ToInteger-1]:=True;
  13.     end;
  14. end;                  

No defense whatsoever. No checks on only numeric entries and "/"
Your Job
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

maurobio

  • Hero Member
  • *****
  • Posts: 640
  • Ecology is everything.
    • GitHub
Re: Deselect/Select items programmatically in a ListBox
« Reply #2 on: April 09, 2025, 02:29:01 pm »
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,
UCSD Pascal / Burroughs 6700 / Master Control Program
Delphi 7.0 Personal Edition
Lazarus 3.8 - FPC 3.2.2 on GNU/Linux Mint 19.1/20.3, Windows XP SP3, Windows 7 Professional, Windows 10 Home

Zvoni

  • Hero Member
  • *****
  • Posts: 2963
Re: Deselect/Select items programmatically in a ListBox
« Reply #3 on: April 09, 2025, 02:33:57 pm »
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,
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
« Last Edit: April 09, 2025, 02:37:23 pm by Zvoni »
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

maurobio

  • Hero Member
  • *****
  • Posts: 640
  • Ecology is everything.
    • GitHub
Re: Deselect/Select items programmatically in a ListBox
« Reply #4 on: April 09, 2025, 02:43:33 pm »
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,
UCSD Pascal / Burroughs 6700 / Master Control Program
Delphi 7.0 Personal Edition
Lazarus 3.8 - FPC 3.2.2 on GNU/Linux Mint 19.1/20.3, Windows XP SP3, Windows 7 Professional, Windows 10 Home

Zvoni

  • Hero Member
  • *****
  • Posts: 2963
Re: Deselect/Select items programmatically in a ListBox
« Reply #5 on: April 09, 2025, 03:19:26 pm »
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,
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
« Last Edit: April 09, 2025, 03:25:31 pm by Zvoni »
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

 

TinyPortal © 2005-2018