Visit our Sponsor   Visit our Sponsor
delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
497 Users Online NOW
Have a look at your member-status

connecting people's knowledge


  - Recent ArticlesRSS feed for Recent Articles on delphi3000.com
  - List of All Articles
  - Top Viewed Articles
  - Articles (+Attachem.)
  - Articles Of Interest
  - Categories
  - Top Uploader
  - Search
  - Index

  - My Home
  - Submit an Article
  - My Articles
  - My Personal Data
  - My Bookmarks
  - Activities
  - Login/Logout

  - Sign Up
  - Why Sign Up
  - Newsletter

  - Press
  - Advertise

  - Contact
  - Feedback





Community
Borland
ClubeDelphi
Dr. Bob
UK-BUG
Delphi Meetings
Planeta Delphi



Loremo - the 1.5 liter car coming in 2009




Startblatt.de






Share this article with friendsShare this article with friends
Rate this articleRate this article - to keep the quality of delphi3000.com !
Comment this article or read through previous comments (12)


Delphi trick for implementing VB's Control ArraysGo to Malek BADI's websiteFormat this article printer-friendly!Bookmark function is only available for registered users!
Product:
Delphi all versions
Category:
VCL-General
Skill Level:
Scoring:
Last Update:
02/21/2002
Search Keys:
delphi delphi3000 article borland vcl code-snippet Controls arrays vcl VB control-arrays
Times Scored:
7
Visits:
4890
Uploader: Malek BADI
Company: ELMAWRID Automation Centre
Reference: www.ELMAWRID.com
 
Question/Problem/Abstract:
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 ===================





Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment
real world
    Eber Irigoyen (Feb 21 2002 6:18PM)

on my article Get all table names of a database I use this technique in a "real world example"

salu2
EberSys
Respond

Uh, well yes, but what about...
    Sam Spade (Feb 21 2002 3:41AM)

TWinControl has a Controls property that contains all of the controls that it owns.  Since TForm descends from TWinControl, just iterate through all the controls on a form like:

with MyForm do
  for I := 0 to TForm1.ControlCount - 1 do
    if Control[I] is TEdit then
      TEdit(Control[I]).Caption := 'Hi! I'm edit control ' + IntToStr(I)

or something to the sort.
Respond

RE: Uh, well yes, but what about...
Robert Shanbaum (Feb 22 2002 7:40PM)

The TComponent.Components may be more useful than TWinControl.Controls. The former exposes all the controls for which the form is the Owner (usually, all the controls on a form) - the latter, only the controls for which the form is the Parent - which would not include, for example, controls on a TPanel.
Respond

Come to think of it...
Robert Shanbaum (Feb 22 2002 8:13PM)

Let's take a more objective approach:

unit EditArray;

interface

uses Classes, StdCtrls;

type

  TEdits = class(TList)
  private
    function GetListItem(i: integer): TEdit;
  public
    constructor Create(Owner: TComponent);
    property List[i: integer]: TEdit read GetListItem; default;
  end;


implementation

{ TEdits }

constructor TEdits.Create(Owner: TComponent);
var
  i: integer;
begin
  inherited Create;
  for i := 0 to (Owner.ComponentCount - 1) do begin
    if Owner.Components[i] is TEdit then
      Add(Owner.Components[i])
  end
end;

function TEdits.GetListItem(i: integer): TEdit;
begin
  if (i < Count) then
    result :=  items[i]
  else
    result :=  NIL
end;

end.


Then, in a form, just instance this class in FormCreate:

  Edits := TEdits.Create(Self)

from which point you can do things like:

if assigned(Edits[3]) then
  Edits[3].SetFocus


Tell me that's not a little more elegant.

You could obviously make this into a component...
Respond

Reply: RE: Come to think of it...
Malek BADI (Feb 23 2002 5:14AM)

