devxlogo

Creating Component Arrays

Creating Component Arrays

Question:
In Visual Basic I can create component arrays, but I can’t seem to find a way to in Delphi. Does Delphi have this capability?

Answer:

Do You Really Need a Component Array?

Visual Basic has no way to share events among components other than through component arrays, which is why it’s so easy to program in Visual Basic. But with Delphi, groups of components can share a common event handler merely by assigning the event handler code for the event in the Object Inspector. Unlike VB, which requires the components to be of the same type when assigning a shared event handler, dissimilar components in Delphi can share the same event handler.

For instance, let’s say you want a TSpeedButton to behave exactly like a TMenuItem. If you already have code for the TMenuItem, all you have to do is select the button and go to its events page. Under its OnClick event, drop down the list of available event handlers and select the event handler for the OnClick Event of the TMenuItem.

What I discussed above is usually the reason for wanting to build a component array. However, there are certain circumstances under which you would want a component array. One of those circumstances is handling processing for a group of like objects in a loop. For example, let’s say you have a bunch of TLabels on a form whose appearance and text you want to update at runtime in response to a mouse click. Rather than creating one big switch statement or several if..then statements to change the text and appearance of the TLabels, it’s much easier to process the changes in a loop.

See also  Why ChatGPT Is So Important Today

Creating an Array of TLabels

The first thing that needs to be done is to make declaration of the array of TLabels. For this example, I’ve dropped eight TLabel components on a form. I want the array index to match the TLabel indexes, so I declare the array as follows under the private section of my form:

  arLbl : Array[1..8] of TLabel;

Next, because I know I’ll be needing the label array almost immediately, I’ll pop some code into the form’s OnCreate event to initialize the values of the label array elements:

procedure TForm1.FormCreate(Sender: TObject);var  I : Integer;begin  for I := 1 to 8 do    arLbl[I] := TLabel(FindComponent(‘Label’ + IntToStr(I)));end;

Notice that I performed a typecast around the FindComponent function. FindComponent returns the component specified by the name input into its formal parameter. However, without the typecast, FindComponent will return a type of TComponent, and a type mismatch compiler error will occur, despite the fact that it returns the TLabel we’re looking for. So by forcing the typecast of TLabel, we ensure that the type assigned to the label array element is correct.

Then, because the TLabels will share the same event handler, all we need to do is set up an event handler for the OnClick event of one of the TLabels, then set up the other labels’ OnClick event handlers to point to the OnClick event of the label we wrote the code under:

procedure TForm1.Label1Click(Sender: TObject);var  I : Integer;begin  for I := 1 to 8 do begin    arLbl[I].Caption    := TLabel(Sender).Name;    arLbl[I].Font.Style := [];    arLbl[I].Font.Color := clWindowText;    arLbl[I].Color      := clBtnFace;  end;  with TLabel(Sender) do begin    Font.Style := [fsBold];    Font.Color := clWhite;    Color      := clBlue;  end;end;

In the OnClick event handler above, the program goes through the entire array and sets some basic properties. These are essentially the default properties of the labels when they’re dropped onto the form. Then, the procedure takes the Sender, the label that fired off the event, and changes its appearance to white, bold text with a blue background. This is much simpler than doing a big case or if..then. Here’s the entire unit code:

unit lblform;interfaceuses  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,  Menus, StdCtrls;type  TForm1 = class(TForm)    Label1: TLabel;    Label2: TLabel;    Label3: TLabel;    Label4: TLabel;    Label5: TLabel;    Label6: TLabel;    Label7: TLabel;    Label8: TLabel;    procedure Label1Click(Sender: TObject);    procedure FormCreate(Sender: TObject);  private    { Private declarations }    arLbl : Array[1..8] of TLabel;  public    { Public declarations }  end;var  Form1: TForm1;implementation{$R *.DFM}procedure TForm1.Label1Click(Sender: TObject);var  I : Integer;begin  for I := 1 to 8 do begin    arLbl[I].Caption    := TLabel(Sender).Name;    arLbl[I].Font.Style := [];    arLbl[I].Font.Color := clWindowText;    arLbl[I].Color      := clBtnFace;  end;  with TLabel(Sender) do begin    Font.Style := [fsBold];    Font.Color := clWhite;    Color      := clBlue;  end;end;procedure TForm1.FormCreate(Sender: TObject);var  I : Integer;begin  for I := 1 to 8 do    arLbl[I] := TLabel(FindComponent(‘Label’ + IntToStr(I)));end;end.

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist