Author: Lou Adler
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.
However, in spite of that, a component or object array could be quite useful;
especially if you're performing the same thing on a group of the same types of
components sequentially. A good example of this is how instantiate a series of
threads of the same type all at once in my article Waiting for Threads. Instead of
making a separate variable declaration for each thread that I need to create, I
instead create an array of the threads, and instantiate them with a FOR loop. This
is not only much more convenient, the coding it saves makes this approach much more
efficient with respect to productivity. So there's a good argument why you might
want to create an array of components or objects. However, keep in mind what I said
above. If you're using an array merely to apply a group-based event handling
mechanism, it's probably better to stick with my example below.
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:
1 procedure TForm1.FormCreate(Sender: TObject);
2 var
3 I: Integer;
4 begin
5 for I := 1 to 8 do
6 arLbl[I] := TLabel(FindComponent('Label' + IntToStr(I)));
7 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:
8 procedure TForm1.Label1Click(Sender: TObject);
9 var
10 I: Integer;
11 begin
12
13 for I := 1 to 8 do
14 begin
15 arLbl[I].Caption := TLabel(Sender).Name;
16 arLbl[I].Font.Style := [];
17 arLbl[I].Font.Color := clWindowText;
18 arLbl[I].Color := clBtnFace;
19 end;
20
21 with TLabel(Sender) do
22 begin
23 Font.Style := [fsBold];
24 Font.Color := clWhite;
25 Color := clBlue;
26 end;
27
28 end;
29
30 //In the OnClick event handler above, the program goes through the entire array and
31 sets some basic properties. These are essentially the default properties of the
32 labels when they're dropped onto the form. Then, the procedure takes the Sender,
33 the label that fired off the event, and changes its appearance to white, bold text
34 with a blue background. This is much simpler than doing a big case or if..then.
35 Here' entire unit code:
36
37 unit lblform;
38
39 interface
40
41 uses
42 Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
43 Menus, StdCtrls;
44
45 type
46 TForm1 = class(TForm)
47 Label1: TLabel;
48 Label2: TLabel;
49 Label3: TLabel;
50 Label4: TLabel;
51 Label5: TLabel;
52 Label6: TLabel;
53 Label7: TLabel;
54 Label8: TLabel;
55 procedure Label1Click(Sender: TObject);
56 procedure FormCreate(Sender: TObject);
57 private
58 { Private declarations }
59 arLbl: array[1..8] of TLabel;
60 public
61 { Public declarations }
62 end;
63
64 var
65 Form1: TForm1;
66
67 implementation
68
69 {$R *.DFM}
70
71 procedure TForm1.Label1Click(Sender: TObject);
72 var
73 I: Integer;
74 begin
75
76 for I := 1 to 8 do
77 begin
78 arLbl[I].Caption := TLabel(Sender).Name;
79 arLbl[I].Font.Style := [];
80 arLbl[I].Font.Color := clWindowText;
81 arLbl[I].Color := clBtnFace;
82 end;
83
84 with TLabel(Sender) do
85 begin
86 Font.Style := [fsBold];
87 Font.Color := clWhite;
88 Color := clBlue;
89 end;
90
91 end;
92
93 procedure TForm1.FormCreate(Sender: TObject);
94 var
95 I: Integer;
96 begin
97 for I := 1 to 8 do
98 arLbl[I] := TLabel(FindComponent('Label' + IntToStr(I)));
99 end;
100
101 end.
|