AW: String2Class
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; |
AW: String2Class
Da es ja letztlich um das Setzen einer Property geht, geht auch das:
Delphi-Quellcode:
Aufruf erfolgt dann mit: SetControlProperty(Button1,'Exit');
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; Ist nur so hingetippert, also ungeprüft ... Gruß Thomas |
Alle Zeitangaben in WEZ +1. Es ist jetzt 13:37 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