Get controls' Caption on one form

Dear all,

How do I get all controls' Caption on one form into a ListBox, ie. Label, RadioButton, CheckBox, GroupBox, Button?

I use following code, but a compilation error is prompted out.

procedure TfrmTest.FormCreate(Sender: TObject);
var
  i: Integer;
begin
  for i := 0 to ComponentCount - 1 do
  begin
    if Components[i] is TControl then
      ListBox1.Items.Add( TControl( Components[i] ).Caption ); <- error
   end;
end;

Error: Undeclared identifier: 'Caption'

TIA

Lurc
lurcAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

rwilson032697Commented:
Try this:

procedure TfrmTest.FormCreate(Sender: TObject);
var
  i: Integer;
begin
  for i := 0 to ComponentCount - 1 do
  begin
    if Components[i] is TWinControl then
      ListBox1.Items.Add( TWinControl ( Components[i] ).Caption ); <- error
   end;
end;

Cheers,

Raymond.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rwilson032697Commented:
Actually, this will be better:

type
  TMyControl = class(TControl);

Try this:

procedure TfrmTest.FormCreate(Sender: TObject);
var
  i: Integer;
begin
  for i := 0 to ComponentCount - 1 do
  begin
    if Components[i] is TControl then
      ListBox1.Items.Add(TMyControl( Components[i]).Caption );
   end;
end;

Cheers,

Raymond.  
0
DrDelphiCommented:
Actually this is better yet... Raymond's example will given you a blank where a control is with no Caption... for example a Timage. My routine checks to see if that property even applies to the component in question:


Function Tform1.ObjHasProp(Obj:Tobject;Prop:String):Boolean;
var Props:Tstrings;
    ret:integer;
begin
 Try
    props:=getobjProps(obj);
    ret:=Props.IndexOf(prop);
      if ret > -1 then
      begin
        result:=true;
      end
      else
      result:=false;
  Finally
   Props.free;
end;
end;



procedure TForm1.Button1Click(Sender: TObject);
var
  i: Integer;
begin
  for i := 0 to ComponentCount - 1 do
  begin
    if (Components[i] is TControl) and ObjhasProp(Tobject(components[i]),'Caption') then
      ListBox1.Items.Add(TMyControl( Components[i]).Caption + (TMycontrol(components[i]).Name));


   end;
end;
 
0
C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

DrDelphiCommented:
neglected to mention... you have to add TYPEInfo to your uses for this...

Good luck!!
0
lurcAuthor Commented:
Thanks for your help, Raymond.

Lurc
0
DrDelphiCommented:
I was robbed! <g>
0
rwilson032697Commented:
Happens to the best of us :-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.