Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Sonstige Fragen zu Delphi (https://www.delphipraxis.net/19-sonstige-fragen-zu-delphi/)
-   -   button, label... : variable (https://www.delphipraxis.net/92360-button-label-variable.html)

dino 18. Mai 2007 14:33


button, label... : variable
 
Hi DP, habe da mal ne Frage...

ich möchte eine Variable machen, in der einen bestimmten button oder ein label oder oder oder speichern kann

also um dann später per variable.caption:='Hier' oder so die variable benutzen zu können

weiss leider nicht, was ich dafür in die forensuche eingeben soll, bzw in die delphi hilfe, darum frage ich euch, ob es so einen variablentyp gibt

fLaSh11 18. Mai 2007 14:35

Re: button, label... : variable
 
Versuchs mal mit
Delphi-Quellcode:
(FindComponent(variable) as TLabel).Caption:='blah'

dino 18. Mai 2007 14:39

Re: button, label... : variable
 
klappt genial danke

nur ists in meinem fall TButton

eine allgemeine Form gibts wohl nicht, oder?

naja dann hättest du es mir schon gesagt...

fLaSh11 18. Mai 2007 14:44

Re: button, label... : variable
 
wie eine allgemeine Form?
Delphi-Quellcode:
FindComponent(variable)
ohe as gibts auch, kannst dann halt nur die Eigenschaften von TComponent benutzen...

Christian Seehase 18. Mai 2007 14:45

Re: button, label... : variable
 
Moin dino,

Du kannst die Klasse angeben, in der die von Dir gewünschte Eigenschaft eingeführt wird.
In diesem Falle also TControl, da TButton und TLabel von TControl abstammen (siehe Hilfe) und die Eigenschaft Caption von TControl erben.

dino 18. Mai 2007 14:48

Re: button, label... : variable
 
wobei die variable ja hier eine stringvariable ist und die funktion eine komponente mit diesen namen sucht...

aber für miene Zwecke geht es so schon sehr gut, danke

Sunlight7 19. Mai 2007 01:03

Re: button, label... : variable
 
Moin dino!

Meinst Du vielleicht so etwas?
Delphi-Quellcode:
   var Varibale:TComponent;
...
procedure TForm1.SetCaption;
begin
   If (Varibale is TButton) then
      TButton(Varibale).Caption:='Ich bin ein Button';

   If (Varibale is TLabel) then
      TLabel(Varibale).Caption:='Ich bin ein Label';

   If (Varibale is TCheckBox) then
      TCheckBox(Varibale).Caption:='Ich bin eine CheckBox';

   If (Varibale is TRadioButton) then
      TRadioButton(Varibale).Caption:='Ich bin ein RadioButton';
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
   Randomize;
   Case Random(4) of
      0: Varibale:=Button1;
      1: Varibale:=Label1;
      2: Varibale:=CheckBox1;
      3: Varibale:=RadioButton1;
   end;

   SetCaption;
end;

Sharky 19. Mai 2007 06:40

Re: button, label... : variable
 
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.


Alle Zeitangaben in WEZ +1. Es ist jetzt 01:57 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz