### Bookstore

 Computer Math and Games in Pascal (preview) Lazarus Handbook

### Author Topic: How to find the number of items checked in a check group?  (Read 439 times)

#### Doe

• New Member
• Posts: 17
##### How to find the number of items checked in a check group?
« on: March 28, 2021, 11:17:33 pm »
Hello, guys. I was trying to get the number of items checked within a check box and assign it to variable j. But it seems the value of j is 0 no matter how many items are checked. My code is below:
Code: Pascal  [Select][+][-]
1. unit Unit1;
2.
3. {\$mode objfpc}{\$H+}
4.
5. interface
6.
7. uses
8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, StdCtrls;
9.
10. type
11.
12.   { TForm1 }
13.
14.   TForm1 = class(TForm)
15.     Button1: TButton;
16.     CheckBox1: TCheckBox;
17.     CheckBox2: TCheckBox;
18.     CheckBox3: TCheckBox;
19.     CheckBox4: TCheckBox;
20.     CheckBox5: TCheckBox;
21.     CheckGroup1: TCheckGroup;
22.     procedure Button1Click(Sender: TObject);
23.   private
24.
25.   public
26.
27.   end;
28.
29. var
30.   Form1: TForm1;
31.   i,j:integer;
32.
33. implementation
34.
35. {\$R *.lfm}
36.
37. { TForm1 }
38.
39. procedure TForm1.Button1Click(Sender: TObject);
40. begin
41.     j:=0;
42.   for i:=0 to CheckGroup1.Items.Count-1 do
43.     begin
44.     if CheckGroup1.Checked[i] then
45.       begin
46.       j:=j+1;
47.       end;
48.     end;
49.     showmessage(FloatToStr(j));
50.     end;
51.
52. end.
53.
54.

#### engkin

• Hero Member
• Posts: 2721
##### Re: How to find the number of items checked in a check group?
« Reply #1 on: March 28, 2021, 11:52:45 pm »
Maybe because you have zero for CheckGroup1.Items.Count, because you did not put anything in CheckGroup1.Items using OI for instance.

#### Doe

• New Member
• Posts: 17
##### Re: How to find the number of items checked in a check group?
« Reply #2 on: March 29, 2021, 12:39:28 am »
So how can I fix it?

#### Doe

• New Member
• Posts: 17
##### Re: engkin
« Reply #3 on: March 29, 2021, 12:43:24 am »
How can I fix this problem?

#### engkin

• Hero Member
• Posts: 2721
##### Re: How to find the number of items checked in a check group?
« Reply #4 on: March 29, 2021, 01:54:56 am »
Select the GroupBox and look for Items in the Object Inspector. Open them and add the items you want, one in each line. When you close it, the check boxes will be visible in the GroupBox.

#### josh

• Hero Member
• Posts: 827
##### Re: How to find the number of items checked in a check group?
« Reply #5 on: March 29, 2021, 01:55:57 am »
Hi

It may help if you upload the complete project.

Using your code to calculate the checked I have created a project for you; hopefully it may help
Development Installation Lazarus 1.3, FPC 2.7.1,Windows 7/8 32/64, OSX, *nix

Test Environment Lazarus & FPC Trunk on Windows and OSX (Cocoa Mainly on OSX). Testing also Crosscompile windows to OSX..
Any posts made from 2015 will be based on Lazarus Trunk.