Delphi-Quelltext
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19:
| private MyLabels : Array[0..4] of TLabel;
procedure TForm1.Button1Click(Sender: TObject); var i : integer; begin for i := 0 to 4 do begin MyLabels[i] := TLabel.Create(Form1); with MyLabels[i] do begin Parent := Form1; Top := 10+10*i; Left := 10+10*i; Caption := 'Text'+IntToStr(i); end; end; end; |
Wobei man, wenn die zu setzenden Eigenschaften überall gleich sind, oder nur einen Datentyp (z.B. Integer) auch ein wenig anders vorgehen kann:
Delphi-Quelltext
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30:
| private MyLabels : Array[0..4] of TLabel;
const MyProps: array[0..4] of record Top, Left: Integer; Caption: String; end = ( (Top: 10; Left: 10; Caption: 'Text0'), (Top: 20; Left: 20; Caption: 'Text1'), (Top: 30; Left: 30; Caption: 'Text2'), (Top: 40; Left: 40; Caption: 'Text3'), (Top: 50; Left: 50; Caption: 'Text4') );
procedure TForm1.Button1Click(Sender: TObject); var i : integer; begin for i := 0 to 4 do begin MyLabels[i] := TLabel.Create(Form1); with MyLabels[i] do begin Parent := Form1; Top := MyProps[i].Top; Left := MyProps[i].Left; Caption := MyProps[i].Caption; end; end; end; |
Oder, wenn halb Du nur einen Typ hast, könntest Du das sogar (via RTTI) so machen:
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32:
| private MyLabels : Array[0..4] of TLabel;
const MyProps: array[0..9] of record Index: Integer; PropName: String; PropValue: Integer; end = ( (Index: 0; PropName: 'Top'; PropValue: 10), (Index: 1; PropName: 'Top'; PropValue: 20), (Index: 2; PropName: 'Top'; PropValue: 30), (Index: 3; PropName: 'Top'; PropValue: 40), (Index: 4; PropName: 'Top'; PropValue: 50), (Index: 0; PropName: 'Left'; PropValue: 10), (Index: 1; PropName: 'Left'; PropValue: 20), (Index: 2; PropName: 'Left'; PropValue: 30), (Index: 3; PropName: 'Left'; PropValue: 40), (Index: 4; PropName: 'Left'; PropValue: 50) );
procedure TForm1.Button1Click(Sender: TObject); var i : integer; begin for i := 0 to 4 do begin MyLabels[i] := TLabel.Create(Form1); MyLabels[i].Parent := Form1; end; for i := Low(MyProps) to High(MyProps) do SetIntProperty(MyLabels[MyProps[i].Index], MyProps[i].PropName, MyProps[i].PropValue); end; |
Alles eine Frage der Anforderungen. Wobei mit etwas mehr Aufwand der letzte Teil auch für unterschiedliche Datentypen geht, wenn man da bei seiner vordefinierten Datenstruktur etwas umbaut.
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.