Author: Tomas Rutkauskas
Is there an easier way to assign multiple Edit fields to variables without
individually setting each one? Here is a sample code.
1 type2 testrec = record3 fees: array[1..10] ofstring[65];
4 end;
5 6 var7 dat: testrec;
8 9 procedure FormToDat;
10 begin11 fees[1] := Edit1.Text;
12 fees[2] := Edit2.Text;
13 fees[3] := Edit3.Text;
14 fees[4] := Edit4.Text;
15 { ... }16 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:
17 var18 I: Integer;
19 C: TComponent;
20 begin21 for I := 1 to 10 do22 begin23 C := FindComponent('Edit' + IntToStr(I));
24 if C is TEdit then25 TEdit(C).Text := Fees[1];
26 end;
27 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.