IT SOLUTIONS
Your full service technology partner! 
-Collapse +Expand
Delphi
Search Delphi Group:

Advanced
-Collapse +Expand Delphi To/From
To/FromCODEGuides
-Collapse +Expand Delphi Store
PRESTWOODSTORE

Prestwood eMagazine

July Edition
Subscribe now! It's Free!
Enter your email:

   ► KBProgrammingDelphi for W...Using Data   Print This     
  From the April 2008 Issue of Prestwood eMag
 
Delphi Using Data:
Updating components on a Form using the Components Array
 
Posted 18 years ago on 10/21/2002
Take Away: This example shows how to update a large number of components having the same type and similar characteristics.

KB100032

If you have a large Form with many components on it, and you wish to assign all of the components of a certain type the same property, you may find that you have to code the same assignment line for every component on the form that meets your need.

There is a way to streamline this process using the Components array on the Form. This is a Delphi collection object (available with any descendant of TComponent), that lists all of the components contained on the Form.

Here is a code example that demonstrates how you could disable all of the buttons on a Form that are TButton components:

procedure TForm1.DisableAllButtons;

var

   x : integer;

begin

   for x := 0 to (Form1.ComponentCount - 1) do

   begin

       if (Form1.Components[x] is TButton) then

         TButton(Form1.Components[x]).Enabled := False;

   end;

end;

In this code example, all of the labels on the form will be assigned the same caption. The labels all have the same characteristic - that is, their object names are all "Label##" where "##" is the numeric suffix automatically assigned by the component editor when the TLabel is dropped on the Form:

pprocedure TForm1.SetAllCaptions;

var

   x : integer;

begin

   for x := 0 to (Form1.ComponentCount - 1) do

   begin

       if (Form1.Components[x] is TLabel) then

         if (Copy(TLabel(Form1.Components[x]).Name,1,5) = 'Label') then

         TLabel(Form1.Components[x]).Caption := 'Hi!';

   end;

end;


Comments

1 Comments.
Share a thought or comment...
Comment 1 of 1
And remember, if the changes you want to make can be made at design time, you can simply open the form in text mode and use the Edit/Replace commands from the toolbar to make the changes to the form. Since the IDE is a two-way operation, any changes you make to the form will be immediately reflected in the code. -Larry Drews
Posted 18 years ago
 
Write a Comment...
...
Sign in...

If you are a member, Sign In. Or, you can Create a Free account now.


Anonymous Post (text-only, no HTML):

Enter your name and security key.

Your Name:
Security key = P184A1
Enter key:
KB Post Contributed By Scott Wehrly:

Scott Wehrly is currently working on .Net web applications for the gaming industry. Scott is a former employee of Prestwood Software (he was a Development Manager). Scott's specialties include C#, ASP.Net, MSSQL Server 2005, Delphi, SQL databases, C++, C, and Windows programming in general. When time allows, he participates in this online community.

Visit Profile

 KB Article #100032 Counter
9010
Since 4/2/2008
Sales Website: www.prestwood.com Or visit our legacy sales site: 
legacy.prestwood.com


©1995-2020 Prestwood IT Solutions.   [Security & Privacy]