Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find all the controls that are visible to the user

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

like image 447
norgepaul Avatar asked Sep 18 '12 11:09

norgepaul


1 Answers

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;
like image 87
Andreas Rejbrand Avatar answered Sep 23 '22 15:09

Andreas Rejbrand