Search code examples
delphidelphi-2005

Text Method doesn't work with Findclass(...) but with plain "TEdit"


I want to use findclass and findcomponent to be able to pass the sender component as parameter in a procedure.

Thank you for reading.

Edit: I use Delphi 2005


[Error]: E2003 Undeclared identifier: 'text'

TestMemo.Text := (FindComponent(VonKomponente.name) as
  (Findclass(vonkomponente.ClassType.ClassName))).text; -> does not work

TestMemo.Text := (FindComponent(VonKomponente.name) as TEdit).text; -> works

procedure TFormTest.Edit7DblClick(Sender: TObject);
begin
  MemoEdit((Sender as TComponent),'table','row');
end;


procedure TFormTest.MemoEdit(VonKomponente :TComponent;table,row : String);
begin
  FormTestMemo.Max_Textlaenge := get_length(table,row);
  FormTestMemo.Text := (FindComponent(VonKomponente.name) as
    (Findclass(vonkomponente.ClassType.ClassName))).text;
  If FormTestMemo.Showmodal = MrOk then
  begin
    ...
  end;
end;

Solution

  • What you are trying to do is not possible. You cannot pass a metaclass type determined at runtime to the as operator.

    For what you are trying to do, you will have to resort to using old-style RTTI via the TypInfo unit, in this case the TypInfo.GetStrProp() function, eg:

    uses
      ..., TypInfo;
    
    FormTestMemo.Text := GetStrProp(VonKomponente, 'Text');
    

    Note that not all text-based components have a Text property, some have a Caption property, eg:

    uses
      ..., TypInfo;
    
    var
      prop: PPropInfo;
    
    prop := GetPropInfo(VonKomponente, 'Text');
    if prop = nil then
      prop := GetPropInfo(VonKomponente, 'Caption');
    
    if prop <> nil then
      FormTestMemo.Text := GetStrProp(VonKomponente, prop)
    else
      FormTestMemo.Text := '';