Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Looping all components on a TabSheet

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?

like image 644
OZ8HP Avatar asked Dec 21 '22 08:12

OZ8HP


1 Answers

What's wrong is that you are looping over the Components property. 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 that pagUsers.Properties.ActivePage.ComponentCount is zero since the only thing on your form that owns anything is the form itself.

What you need to to is use ControlCount and Controls[] to iterate over the children of the tab sheet. Simply replace all use of ComponentCount with ControlCount, and likewise replace Components[] with Controls[].

Note that the ControlCount and Controls[] 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:

TabSheet := pagUsers.Properties.ActivePage;
for CheckBox in TControls.Enumerator<TcxDbCheckBox>(TabSheet) do
  ....
like image 200
David Heffernan Avatar answered Dec 24 '22 00:12

David Heffernan