Autor Beitrag
Frühlingsrolle
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: So 07.11.10 06:24 
- Nachträglich durch die Entwickler-Ecke gelöscht -
BenBE
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: So 07.11.10 08:19 
ausblenden 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:

ausblenden 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:

ausblenden volle Höhe 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:
31:
32:
private
  MyLabels : Array[0..4] of TLabel;

const MyProps: array[0..9] of record
        Index: Integer;
        PropName: String;
        PropValue: Integer; //Hier die das Problem wegen Datentyp
    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.
Frühlingsrolle
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: So 07.11.10 19:57 
- Nachträglich durch die Entwickler-Ecke gelöscht -