Home > Net >  How can I iterate over a TGroupBox and check the state of its children?
How can I iterate over a TGroupBox and check the state of its children?

Time:06-30

Using C Builder 10.4 Community edition, I have a TGroupBox populated with some TCheckbox and TButton controls. I want to iterate over the TGroupBox to get the state of each TCheckBox. How can I do this?

I tried this:

auto control = groupBxLB->Controls;

for( uint8_t idx = 0; idx < groupBxLB->ChildrenCount; idx  ) {
    if( control[idx] == TCheckBox) {
        //get the state of the TCHeckBox
    }
}

but no success.

Does anybody have an idea how I can do this?

CodePudding user response:

The TWinControl::Controls property is not an object of its own, so you can't assign it to a local variable, the way you are trying to.

Also, there is no ChildrenCount property in TWinControl. The correct property name is ControlCount instead.

The C equivalent of Delphi's is operator in this situation is to use dynamic_cast (cppreference link) and check the result for NULL.

Try this:

for(int idx = 0; idx < groupBxLB->ControlCount;   idx) {
    TCheckBox *cb = dynamic_cast<TCheckBox*>(groupBxLB->Controls[i]);
    if (cb != NULL) {
        // use cb->Checked as needed...
    }
}

UPDATE:

You did not make it clear in your original question that you wanted a solution for FMX. What I posted above is for VCL instead. The FMX equivalent would look more like this:

auto controls = groupBxLB->Controls;

for(int idx = 0; idx < controls->Count;   idx) {
    TCheckBox *cb = dynamic_cast<TCheckBox*>(controls->Items[idx]);
    if (cb != NULL) {
        // use cb->IsChecked as needed...
    }
}
  • Related