I have the following code which shoul loop all components on a given tab on my tabsheet. I have tried many variants of the same code found on the net, but I simply can't get it to work. First I check if it is the right tab - that works. Then I check to see how many components - that doesn't work. It says 0 component even though I now that there are 2 panels with 9 checkboxes total.
procedure TfrmHsUsers.pagUsersClick(Sender: TObject);
var
i: integer;
Fieldname: string;
begin
if pagUsers.Properties.ActivePage.Name = 'tabProgram' then
begin
ShowMessage(IntToStr(pagUsers.Properties.ActivePage.ComponentCount));
for i := 0 to pagUsers.Properties.ActivePage.ComponentCount - 1 do
if (pagUsers.Properties.ActivePage.Components[i]) is TcxDbCheckBox then
begin
Fieldname := TcxDbCheckBox(pagUsers.Properties.ActivePage.Components[i]).DataBinding.DataField;
TcxDbCheckBox(pagUsers.Properties.ActivePage.Components[i]).Enabled := Settings.License.IsEnabled(Fieldname);
end;
end;
end;
Any hints to what might be wrong in my code?
What's wrong is that you are looping over the
Componentsproperty. That lists the components that are owned by the tab sheet. For components created in the form designer, the form is the owner. So it is expected thatpagUsers.Properties.ActivePage.ComponentCountis zero since the only thing on your form that owns anything is the form itself.What you need to to is use
ControlCountandControls[]to iterate over the children of the tab sheet. Simply replace all use ofComponentCountwithControlCount, and likewise replaceComponents[]withControls[].Note that the
ControlCountandControls[]properties only give the immediate children. Since you have panels you most likely have the panels as children of the tab sheet, and the check boxes as children of the panels. So you need to iterate over the children of the panels.My answer here shows one way to do that. If you use the code I presented there then your iteration over checkboxes can be written very simply indeed: