用户名:  密码:   
您当前的位置:首页 > 程序设计 >  Delphi > 

关于Mdi主与子窗体相关代码

上次修改时间: 2023-05-11 11:28:20      来源:网络总结      作者:钢铁长陈
上一页      路径:DSD\Learn\Delphi语言\      下一页

设置主窗体与子窗体

错误一
Cannot Create form. No MDI forms are currently active. Process stopped. 原因:将主窗体FormStyle的属性设为了fsChildForm
错误二:Missing Connection or ConnectionString ,重新配置连接字符串
错误三:****
Project Project1.exe raised exception class EAccessViolation with message
原因:不能用form1.Show;的方式显示子窗体,只能用 newform:=Tform2.Create(Self);,详见下面的子窗体的显示


一、窗体属性设置
1. 主窗体的FormStyle属性设为fsMDIForm
2. 主窗体的FormStyle属性设为fsChildForm
3. Project--OPtions,将Form选择卡下,把要作为子窗体的移动到右边Available forms


二、子窗体的打开关闭,最大化
1. 子窗体的关闭,在子窗体的onclose事件加如下代码
procedure TForm2.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Action:=caFree;
end;

2. 子窗体的显示,在主窗体某个按钮加如下代码
procedure TForm1.form21Click(Sender: TObject);
var
  newform:TForm2;
  counter:integer;
begin
  Inc(counter);
  newform:=Tform2.Create(Self);
  newform.caption:='test'+IntToStr(counter);
  newform.show;
end;

3. 子窗体显示时最大化,在子窗体show事件加如下代码
procedure TForm2.FormShow(Sender: TObject);
begin
Self.WindowState:= wsMaximized;
end;

三、防止重复打开子窗体
1. 在打开窗体时用下面代码先判断
procedure TMainFr.N20Click(Sender: TObject);
begin
  if not assigned(CtCenterFr) then //先判断是否已经打开过
    Application.CreateForm(TCtCenterFr, CtCenterFr)
  else
    CtCenterFr.Show;
end;
2. 关闭窗体时释放窗体名
procedure TCtCenterFr.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Action:=caFree;
CtCenterFr:=nil;  //主要要加这句,否则if not assigned(CtCenterFr) then这里判断窗体还在
end;