C ++构建器-枚举TPanel上的组件 [英] C++ builder - enumerate components on TPanel
问题描述
枚举表单组件非常简单
for (int i=0;i<ComponentCount;i++)
{
ShowMessage(Components[i]->Name);
//.....
}
如果我只想枚举位于面板上的组件,则无法正常工作。
but the same thing does not work if I want to enumerate only the components which are located on Panel.
for (int i=0;i<Panel1->ComponentCount;i++)
{
ShowMessage(Panel1->Components[i]->Name);
//.....
}
因为
Panel1->ComponentCount;
只是零,而面板上有多个组件。因此,如何枚举Panel的子组件?
is just zero while having several components on Panel. So, how can I enumerate the child components of Panel?
推荐答案
ComponentCount
和 Components []
属性访问拥有组件的组件列表-将组件设置为其 Owner的组件
通过将该组件传递给其构造函数。在设计时创建的所有组件都具有父 TForm
(或 TFrame
或 TDataModule )。 code>)设置为其
所有者
。您的第一个循环是遍历TForm的自有组件,这就是它起作用的原因。您的TPanel不拥有任何组件,这就是第二个循环失败的原因。
The ComponentCount
and Components[]
properties access a component's list of owned components - components that have the component set as their Owner
by having that component passed to their constructor. All components created at design-time have the parent TForm
(or TFrame
or TDataModule
) set as their Owner
. Your first loop is iterating through the TForm's owned components, that is why it works. Your TPanel does not own any components, that is why the second loop fails.
您要查找的是 ControlCount
和 Controls []
属性。他们改为访问视觉控件的子控件列表-为视觉表示起见,已将父控件设置为其 Parent
的控件。只有 TWinControl
派生的控件(例如 TPanel
)可以具有子控件:
What you are looking for is the ControlCount
and Controls[]
properties instead. They access a visual control's list of child controls instead - controls that have the parent control set as their Parent
for purposes of visual representation. Only TWinControl
-derived controls (like TPanel
) can have child controls:
for (int i = 0; i < Panel1->ControlCount; ++i)
{
ShowMessage(Panel1->Controls[i]->Name);
//.....
}
这篇关于C ++构建器-枚举TPanel上的组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!