Public
     F_Department:T'Frm_Department;procedure TFrmMain.N1Click(Sender: TObject);
begin
  if application.FindComponent('Frm_Department') = Nil then
    begin
    F_Department:= TFrm_Department.Create(Application);
    F_Department.Show ;
    end
  else
    F_Department.BringToFront;  
end;我基本上打开每个窗体都要这样子走一遍,所以我想把它写成一个函数,这样每次都调用一下就可以了,
比如调用函数:ShowMDIChild(F_Department);于是我就写啊,写啊,可是写不出来,只能写成这样子:Function ShowMDIChild(Formname:TForm;ClsFrm: TFormClass):Boolean;Begin
  result:= false;
  if application.FindComponent(Formname.Name ) = Nil then
    begin
    Formname:= ClsFrm.Create(Application);
    Formname.Show ;
    Result:=True;
    end
  else
    Formname.BringToFront;  
End;
-----
好不容易写好了,可是调用的时候
ShowMDIChild(F_Department,TFrm_Department);
都提醒出错啊,哎哟,所以我就不知道怎么办才好,好心的人帮忙啊。
最好写成 ShowMDIChild(F_Department);
含一个参数的函数!谢谢!

解决方案 »

  1.   

    Public
         F_Department:T'Frm_Department;这个应该改成
    Public
         F_Department:TFrm_Department;
    ==============
    纯属笔误啊!
      

  2.   

    procedure ShowForm(FormName : TForm);
    begin
      if application.FindComponent(Formname.Name ) = Nil then
      begin
        Formname:= FormName.Create(Application);
        Formname.Show ;
      end
      else
        Formname.Show;
    end;
      

  3.   

    Function ShowMDIChild(Formname:TForm;ClsFrm: TFormClass):Boolean;Begin
      result:= false;
      if Formname = Nil then
        begin
        Formname:= ClsFrm.Create(Application);
        Formname.Show ;
        Result:=True;
        end
      else
        Formname.BringToFront;  
    End;没有测试
      

  4.   

    ShowMDIChild(var AForm:TForm;FormClass:TFormClass):Boolean;
    begin
      Result:=False;
      if not Assigned(AForm) then
      begin
        AForm:=FormClass.Create(Application);
        AForm.Show;
        Result:=True;
      end
      else
        AForm.BringToFront;
    end;
    var 
      NewForm:TNewForm;  NewForm:=nil;
      ShowMDIChild(TForm(NewForm),AFormClass)
      

  5.   

    to: idilent(不工作会发疯吗?)你那样写跟我这样写有什么区别啊?就是把
    application.FindComponent(Formname.Name ) = Nil  改成 Forname=nil啊,我自己写好的函数,这样调用都通不过,
    ShowMDIChild(F_Department,TFrm_Department);你那个还不是一样?
    晕~
      

  6.   

    to  FrameSniper(§绕瀑游龙§) :老大,你怎么知道什么时候改揭帖的?你又没测试过!
      

  7.   

    //为什么要返回值,何必不把代码变的精炼些
    procedure TForm1.ShowMDIChild(FForm:TForm;TBaseClass: TFormClass);
    Begin
      if not Assigned(FForm) then
        FForm:= TBaseClass.Create(Application);
      FForm.Show;
    End;
      

  8.   

    to  wsforqyc(大海):你的代码跟我的实现的功能不同!返回值可以判断我打开窗口是否成功啊!
      

  9.   

    to :hkbarton(宁静至远||淡泊明志)
    还是你提到了根本的东西!
    祝你明天就能上 红星!
    ======================
    调用:
    ShowMDIChild(F_Department,TFrm_Department);
    =======================
    Error Message:
    [Error] Incompatible types: 'TForm' and 'TFrm_Department'
    [Error] Incompatible types: 'TFormClass' and 'Class reference'
      

  10.   

    这样好了:
    Function ShowMDIChild(Formname:string;ClsFrm: TFormClass):Boolean;
    var
     ThisFrm:TForm;
    Begin
      result:= false;
      if application.FindComponent(Formname) = Nil then
        begin
        ThisFrm:= ClsFrm.Create(Application);
        ThisFrm.Show ;
        Result:=True;
        end
      else
        (application.FindComponent(Formname) as TForm).BringToFront;
    End;procedure TForm1.Button1Click(Sender: TObject);
    begin
     ShowMDIChild('Form2',TForm2);
    end;
      

  11.   

    当然对于你的调用,这样:
    ShowMDIChild('F_Department',TFrm_Department);
      

  12.   

    老哥
    application.FindComponent(Formname.Name ) 这一行在formName是nil的情况下就出错,知道我的和你的区别了吗?
      

  13.   

    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm1 = class(TForm)
        Button1: TButton;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementationuses Unit2;{$R *.dfm}
    Function ShowMDIChild(Formname:TForm;ClsFrm: TFormClass):Boolean;Begin
      result:= false;
      if Formname = Nil then
        begin
        Formname:= ClsFrm.Create(Application);
        Formname.Show ;
        Result:=True;
        end
      else
        Formname.BringToFront;  
    End;
    procedure TForm1.Button1Click(Sender: TObject);
    begin
      showMDIChild(Form2,TForm2);
    end;end.
    测试通过
      

  14.   

    to:hkbarton(宁静至远||淡泊明志);
    -------------
    你的想法很好,可是还是通不过。[Error] Incompatible types: 'TFormClass' and 'Class reference';
    ------------------------------------也就是说根本上还没解决为什么'TFrm_Department'不能作为是继承'TForm'的类调用呢?麻烦你能找出原因,当然可以先帮我解决这个函数问题再去研究,! 
      

  15.   

    你的错误信息说明了你对自己写的函数的调用不对。或者你得TFrmDepartment不是TForm的子类。
    而且application.FindComponent(Formname.Name )的确有问题。
      

  16.   

    不过AForm.BringToFront;也有问题,也可能使我的delphi的问题,当AFrom是由application创建时,就不达到目的。
    Function ShowMDIChild(Formname:TForm;ClsFrm: TFormClass):Boolean;Begin
      result:= false;
      if Formname = Nil then
      begin
        Formname:= ClsFrm.Create(Application);
        Result:=True;
      end;
        Formname.Show ;
    End;
    这样可能好一些。
      

  17.   

    参考一下以下的代码
    TParentForm = class(TForm)
    public
      property SQL: string read FSQL write FSQL;
      property ID: Integer read FID write FID;
      property IDClass: Integer read FIDClass write FIDClass;
      property TableName: string read FTableName write FTableName;
      property NoName: string read FNoName write FNoName;
    end;TAAMaterielEditFrm = class(TParentForm);end;TABUnitEditFrm  = class(TParentForm)end;TParentFormClass = class of TParentForm;procedure SomeCall(AClass: TParentFormClass);
    begin
      with AClass.Create(Application) do
      try
        ID := ...
        TableName := ....
        ....
        ShowModal;
      finally
        Free;
      end;
    end;Call:
      SomeCall(TAAMaterielEditFrm);
    or 
      SomeCall(TABUnitEditFrm);
      

  18.   

    不会吧~~~~~~~~,我都通过了,看来需要你贴出更完整的代码了,你的TFrm_Department是怎么继承的,这到底是个什么??
      

  19.   

    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TFrm_Department = Class of TForm
      TFrm_Department= class(TForm)
        Button1: TButton;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementationuses Unit2;{$R *.dfm}
    Function ShowMDIChild(ClsFrm: TFrm_Department):Boolean;Begin
      result:= false;
      with ClsFrm.Create(Application) do
          begin
            //还可以赋一些参数
            Show ;
          end
      Result:=True; 
    End;
    procedure TForm1.Button1Click(Sender: TObject);
    begin
      showMDIChild(TFrm_Department);
    end;end.
      

  20.   

    to 楼上各位老大:
    今天我把你们所提到的每一个思路都走一遍。
    下面就是我的结果:
    首先,Frm_Department 是由 FrmBase继承过来的。我要打开的窗口全部都是由FrmBase继承来的。FrmBase是由TForm继承来的,是不是就不能说 Frm_Department由TForm继承来的呢?
    其次,我用 yujohny(踏网无痕) 的方法,编译成功,函数如下:
    TBaseCls = class of TFrmBase;Function ShowMDIChild(Formname:TFrmBase;ClsFrm: TBaseCls):Boolean;Begin
      result:= false;
      //if application.FindComponent(Formname.Name ) = Nil then (用此句运行就会出错)
      if Formname=nil then (用此句窗体就会不断的创建新的。达不到要求)
        begin
        Formname:= ClsFrm.Create(Application);
        //Formname:= Class(FormName).Create(Application);
        Formname.Show ;
        Result:=True;
        end
      else
        Formname.BringToFront;  //把它Show在最前面
    End;调用:
    ShowMDIChild(Frm_Department,TFrm_Department);==========================谁能告诉我到底该怎么做 才能OK?
      

  21.   

    再继续补充一点。假如我先用
    if application.FindComponent(Formname.Name ) = Nil then 创建新的窗体,然后再调用
    if Formname=nil then 这个创建窗体,窗体就不会被不停的新建,但是如果我把创建的窗体关闭,然后再调用 if Formname=nil then 的话,就会出错。推断:也就是说用FindComponent(Formname.Name ) = Nil 创建的窗体,Frm_Department就不再是nil的了,即时将这个Frm_Department窗体关闭,Frm_Department也还存在你们所谓的堆栈(堆栈里到底是怎么处理的,我还是不太清楚,希望各位老大能够帮我解释解释原理)里,
    但是通过FindComponent(Formname.Name )找的结果还是nil。
    Delphi syntax:
    Indicates whether a given component is owned by the component.
    (我的理解:判断属于当前组件的一个组件是否已经given(我不知道given怎么理解,‘给出’?,但是应该不是创建的意思,也就是说组件还存在当前的component里,没被释放。))
    function FindComponent(const AName: string): TComponent;
    DescriptionFindComponent returns the component in the Components property array with the name that matches the string in the AName parameter. Use FindComponent to determine whether a given component is owned by another.Component name matches are not case sensitive.
    ===========================
    nil:
    『我在delphi的帮助里只查到这么一点,请大家再给我提供点资料,谢谢』
    The reserved word nil is a special constant that can be assigned to any pointer. When nil is assigned to a pointer, the pointer doesn't reference anything.
    --------------------------以上就是我要补充的几点,也是我根本就搞不明白为什么的地方!
      

  22.   

    to: hiflower(花)
    --------------------
    ShowMDIChild(var AForm:TForm;FormClass:TFormClass):Boolean;
    begin
      Result:=False;
      if not Assigned(AForm) then
      begin
        AForm:=FormClass.Create(Application);
        AForm.Show;
        Result:=True;
      end
      else
        AForm.BringToFront;
    end;
    var 
      NewForm:TNewForm;  NewForm:=nil;
      ShowMDIChild(TForm(NewForm),AFormClass)
    --------------------你的这个我倒可以试试,NewForm:=nil;
    关于这个if not Assigned(AForm) then
    已经被我排除了。
    ---------
    很谢谢你的关心!
      

  23.   

    问题解决了,虽然比较土一点,但是好用,又不会是 formname=nil。
    代码:
    ----------------------------
    Function ShowMDIChild(Formname:TFrmBase;FormNameStr:String;ClsFrm: TBaseCls):Boolean;Begin
      result:= false;
      //if Formname=nil then
       try
          if application.FindComponent(FormNameStr) = Nil then
              begin
            Formname:= ClsFrm.Create(Application);
            //Formname:= Class(FormName).Create(Application);
            Formname.Show ;
            Result:=True;
            end
          else
            Formname.BringToFront;  //把它Show在最前面
       except
          result:= false;
       end;
    End;
    -----------------------------调用:
    ShowMDIChild(Frm_Department,'Frm_Department',TFrm_Department);