两个方法:
procedure GetChildControls(AControl: TWinControl; Strs: TStrings);
var
I: Integer;
begin
for I := 0 to AControl.ControlCount - 1 do
begin
Strs.Add(AControl.Controls.Name);
if AControl.Controls is TWinControl then
GetChildControl(TWinControl(AControl.Controls), Strs);
end;
end;
procedure GetChildControls(AParent: TWinControl; Strs: TStrings);
function ParentIs(AControl: TControl): Boolean;
begin
Result := True;
while AControl.Parent <> nil do
begin
if AControl.Parent = AParent then
Exit;
AControl := AControl.Parent;
end;
Result := False;
end;
var
I: Integer;
begin
Strs.Clear;
Strs.BeginUpdate;
for I := 0 to Form1.ComponentCount - 1 do
if Form1.Components is TControl then
if ParentIs(TControl(Form1.Components)) then
Strs.Add(Form1.Components.Name);
Strs.EndUpdate;
end;
第一个可能更快