SRL Archive - OSR Help

checkbox


IwriteCode

2011-07-07 09:47:38

OK so i have about 30 checkboxes in my form and on a button press i want to be able to check which of each check box is check and store a 1 inside if it is check or a 0 if it is not checked inside a 30 integer wide array. I want to do this in a simple loop but i dont know how to loop through each check box. The checkboxes are named cb1,cb2,cb3,cb4…,…cb30.

Heres my current code:

[PHP]for icb:= 1 to 30 do
begin
if (cb[icb].checked) then begin
ItemControlArray[icb]:=1;
end else begin ItemControlArray[icb]:=0; end;

end;[/PHP]
This gives me an error cause I just dont know if its possible to loop through the checkboxes cb[1..30] in anyway?

Sex

2011-07-07 09:52:51

What you would do is declare an array like var CheckboxArr : array of TCheckbox; and then you would loop through like this:
h := high(CheckBoxArr);
for i := 0 to h do
CheckedArr[i] := Integer(CheckBoxArr[i].Checked);

IwriteCode

2011-07-07 09:55:08

awesome thank for quick reply!

IwriteCode

2011-07-07 10:22:57

ok it seems that the array is filled with all zeros even when the checkboxes are ticked. here is my code

[PHP]var CheckboxArr : array of TCheckbox;
ItemControlArray : Array[1..80] of integer;

procedure button1Procedure(sender: TObject);
var
i,h:integer;
begin


h := high(CheckBoxArr);
for i := 0 to h do
begin
ItemControlArray := Integer(CheckBoxArr.Checked);
end;

//display values in array
for i := 1 to 80 do
begin
writeln(ItemControlArray) ;
end;
end;[/PHP]

edit:looks like h is giving value of -1

Sex

2011-07-07 10:34:23

It looks like you haven't set the length of the checkbox array. Try putting SetArrayLength(CheckboxArr, 30) above that line.

IwriteCode

2011-07-07 10:53:28

ah got it working my silly mistake:duh: