AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Thema durchsuchen
Ansicht
Themen-Optionen

String2Class

Ein Thema von Sploing · begonnen am 11. Mär 2015 · letzter Beitrag vom 12. Mär 2015
Antwort Antwort
Seite 2 von 2     12   
Blup

Registriert seit: 7. Aug 2008
Ort: Brandenburg
1.429 Beiträge
 
Delphi 10.4 Sydney
 
#11

AW: String2Class

  Alt 12. Mär 2015, 13:19
Irgend ein Unterscheidungskriterium braucht der Compiler, um die richtige Methode für die gesuchte Komponente bereitzustellen.
Entweder der Name unterscheidet sich (z.B. FindComponentEdit, FindComponentComboBox, usw.) oder die Parameter:
Delphi-Quellcode:
type
  TEditClass = class of TEdit;
  TComboBoxClass = class of TComboBox;
  TRadioButtonClass = class of TRadioButton;

function FindComponent(AOwner: TComponent; AClassType: TEditClass; const AName: string): TEdit; overload;
function FindComponent(AOwner: TComponent; AClassType: TComboBoxClass; const AName: string): TComboBox; overload;
function FindComponent(AOwner: TComponent; AClassType: TRadioButtonClass; const AName: string): TRadioButton; overload;

implementation

function FindComponentType(AOwner: TComponent; AClassType: TComponentClass; const AName: string): Pointer;
begin
  Result := AOwner.FindComponent(AName);
  if Assigned(Result) and (not (TComponent(Result) is AClassType)) then
    Result := nil;
end;

function FindComponent(AOwner: TComponent; AClassType: TEditClass; const AName: string): TEdit;
begin
  Result := FindComponentType(AOwner, AClassType, AName);
end;

function FindComponent(AOwner: TComponent; AClassType: TComboBoxClass; const AName: string): TComboBox;
begin
  Result := FindComponentType(AOwner, AClassType, AName);
end;

function FindComponent(AOwner: TComponent; AClassType: TRadioButtonClass; const AName: string): TRadioButton;
begin
  Result := FindComponentType(AOwner, AClassType, AName);
end;
Delphi-Quellcode:
procedure TForm1.Test;
begin
  FindComponent(Self, TEdit, 'Edit1').Text := 'Test';
  FindComponent(Self, TComboBox, 'ComboBox1').ItemIndex := -1;
  FindComponent(Self, TRadioButton, 'RadioButton1').Checked := True;
end;
  Mit Zitat antworten Zitat
Benutzerbild von TRomano
TRomano

Registriert seit: 24. Nov 2004
Ort: Düsseldorf
190 Beiträge
 
Delphi 11 Alexandria
 
#12

AW: String2Class

  Alt 12. Mär 2015, 15:26
Da es ja letztlich um das Setzen einer Property geht, geht auch das:

Delphi-Quellcode:
type TDictionaryControls = TDictionary<TControl,string>; // im String wird der Property-Name gespeichert
     
     TForm1 = class(TForm)
...
      procedure SetControlProperty(pControl : TControl; const PropValue : string);


var dictControls : TDictionaryControls;

...

procedure TForm1.FormCreate(Sender: TObject);
begin
  dictControls : TDictionaryControls.Create(3);
  dictControls.Add(Button1,'Caption');
  dictControls.Add(Label1 ,'Caption');
  dictControls.Add(Edit1 ,'Text');
end;

procedure TForm1.SetControlProperty(pControl : TControl; const PropValue : string);
var aContext : TRTTIContext;
    aProperty : TRTTIProperty;
    aValue : TValue;
    sPropName : string;
begin
  if (pControl = nil) or (not dictControls.TryGetValue(pControl,sPropName) then Exit;
 
  // mit Prüfung auf Property-Name
  aProperty := aContext.GetType(pControl.ClassInfo).GetProperty(sPropName);
  if Assigned(aProperty) then begin
     aValue := TValue.From(PropValue);
     aProperty.SetValue(pControl,aValue);
  end;
 
  // hier ohne Prüfung und mit "alter" RTTI
  System.TypInfo.SetStrProp(pControl,sPropName,PropValue);
end;
Aufruf erfolgt dann mit: SetControlProperty(Button1,'Exit');

Ist nur so hingetippert, also ungeprüft ...

Gruß Thomas
Thomas Forget
  Mit Zitat antworten Zitat
Antwort Antwort
Seite 2 von 2     12   


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 02:03 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