Recent

Author Topic: [SOLVED] Checking items in a CheckListBox based on a StringList  (Read 292 times)

maurobio

  • Full Member
  • ***
  • Posts: 110
[SOLVED] Checking items in a CheckListBox based on a StringList
« on: December 05, 2019, 11:33:24 pm »
Dear ALL,

I have a StringList containing integer numbers (as strings, of course) and a CheckListBox on a form with a few items

What I want is to have the CheckListBox items checked according to the numbers in the StringList. So, if the StringList is ['1', '2', '5'], the first, second, and fifth items in the CheckListBox should be checked.

The test code I wrote (attached to this message) only works for the first two items; all the others appear unchecked.

Could someone help me to solve this?

Thanks in advance!

« Last Edit: December 06, 2019, 01:33:03 am by maurobio »
UCSD Pascal / Burroughs 6700 / Master Control Program
Lazarus 1.9.3/2.0.4 - FPC 3.0.4 on GNU/Linux Mint 19 ("Tessa"), Windows XP SP3, Windows 7 Professional, Windows 10 Home

wp

  • Hero Member
  • *****
  • Posts: 6664
Re: Checking items in a CheckListBox based on a StringList
« Reply #1 on: December 06, 2019, 12:31:37 am »
Try this (there are other possibilities)
Code: Pascal  [Select]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. var
  3.   I, J: integer;
  4. begin
  5.   L := TStringList.Create;
  6.   L.Add('1');
  7.   L.Add('2');
  8.   L.Add('5');
  9.   for I := 0 to 4 do
  10.     CheckListBox1.Items.Add('Item ' + IntToStr(I + 1));
  11.   for I := 0 to CheckListbox1.Count - 1 do
  12.     CheckListBox1.Checked[I] := L.IndexOf(IntToStr(I+1)) > -1;
  13. end;
Of course, you could also merge the two I loops into one.
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

maurobio

  • Full Member
  • ***
  • Posts: 110
Re: Checking items in a CheckListBox based on a StringList
« Reply #2 on: December 06, 2019, 01:32:28 am »
@wp,

That'it it! I had forgotten the very handy IndexOf method.

Thank you!

Cheers,
UCSD Pascal / Burroughs 6700 / Master Control Program
Lazarus 1.9.3/2.0.4 - FPC 3.0.4 on GNU/Linux Mint 19 ("Tessa"), Windows XP SP3, Windows 7 Professional, Windows 10 Home