Mega Code Archive

 
Categories / Delphi / Algorithm Math
 

Delphi trick for implementing VBs Control Arrays

Title: Delphi trick for implementing VB's Control Arrays Question: This is a delphi trick for implementing "arrays of components" as used in Visual Basic's "Control Arrays" which allow you to access Controls placed on a form via an index.. Answer: The example can be applied to any set of component such as TEdits, or TTables , or TImage etc.. See the example project CAProj as the trick is explained thoroughly.. Perhaps this trick would help to convert VB Developers to the exciting world of Delphi .... By Malek BADI B.Sc.(Eng.) M.Sc. www.ELMAWRID.com email: info@ELMAWRID.com or: Malek_BADI@hotmail.com ============ Example Form Unit Code ========================================== unit ControlArrayExampleForm; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TControlsArrayForm = class(TForm) Edit1: TEdit; Edit2: TEdit; Edit3: TEdit; Edit4: TEdit; Edit5: TEdit; Edit6: TEdit; Edit7: TEdit; Edit8: TEdit; Edit9: TEdit; Edit10: TEdit; Button1: TButton; Button2: TButton; Label1: TLabel; GroupBox1: TGroupBox; Memo1: TMemo; procedure FormCreate(Sender: TObject); procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); private { Private declarations } public { Public declarations } // Declare an array of whatever Components you want to handle // as a collection in an Array MyEdits : array [1..10] of TEdit; end; var ControlsArrayForm: TControlsArrayForm; implementation {$R *.DFM} procedure TControlsArrayForm.FormCreate(Sender: TObject); begin // First Assign Edit1 to Edit10 to corresponding // elements into the MyEdits Array MyEdits[1]:=Edit1; MyEdits[2]:=Edit2; MyEdits[3]:=Edit3; MyEdits[4]:=Edit4; MyEdits[5]:=Edit5; MyEdits[6]:=Edit6; MyEdits[7]:=Edit7; MyEdits[8]:=Edit8; MyEdits[9]:=Edit9; MyEdits[10]:=Edit10; end; procedure TControlsArrayForm.Button1Click(Sender: TObject); var i : integer; begin for i:=Low(MyEdits) to High(MyEdits) do MyEdits[i].Text:='Hello from Edit '+inttostr(i) end; procedure TControlsArrayForm.Button2Click(Sender: TObject); var i : integer; begin for i:=Low(MyEdits) to High(MyEdits) do begin MyEdits[i].Text:='Hello I am Edit '+inttostr(i); MyEdits[i].Font.Style:=[fsBold]; MyEdits[i].Color:=clYellow; end; end; end. ========================== F O R M ==================== object ControlsArrayForm: TControlsArrayForm Left = 241 Top = 120 Width = 524 Height = 440 Caption = 'Example of Controls Arrays' Color = clBtnFace Font.Charset = DEFAULT_CHARSET Font.Color = clWindowText Font.Height = -11 Font.Name = 'MS Sans Serif' Font.Style = [] OldCreateOrder = False Position = poScreenCenter OnCreate = FormCreate PixelsPerInch = 96 TextHeight = 13 object Label1: TLabel Left = 72 Top = 13 Width = 385 Height = 13 Caption = '(c) Malek BADI , www.ELMAWRID.COM' Font.Charset = DEFAULT_CHARSET Font.Color = clWindowText Font.Height = -11 Font.Name = 'MS Sans Serif' Font.Style = [fsBold] ParentFont = False end object Edit1: TEdit Left = 32 Top = 39 Width = 121 Height = 21 TabOrder = 0 Text = 'Edit1' end object Edit2: TEdit Left = 32 Top = 67 Width = 121 Height = 21 TabOrder = 1 Text = 'Edit2' end object Edit3: TEdit Left = 32 Top = 96 Width = 121 Height = 21 TabOrder = 2 Text = 'Edit3' end object Edit4: TEdit Left = 32 Top = 124 Width = 121 Height = 21 TabOrder = 3 Text = 'Edit4' end object Edit5: TEdit Left = 32 Top = 153 Width = 121 Height = 21 TabOrder = 4 Text = 'Edit5' end object Edit6: TEdit Left = 32 Top = 181 Width = 121 Height = 21 TabOrder = 5 Text = 'Edit6' end object Edit7: TEdit Left = 32 Top = 210 Width = 121 Height = 21 TabOrder = 6 Text = 'Edit7' end object Edit8: TEdit Left = 32 Top = 238 Width = 121 Height = 21 TabOrder = 7 Text = 'Edit8' end object Edit9: TEdit Left = 32 Top = 267 Width = 121 Height = 21 TabOrder = 8 Text = 'Edit9' end object Edit10: TEdit Left = 32 Top = 295 Width = 121 Height = 21 TabOrder = 9 Text = 'Edit10' end object Button1: TButton Left = 32 Top = 334 Width = 217 Height = 25 Caption = 'Perform some Action to Above Controls' TabOrder = 10 OnClick = Button1Click end object Button2: TButton Left = 32 Top = 367 Width = 217 Height = 25 Caption = 'Do Something Else for Above Controls' TabOrder = 11 OnClick = Button2Click end object GroupBox1: TGroupBox Left = 168 Top = 43 Width = 305 Height = 263 Caption = 'Example Description :' TabOrder = 12 object Memo1: TMemo Left = 11 Top = 28 Width = 281 Height = 206 Color = clInfoBk Font.Charset = DEFAULT_CHARSET Font.Color = clWindowText Font.Height = -11 Font.Name = 'MS Sans Serif' Font.Style = [] Lines.Strings = ( 'These are 10 TEdits which you' 'can Handle as an Array of Componetst ' '(ie Controls Array as in Visual Basic)' 'After Placing the Components ( i.e. Controls in VBSpeak)' 'on the form , Declare an Array (say MyEdits) with proper ' 'dimension of the same type of the controls.' 'In The Create Event of the form you Assign each Array ' 'elemnt to the corresponding component' 'Then you can easily handle all components' 'via the Array.. So you can use FOR ..loops, etc...') ParentFont = False ReadOnly = True TabOrder = 0 end end end ====================== E N D O F F O R M ===================