0

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;
Ken White
  • 123,280
  • 14
  • 225
  • 444
Huppel
  • 25
  • 5
  • 2
    FindClass returns a TPersistentClass which doesn't have any Text. You probably want to use RTTI. – Sertac Akyuz Feb 15 '19 at 20:47
  • 1
    Look at the source code of TMemo. It doesn't have a `Text` property. It has a `Lines` property which in turn has a `Text` one, so you can write `Memo1.Lines.Text := 'Hello'`. Also, you can't call `ShowModal` on `TestMemo` if `TestMemo` is of type `TMemo`. – MartynA Feb 15 '19 at 20:50
  • TestMemo is a Form. Max_Textlaenge and Text are public variables. Sorry i will edit. – Huppel Feb 15 '19 at 20:59
  • @SertacAkyuz - I understand the point with the TPersistentClass now. But how do i use RTTI for a better result? Thank you. – Huppel Feb 15 '19 at 21:03
  • http://docwiki.embarcadero.com/RADStudio/Tokyo/en/Working_with_RTTI – Sertac Akyuz Feb 15 '19 at 21:18
  • @Sertac: Poster is using D2005. Tokyo RTTI won't help. They'll need TypInfo style RTTI instead. – Ken White Feb 15 '19 at 21:45
  • 1
    @Ken thanks! Huppel, refer to [here](http://www.blong.com/conferences/borconuk98/delphirtti/cb140.htm), although old, it is more fun than the official [docs](http://docwiki.embarcadero.com/Libraries/Tokyo/en/System.TypInfo). I can't find any conceptual topics in the official documentation. – Sertac Akyuz Feb 15 '19 at 22:03
  • @Sertac: I couldn't find anything in the official docs either, but didn't have much time to look. – Ken White Feb 15 '19 at 23:08
  • @KenWhite - thank you – Huppel Feb 18 '19 at 14:24
  • @SertacAkyuz - thank you as well, you got me into the right direction – Huppel Feb 18 '19 at 14:25

1 Answers1

2

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 := '';
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770