在程序运行中我生成了一个窗体,下次再生成时要判断它是否存在,如果存在则不生成,请问如何实现

解决方案 »

  1.   

    if FrmName = nil then
      

  2.   

    在Form1中:
    procedure TForm1.Button1Click(Sender: TObject);
    begin
      if not Assigned(Form2) then
      begin
         Form2:=  TForm2.Create(Application);
         Form2.Show;
      end
      else
         SetActiveWindow(Form2.Handle);
    end;在Form2中:
    procedure TForm2.FormClose(Sender: TObject; var Action: TCloseAction);
    begin
      Form2.Free;
      Form2:= nil;
    end;
      

  3.   

    var
      f: tobject;
      i: integer;
    const
      formname = 'Form2';
    begin
      form2.Show;
      showmessage('第一种方法:');
      f := Application.FindComponent(Formname);
      if f <> nil then
        Tform(f).Hide;  showmessage('第二种方法:');
      for I := 0 to Screen.FormCount - 1 do
        if Screen.Forms[i].Name = formname then
        begin
          Screen.Forms[i].Show;
          break;
        end;
    end;也可以用findwindow(..
      

  4.   

    还有更好的方法:
    form.show
    G_formName := form.name;
    G_formindex := Form.ComponentIndex;
    ====
    if Screen.Forms[G_FormIndex].name = G_formName then
      Screen.Forms[G_formIndex].show;
      

  5.   

    if form.active then
    begin
         showmessage('form已打开');
    end
    else
        form.show;