Mirror

How to assign multiple TEdit fields to variables (Views: 706)


Problem/Question/Abstract:

Is there an easier way to assign multiple Edit fields to variables without individually setting each one? Here is a sample code.

type
  testrec = record
    fees: array[1..10] of string[65];
  end;

var
  dat: testrec;

procedure FormToDat;
begin
  fees[1] := Edit1.Text;
  fees[2] := Edit2.Text;
  fees[3] := Edit3.Text;
  fees[4] := Edit4.Text;
  { ... }
end;

This sample code seems inefficient and I'm thinking there might be an easier way to do this.

Answer:

There are a wide variety of ways to do this in Delphi, here's one:

var
  I: Integer;
  C: TComponent;
begin
  for I := 1 to 10 do
  begin
    C := FindComponent('Edit' + IntToStr(I));
    if C is TEdit then
      TEdit(C).Text := Fees[1];
  end;
end;

You could also store references to the edits in a TList or an array, or you could also iterate through the Controls or Components properties.

<< Back to main page