Jump to content
dummzeuch

Creating an array of controls in Delphi

Recommended Posts

One frequently asked question that still gets asked today goes like this: “How do I create an array of [component] and fill it with existing [component] instances from the form?” Where [component] usually is TLabel, TCheckbox or TEdit.

I’m going to outline some solutions here.

Let’s start by defining some parameters:

  1. We have got a (VCL) form
  2. On that form there are several controls of the same type. Let’s make them CheckBoxes.
  3. We want to do something with all these controls
  4. In order to make this easier, we want to create an array that contains all these controls

 

So, this is the form:

form-with-checkboxes.png.449cefb9e42161f7c0164ea749951dc0.png

 

read on in my blog post

Edited by dummzeuch

Share this post


Link to post

I'd add a scrollbox in the groupboxes first, and add insert the controls on the scrollboxes. Just to be sure that they'll be all visible.

 

Edit: Also, I'd add recursivity in the GetAllCheckboxes method, but that's only my taste.

  

Edited by aehimself

Share this post


Link to post

I'm not talking about dynamically adding checkboxes to groupboxes at runtime, but about adding those created in the designer to an array for easier processing.

Share this post


Link to post

What's wrong with just adding the controls to a collection? 

 

Controls.Add(control1);

Controls.Add(control2);

 

Etc.

 

  • Like 2

Share this post


Link to post

In some places I use such constructions
 

for ctrl in TArray<TControl>.Create(Button1, Checkbox1, Memo1) do
  ctrl.Enabled := True;

 

  • Like 6

Share this post


Link to post
9 hours ago, Fr0sT.Brutal said:

In some places I use such constructions
 


for ctrl in TArray<TControl>.Create(Button1, Checkbox1, Memo1) do
  ctrl.Enabled := True;

 

That's nifty ! I will put that one to my toolbox.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×