0

I want to create a dialog from current form class, and expect to get back a value from the dialog.

This is sample coding.

with TFormClass(FindClass('Tf_dialog_partner')).Create(Application) do
  try
    ShowModal;
    Value := DialogPublicVar;
  except
    Free; 
  end;

DialogPublicVar is a public variable of Tf_dialog_partner (TForm's descendant) class, for right now in my coding this current class doesn't use the Tf_dialog_partner's unit in the USES clause, I just use FindClass function, i can create a new form just fine.

This coding is error because this current class is not aware of Tf_dialog_partner's attributes, so it doesn't recognize DialogPublicVar.

Please help, how to make this current class to aware of DialogPublicVar.

Thanks everyone.

4

2 回答 2

2

如果返回的值是一个整数,一个简单的选择是让 ShowModal() 本身返回该值。当对话框准备关闭时,它可以将其 ModalResult 属性设置为所需的值,并且 ShowModal() 将返回该值。

否则,您可以将变量更改为类的已发布属性,然后使用 RTTI 通过TypInfo.pas单元中可用的功能访问它。

另一种选择是在表单类随后实现的共享单元中定义一个接口,其中该接口声明了一个表单覆盖以检索值的方法。然后,您的其余代码可以通过该函数查询该接口的对话框Supports()并调用公开的方法。

于 2013-05-18T18:10:27.990 回答
1

试试这个(整数)或改变你的类型。

var a: Integer;  
...  
a := MyFormDialog.ShowDialog(...);  
if (a = 5)  
  DoWork()  
else  
  DoNotWork();  
...


function TMyFormDialog.ShowDialog(...): Integer;  
begin  
  ...  
  ShowModal;  
  ...  
  if(A)  
    result := 5;  
  else  
    result := 2;  
end;
于 2013-05-23T08:31:58.730 回答