Delphi MDI中打开子窗体
Form1是主窗体 Form2是子窗体。我想要在Form1中打开From2 代码是这样写的
function TForm1.FindForm(caption: string): bool;vari:integer;begin //result:=false;//防止出现函数返回值没有定义的警告 result:=true;//防止出现函数返回值没有定义的警告 for i:=0 to Form1.MDIChildCount do begin // 通过对窗体标题判断窗体是否存在 if Form1.MDIChildren[i].Caption=caption then begin //如果窗体已存在,则恢复窗体,并返回true SendMessage(mdichildren[i].Handle, WM_SYSCOMMAND, SC_RESTORE, 0); mdichildren[i].Enabled:=true; mdichildren[i].Show; mdichildren[i].SetFocus; result:=true; break; end else //如窗体不存在,则返回false,程序将创建窗体 result:=false; endend;procedure TForm1.Button1Click(Sender: TObject);Var Form2:TForm2;begin if FindForm('Form2')=false then begin Form2:=TForm1.Create(application); Form2.Caption:='Form2'; end;end;end.FindForm多余procedure TForm1.Button1Click(Sender: TObject);beginif not AsSigned(Form2) Thenbegin Form2:=TForm2.Create(application); Form2.Caption:='Form2';endelse Form2.BringToFront;end;另:procedure TForm2.FormDestroy(Sender: TObject);begin Form2:=nil;end;
[解决办法]
我喜欢用mdichildren[i].Name来判断
[解决办法]
Var
Form2:TForm2;
Form2:=TForm1.Create(application);->>Form2:=TForm2.Create(application);