2004. augusztus 14., szombat
Creating component arrays
Problem/Question/Abstract:
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:
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;
interface
uses
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.
Feliratkozás:
Megjegyzések küldése (Atom)
Nincsenek megjegyzések:
Megjegyzés küldése