MDI子窗体调用问题(100分)

  • 主题发起人 主题发起人 海皇
  • 开始时间 开始时间

海皇

Unregistered / Unconfirmed
GUEST, unregistred user!
有多个子窗体,判断子窗体是否存在,如不存在就创建并显示,然后在子窗体下用一个按钮调用新的编辑窗体,可是调用之后,显示子窗体为nil。
第一次启动软件没有问题。关闭子窗体再打开之后就有这个问题。
启动软件时是用Application.CreateForm(TPayOutFrm, PayOutFrm)。
关闭之后用下面函数创建子窗体PayOutFrm。
procedure TMainFrm.SubFromShow(Caption: string; SubFrmClass: TFormClass);
var
I: Integer;
SubFrm: TForm;
begin
for I := 0 to Screen.FormCount - 1 do // Iterate
begin
if (Screen.Forms.Caption = Caption) then
begin
Screen.Forms.BringToFront();
Exit;
end
else
begin
SubFrm := SubFrmClass.Create(application);
SubFrm.Show();
Exit;
end;
end;
end;
这个函数有问题吗?
 
窗体关闭的时候释放了吗?
看看form.onclose事件
还有啊,你的代码好象不对头啊,按照你的代码,如果循环100次的话,假如你的窗体是在第90个上,也就是i=90,那么前面你会创建90次啊.
你else 中的代码不能写在循环内部的.
 
函数循环没有问题,
关闭时用的Action := cafree;
只是不明白SubFrm := SubFrmClass.Create(application)与Application.CreateForm(TPayOutFrm, PayOutFrm)有什么区别!
用Application.CreateForm(TPayOutFrm, PayOutFrm)时,新建编辑窗体可以执行PayOutFrm.PayOutSource.DataSet.Cancel,
用SubFrm := SubFrmClass.Create(application)时,新建编辑窗体执行PayOutFrm.PayOutSource.DataSet.Cancel时显示PayOutFrm为nil。
好像窗体没有加载,但程序运行时明明能看到PayOutFrm窗体,只是在后台。
 
Destory事件中:Form1:=nil;
 
procedure TForm.FormClose(Sender: TObject;
var Action: TCloseAction);
begin
Action := caFree;
Form:= nil;
end;
 
CreateForm method (TApplication):
Delphi syntax:

procedure CreateForm(InstanceClass: TComponentClass; var Reference); virtual;
Description

Call CreateForm to dynamically create a form at runtime. Developers do not need to add code for creating most forms, because typically one or more calls to CreateForm are added automatically to the project's source when using the form designer.

CreateForm creates a new form of the type specified by the FormClass parameter and assigns it to the variable given by the Reference parameter. The owner of the new form is the Application object.

Note: By default, the form created by the first call to CreateForm in a project becomes the application's main form.


constructor Create(AOwner: TComponent);
This allocates storage for the new object on the heap, sets the values of all ordinal fields to zero, assigns nil to all pointer and class-type fields, and makes all string fields empty. Other actions specified in the constructor implementation are performed next; typically, objects are initialized based on values passed as parameters to the constructor. Finally, the constructor returns a reference to the newly allocated and initialized object. The type of the returned value is the same as the class type specified in the constructor call.
 
我想在PayOutEDITFrm窗体下调用执行PayOutFrm.PayOutSource.DataSet.Cancel;
但是PayOutFrm关闭之后重新打开,在PayOutEDITFrm窗体下执行PayOutFrm.PayOutSource.DataSet.Cancel时提示PayOutFrm为nil,系统找不到PayOutFrm下的控件。
 
多人接受答案了。
 
后退
顶部