var
 F:TForm1;
begin
 F:=TForm1.Create();
 ....//就用F来调用TForm1
end;

解决方案 »

  1.   

    我在form2中已经动态创建了一个form1,并且在form1中连上了数据库等其它东西,但是要在form3,form4中调用form1,如果在form3,form4中再重新动态创建form1的话,那么原来form1中的东西就全没了?请问怎样在form3,form4等中调用form1
      

  2.   

    你的问题是很正常的。
    你说在Form3,Form4再重新创建Form1。这是的Form1肯定不是Form2创建的Form1了。这些Form1只是同一个类(TForm1)的不同实例
    如果你想在Form3,4里引用Form2创建的Form1。那么你可以把Fomr1设成全局变量,然后再Form3,4里引用Form2这个单元,这样就可以直接调用Form1了。
    但是还有一点要注意:
    在Form3,4并不知道Form1是否创建的时候要调用最好加上一段保护代码:
    if not Assigned(Form1) then
      //如果Form1没有创建的话;
    else
      //如果Form1没有创建了
      

  3.   

    begin
      with TForm.Create(Application) do try
        ShowModal;
      fianlly
        Free;
      end;
    end;
      

  4.   

    不要动态创建form1,在project中将form1设置成自动创建form1即可。如果不想form1自动显示,将其的Visible属性设置为False
      

  5.   

    这是我动态创建窗体的例程
    用Delphi动态几个相同的窗体
    1.先定原窗体名如: 
      uses Outputtest
    2.在定义几个该窗体的变量
      Test1Form,Test2Form,Test3Form:TOutputTestForm;
      同时定义对应的几个布尔变量:
      Test1,Test2,Test3:Boolean;
    3.在对应的事件中加入
      if not Test1 then
          begin
            test1Form := TOutputTestForm.Create(Application);
            test1Form.Caption :='test1';
            test1Form.ShowCaption(1);
          end
        else
          Test1Form.WindowState :=wsNormal;
        test1Form.show;
    4.
    procedure TOutputTestForm.ShowCaption(Captions: Integer);
    begin
      FormFlag:=Captions;
      Name:='Test'+InttoStr(captions)+'Form';
      case captions of
          1:
            begin
              caption:='窗体'+InttoStr(captions);
              MainMDIForm.test1:=True;
            end;
          2:
            begin
              caption:='窗体'+InttoStr(captions);
              MainMDIForm.test2:=True;
            end;
          3:
            begin
              caption:='窗体'+InttoStr(captions);
              MainMDIForm.test3:=True;
            end;
      end;
      Left  :=0;
      Top    :=0;
      Height := 445;
      Width  := 790;
    end;
    5.在窗体关闭时
      case UserStrToInt(Copy(caption,Length(caption),1)) of
        1:
            begin
              Test1:=False;
              MainMDIForm.test1:=False;
            end;
        2:
            begin
              Test2:=False;
              MainMDIForm.test2:=False;
            end;
        3:
            begin
              Test3:=False;
              MainMDIForm.test3:=False;
            end;
      END;