Einzelnen Beitrag anzeigen

Benutzerbild von Sharky
Sharky

Registriert seit: 29. Mai 2002
Ort: Frankfurt
8.251 Beiträge
 
Delphi 2006 Professional
 
#8

Re: button, label... : variable

  Alt 19. Mai 2007, 06:40
Hai dino,

hier noch ein anderer Lösungsansatz.
Delphi-Quellcode:
uses
  TypInfo;

resourcestring
  SUnknownProperty = 'Eigenschaft %s existiert nicht.';

procedure MySetWideStrProp(Instance: TObject; const PropName: string; const Value: Widestring);

var
  PropInfo: PPropInfo;
begin
  PropInfo := GetPropInfo(Instance, PropName);
  if PropInfo = NIL then
  begin
    raise EPropertyError.CreateResFmt(@SUnknownProperty, [PropName]);
  end;
  SetWideStrProp(Instance, PropInfo, Value);
end;

procedure TDemo_Form.btn_SetCaptionClick(Sender: TObject);
var
  myObjects: array[1..4] of TObject;
  ndx: Integer;
begin
  myObjects[1] := Button1;
  myObjects[2] := Label1;
  myObjects[3] := CheckBox1;
  myObjects[4] := RadioButton1;
  // Ab hier werden die Captions gesetzt
  for ndx := 1 to High(myObjects) do
  begin
    MySetWideStrProp(myObjects[ndx], 'Caption', Format('Caption %d', [ndx]));
  end;
end;
Wenn es bei dir immer nur die Captions sind kannst Du das in MySetWideStrProp auch hardcoden und musst das Property dann nicht mehr bei den Parametern mit übergeben.
Stephan B.
"Lasst den Gänsen ihre Füßchen"
  Mit Zitat antworten Zitat