How can I find all the controls on a form that are currently visible to the user? i.e. list all the controls that can be tabbed to and are not hidden from view (e.g. on a non-visible tab sheet).
Since you write that you want to list the controls that you can tab to, I assume you are talking about windowed controls.
Then you can do simply
procedure TForm1.Button1Click(Sender: TObject);
var
i: integer;
begin
for i := 0 to ComponentCount - 1 do
if Components[i] is TWinControl then
if TWinControl(Components[i]).CanFocus then
Memo1.Lines.Add(Components[i].Name)
end;
if you know that the form owns all its children and no other controls. Otherwise, you have to do
procedure AddVisibleChildren(Parent: TWinControl; Memo: TMemo);
var
i: Integer;
begin
for i := 0 to Parent.ControlCount - 1 do
if Parent.Controls[i] is TWinControl then
if TWinControl(Parent.Controls[i]).CanFocus then
begin
Memo.Lines.Add(Parent.Controls[i].Name);
AddVisibleChildren(TWinControl(Parent.Controls[i]), Memo);
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
AddVisibleChildren(Self, Memo1);
end;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With