I.T. Discussion Community!
-Collapse +Expand
Delphi
Search Delphi Group:

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

Prestwood eMagazine

June 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 16 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 16 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 = P1241A1
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
7882
Since 4/2/2008
-
   Contact Us!
 
PrestwoodBoards.com was developed and is maintainted by me. Do you have a question or suggestion? Do you see a problem? Contact me now. My goal is to build an ad-free and spam-free source of I.T. information with many contributers (ok to promote your website/company in your bio). Yes, my company Prestwood IT Solutions is mentioned in my bio which shows with every post, but you can contribute and promote your pet project too!

2,019 People Online Now!!  
Sign In to see who's online now!  Not a member? Join now. It's free!
Show more stats...


©1995-2018 PrestwoodBoards  [Security & Privacy]
Professional IT Services: Coding | Websites | Computer Tech