Thema: String2Class

Einzelnen Beitrag anzeigen

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