Solved

Programmatically find components on a Form

Posted on 2006-11-29
4
198 Views
Last Modified: 2010-04-05
I need to be able to find (and do some processing) all (in this case) TSpeedButton of a Form.
In fact I use hundreds of forms in one large application.
The main program manages (show etc...) them.
But it should be able to do some rework on all the TSpeedButtons of these forms (to standardise them) before to show them.
So I would like to write a procedure, says StandardiseForm(F:TForm) that will do that work.
Is this possible ?
How ?
0
Comment
Question by:LeTay
  • 4
4 Comments
 
LVL 28

Expert Comment

by:TName
Comment Utility
Hi, do you mean something like this?

procedure TForm1.Button1Click(Sender: TObject);
var
i:Integer;
begin
  for i:=0 to Form2.ComponentCount-1 do begin
     if Form2.Components[i] is TSpeedButton then begin
       ShowMessage(TSpeedButton(Form2.Components[i]).Caption);
       //Do whatever with the button
     end;
  end;
  //Form2.show;
end;


BTW, you can only access TSpeedButtons from within an application. They don't have window handles, so no chance accessing them via api from another application...
0
 
LVL 28

Accepted Solution

by:
TName earned 500 total points
Comment Utility

procedure StandardiseForm(F:TForm);
var
i:Integer;
begin
   for i:=0 to F.ComponentCount-1 do begin
     if F.Components[i] is TSpeedButton then begin
       TSpeedButton(F.Components[i]).Caption:='ChangedCaption'+IntToStr(i);
       TSpeedButton(F.Components[i]).Width:=100;
       TSpeedButton(F.Components[i]).Height:=40;
     end;
  end;
  //Form2.show;
end;


procedure TForm1.Button1Click(Sender: TObject);
begin
  StandardiseForm(Form2);
end;
0
 
LVL 28

Expert Comment

by:TName
Comment Utility
Form2.Show
should read
F.Show
Don't know if you want to use Show there or not...
0
 
LVL 28

Expert Comment

by:TName
Comment Utility
Easier to handle maybe:

procedure StandardiseForm(F:TForm);
var
i:Integer;
sb:TSpeedButton;
begin
   for i:=0 to F.ComponentCount-1 do begin
     if F.Components[i] is TSpeedButton then begin
       sb:=TSpeedButton(F.Components[i]);
       with sb do begin
         Caption:='ChangedCaption'+IntToStr(i);
         Width:=100;
         Height:=40;
         Font.Color:=clRed;
       end;
     end;
  end;
  //F.show;
end;
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now