关于MDI窗体创建时发生EInvalidOperation异常的问题我自定义了一个函数,函数如下:
function TMainFrm.CreateForm(CForm:TForm):boolean;
var
  i:integer;
  FormExist:boolean;
begin
  FormExist:=false;
  if(CForm=Nil) then  //判断CFrom是否为空
    begin
     CreateForm:=false;  //函数返回值赋false
     exit;               //退出本函数
    end;
  for i:=0 to Screen.FormCount-1 do //判断窗体是否已经建立起来
    begin
     if Screen.Forms[i].ClassType=CForm.ClassType then  //判断窗体存在
       FormExist:=true;
    end;
  if FormExist=false then
   begin
     CreateForm:=false;  //函数返回值赋false
     exit;               //退出本函数
    end;   if CForm.WindowState=wsMinimized then
    ShowWindow(CForm.Handle,SW_SHOWNORMAL) //显示窗体
    else
    ShowWindow(CForm.Handle,SW_SHOWNA); //显示窗体
  if not CForm.Visible then
    CForm.Visible:=true;
  CForm.BringToFront;  //当前窗口显示在最前面
  CForm.SetFocus;
  CreateForm:=true;
end;    下面是调用过程,编译可以成功,但是执行下面application.CreateForm(Typxxfrm,ypxxfrm);时就会报EInvalidOperation异常,请大家指教。  procedure Tmainfrm.N4Click(Sender: TObject);
begin
    if CreateForm(ypxxfrm)=false then
      application.CreateForm(Typxxfrm,ypxxfrm);
end;

解决方案 »

  1.   

    if(CForm=Nil) then
    当CForm已经创建过一次并且释放的时候,如果不CForm := nil;赋值的话,CForm<>nil
      

  2.   

    EInvalidOperation is raised when an application attempts an operation that requires a window or widget handle on a component that does not have a parent (The Parent property is Nil in Delphi or NULL in C++). This exception can also occur if a drag-and-drop operation is attempted from a form (for example, when calling the BeginDrag method of a form).Delphi在线帮助里面的.
      

  3.   

    每个子窗体关闭的时候都要进行释放,并且保证你创建对象为NIL
      

  4.   

    各位老大,能否再说详细点,小弟还是不大明白
    当主窗体创建时,子窗体都没创建啊,但是一执行application.CreateForm(Typxxfrm,ypxxfrm);就报异常了
      

  5.   

    偶然间问题解决了,因为我的登录窗体也是用Application.createForm创建,并在主窗体之前,而Delphi会把第一个用Application.createForm创建的窗口作为主窗口的,而MDI主窗口必须为整个的主窗口才行。还是谢谢大家!
      

  6.   

    晕!用 if assigned(Frm_name) then来判断窗体是否创建!