Text Method 不适用于 Findclass(...) 但适用于普通 "TEdit"

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

我想使用 findclass 和 findcomponent 将发送者组件作为过程中的参数传递。

感谢您的阅读。

编辑:我使用 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;

您尝试执行的操作是不可能的。您不能将在运行时确定的元类类型传递给 as 运算符。

对于您正在尝试做的事情,您将不得不通过 TypInfo unit, in this case the TypInfo.GetStrProp() 函数求助于使用旧式 RTTI,例如:

uses
  ..., TypInfo;

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

请注意,并非所有基于文本的组件都有 Text 属性,有些组件有 Caption 属性,例如:

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 := '';