Delphi MDI程序 父窗体如何调用当前活动子窗体的函数/过程?

一个MDI文本文件编辑器

打开了N个子窗体

子窗体的.pas文件有一些public的过程和函数

我想在父窗体调用当前活动的子窗体函数

我用Self.ActiveChildForm无法调用

直接frmEdit.xxxx运行出错

求大家指导,谢谢!

回复于: 2013-01-21 15:31:31

将子窗口的函数定义为类函数

calss function a(const s: string): Boolean;

有两种解决办法,一种就是上楼说的,定义成class function静态方法,第二种就是普通的方法:

Delphi/Pascal code

var

AForm : TForm;

begin

AForm := Self.ActiveMDIChild;

if AForm <> nil then

(AForm as TForm2).func('Hello');

end;

这两种方法都必须在MDI窗体中uses子窗体。

静态方法就一句话:

Delphi/Pascal code

TForm2.func2(1);

form2窗体两个被调方法

Delphi/Pascal code

TForm2 = class(TForm)

private

{ Private declarations }

public

function func(Astring : string) : Boolean;

class function func2(AInteger : Integer) : Boolean;

{ Public declarations }

end;