Sure this is an elegant trick...
But, still ... The trick I proposed is more streamlined for
migrating VISUAL BASIC's control arrays ...
If you know waht i was originally talking about ( ie. if you did use
VB's indexed control array, and had been a VB developer)
then you would apreciate this trick...
Again there are hundereds of ways of geting things done...
and sometimes a specific aproach is more appropritiate even for
the sake of clarity or ease of migration ...

Respond

RE: Reply: RE: Come to think of it...
Robert Shanbaum (Feb 23 2002 12:55PM)

You miss the point. My TEdits object has exactly the same functionalty as your array - it just eliminates the (rather byzantine) need to count edit controls and declare an array of the correct size. When you add another edit control to your form, the "array" is sized automatically.
Respond

RE: Reply: RE: Come to think of it...
Robert Shanbaum (Feb 23 2002 3:13PM)

Or, if you didn't want to add all the TEdits on a form to the "array", you could dispense with scanning the Controls property, and just use the Add method to add the ones you do want to include - again, the advantage being, you don't have to manually match your array declaration to the number of controls you do add.

If you really, really want to use an array, at least use a dynamic one, e.g.,

EditsArray: array of TEdit;

and then in the code that assigns the controls to the array, set the length:

SetLength(EditsArray,3);
EditsArray[0] := Edit1;
EditsArray[1] := Edit4;
EditsArray[2] := Edit6;

Even so, the derivative of TList is better; I'd say that the TList could be considered the much-improved successor to the array type.

Respond

RE: RE: Reply: RE: Come to think of it...
Arrays vs. OpenArrays (Feb 24 2002 8:57AM)

You should consider the following points:
1) The advantages of having a fixed length array contrary to an open array..
2) The simplicity of just declaring an array of SPECIFIC elements (whetever these elements could be) and then the ability to iterate
for each element in this array. Which wil be very fast compared to using open arrays...
3) This trick is applicable and may be useful for specific classes of problems...
4) The way the trick is presented in my example is simple in order to illustrate the point of having an array of Components, and hence the ability to iterate through the whole set of elements with a FOR loop
or whetever means....
5) Your suggestions are just a different approach for implementing the trick, though it may be applicable for a different purpose...

6) FIXED ARRAYS with a PRE-SET Number of ELEMANTS are some-times morre appropriate, as in my Playing CARDS games...As we know that we had ONLY 40 images each rerpresenting a only one Card
and I wanted a way to iterate through the elements, in order to
set thier position on the form, and to shuufle the PLAYING CRADS DECK, and to count scores etc...
7) As they say ... "There are many ways to get to Rome..."
Hope this clarifies may point, and emphasisez that it was not a bezantyne approch.. in fact it is a simple, neat , programmatic, systematic approach....
8) Open arrays are in most Programming languages a just there for convience not for speed... In fact if you are forced to suse open arrays
you should instead use a linked list.. for sake of speed and professinalism.
Your further comments are welcomee..  ;-)


Respond

Corrections to previous comment:
Malek BADI (Feb 24 2002 9:04AM)

As I couldn't make typing corrections to my previous reply, here are the spelling corrections for items 6 & 8 :
6) FIXED ARRAYS with a PRE-SET Number of ELEMANTS are some-times morre appropriate, as in my Playing CARDS games...As we know that we had ONLY 40 images each rerpresenting only one Playing Card
and I wanted a way to iterate through the elements, in order to
set thier position on the form, and to shuffle the PLAYING CRADS DECK, and to count scores etc...
8) Open arrays are in most Programming languages are just there for convience not for speed... In fact if you are forced to use open arrays
you should instead use a linked list.. for the sake of speed and proffesionalism.

Respond

RE: Further Corrections to previous comment:
Malek BADI (Feb 24 2002 9:05AM)

MORE not morre...
I wish we could edit our responses in this sie !!! ;-)
Respond

RE: RE: Further Corrections to previous comment:
Malek BADI (Feb 24 2002 9:07AM)

Site not sie..
Common ... Delphi3000 admins, give us the ability to make corrections
to our replies in this site...
Respond

RE: RE: Further Corrections to previous comment:
emre (Jul 1 2004 5:32PM)

may ý ask a questions???

you are the cities what scout around????


RECOMMEND
www.programlama.com
Respond

RE: RE: Uh, well yes, but what about...
sdvf xvbb (May 25 2008 5:48PM)

whois
alexa
alexa
prsitecheck
statistics
Respond














 
Sign up to consume product discounts for Bronze memberships !

read more


  Visit our Sponsor

 

  Community Ad of
R. Lefter
 
   














 







     
  Copyright © 2000 - 2007 delphi3000.com - All rights reserved. Terms of use. || Privacy
delphi3000.com is a service by bluestep.com IT-Services GmbH (Vienna)