array of labels or edit boxes

Does anyone know how to have an array of say, labels, or edit boxes? I would like to be able to access these objects in loops, rather than something messy like:
edit1.text := object[1];
:
:
edit50.text := object[50];
MaestroDaveAsked:
Who is Participating?
 
EdHillmannConnect With a Mentor Commented:
Hi Dave.  Generally, you can achieve this using either the Components property or the Controls property.  Quick definitions:

Components is defined by the TComponent class, and "lists components owned by the component".

Controls is defined by the TWinControl class, and "lists all children of the windowed control."

So, these properties will allow you access to all the controls (read visual) and/or components (read nonvisual) on a form (or anything else that inherits from the right class).

So, as an example we'll use Controls.  If I've gone through and named all my edit boxes "MyAutoEdit1", "MyAutoEdit2" and so on, I can loop through the Controls array and assign values to them from a StringList I prepared earlier (I'm using a stringlist to contain the default values, but you don't have to... just for the purpose of the example)...

procedure TMyForm.SetEditValues;
var
  i, vPos, vIndex: integer;
begin
  for i := 0 to ControlCount - 1 do begin
    if (Controls[i] is TEdit) then begin
      vPos := Pos('MyAutoEdit', Controls[i].Name;
      if (vPos > 0) then begin
        vIndex := StrToInt(Controls[i].Name, vPos + 11, Length(Controls[i].Name));
        (Controls[i] as TEdit).Text := FMyStringList.Strings[vIndex];
      end;
    end;
  end;

I haven't tested this, but it looks like it should work.

The other alternative would be to derive your own list, using a TList to store the references to the WinControls you want to access.

Hope this helps!
0
 
MaestroDaveAuthor Commented:
this line doesn't make sense:?
StrToInt(Controls[i].Name, vPos + 11, Length(Controls[i].Name))
0
 
EdHillmannCommented:
Oops!  I forgot to include the copy ...  It should read
StrToInt(Copy(Controls[i].Name, vPos + 11, Length(Controls[i].Name)))

Basically getting the integer value out of the Edit field name.  I chose vPos + 11 because that should give me the position after 'MyAutoEdit'
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
MaestroDaveAuthor Commented:
still can't get it to work.

I get the error message when I run it:
"" is not a valid integer value
0
 
EdHillmannCommented:
Final fix!  Change vPos + 11 to vPos + 10.  I've actually ran this one, using the following code (just a new application with a form with 5 edit fields and a button)....

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TForm1 = class(TForm)
    MyAutoEdit0: TEdit;
    MyAutoEdit1: TEdit;
    MyAutoEdit2: TEdit;
    MyAutoEdit3: TEdit;
    MyAutoEdit4: TEdit;
    Button1: TButton;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
    FMyStringList: TStringList;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
begin
  FMyStringList := TStringLIst.Create;
  with FMyStringList do begin
    Add('String0');
    Add('String1');
    Add('String2');
    Add('String3');
    Add('String4');
  end;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  FMyStringList.Free;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  i, vPos, vIndex: integer;
begin
  for i := 0 to ControlCount - 1 do begin
    if (Controls[i] is TEdit) then begin
      vPos := Pos('MyAutoEdit', Controls[i].Name);
      if (vPos > 0) then begin
        vIndex := StrToInt(Copy(Controls[i].Name, vPos + 10, Length(Controls[i].Name)));
        (Controls[i] as TEdit).Text := FMyStringList.Strings[vIndex];
      end;
    end;
  end;
end;

end.
0
 
OpitzSCommented:
You can either use Form1.Controls or you can use the following.
Let's say you have 50 TEdits, named Edit1 to Edit50, then you can use FindComponent, and find it by the name, but Controls is always faster.

To the proposed answer:
if you set the tag value to some certain value, you can check for this value, too, to make sure, you have found one of the TEdits you wanted to change.

Sven
0
 
MaestroDaveAuthor Commented:
cheers, dude!
0
All Courses

From novice to tech pro — start learning today.