我的表单上有页面控件。此页面控件有两个或三个选项卡。每个选项卡都有一些其他控件,如按钮,标签面板等。我能够找到所有控件,但如果某些控件位于面板顶部,那么我无法找到该控件
我的代码:
for I := 0 to Pagecontrol.Pagecount -1 do
begin
for J := 0 to pagecontrol.pages[i].controlcount - 1 do
showmessage(pagecontrol.pages[i].controls[J].name) // not able to find button whose parent is panel
end
当我做的时候
for J := 0 to pagecontrol.pages[i].componentscount- 1 do // it does not enter into loop
答案 0 :(得分:4)
Controls[]
属性列出了直接子项。你需要深入了解孩子的孩子,等等。通常,这是递归地完成的:
procedure WalkChildren(Parent: TWinControl; Visit: TProc<TControl>);
var
i: Integer;
Child: TControl;
begin
for i := 0 to Parent.ControlCount-1 do
begin
Child := Parent.Controls[i];
Visit(Child);
if Child is TWinControl then
WalkChildren(TWinControl(Child), Visit);
end;
end;
您可以这样称呼:
for i := 0 to PageControl1.PageCount-1 do
WalkChildren(
PageControl1.Pages[i],
procedure(Child: TControl)
begin
Memo1.Lines.Add(Child.Name);
end
);
甚至:
WalkChildren(
PageControl1,
procedure(Child: TControl)
begin
Memo1.Lines.Add(Child.Name);
end