简体   繁体   English

如何遍历 TGroupBox 并检查其子项的状态?

[英]How can I iterate over a TGroupBox and check the state of its children?

Using C++ Builder 10.4 Community edition, I have a TGroupBox populated with some TCheckbox and TButton controls.使用 C++ Builder 10.4 社区版,我有一个TGroupBox填充了一些TCheckboxTButton控件。 I want to iterate over the TGroupBox to get the state of each TCheckBox .我想遍历TGroupBox以获取每个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?有人知道我该怎么做吗?

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. TWinControl::Controls属性不是它自己的对象,因此您不能按照您尝试的方式将其分配给局部变量。

Also, there is no ChildrenCount property in TWinControl .此外, TWinControl中没有ChildrenCount属性。 The correct property name is ControlCount instead.正确的属性名称是ControlCount

The C++ equivalent of Delphi's is operator in this situation is to use dynamic_cast ( cppreference link ) and check the result for NULL .在这种情况下,Delphi 的is运算符的 C++ 等效项是使用dynamic_cast ( cppreference link ) 并检查结果是否为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.您在最初的问题中没有明确表示您需要 FMX 的解决方案。 What I posted above is for VCL instead.我上面发布的内容是针对 VCL 的。 The FMX equivalent would look more like this: FMX 等价物看起来更像这样:

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...
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM