我创建了一个窗体,需要对这个窗体类实例话,但是我想让它只能被实例化一次,当这个实例销毁后才能创建另一个实例?

解决方案 »

  1.   

    一:创建一个互斥量或者信号量进行控件,就是网上说的如何控制某一程序只能打开一次一样二:
    procedure TForm1.Button1Click(Sender: TObject);
    begin
      if Form2=nil then
        Form1:=TForm2.Create(Application);
      Form2.Show;
    end;procedure TForm2.FormClose(...);
    begin
      Action:=caFree;
      Form2:=nil;
    end;
      

  2.   

    if Form1<>nil then //说明创建了。
    if Form1=nil then //说明没创建。
    在Form1 的OnDestroy 事件中
    Form1:=nil
    在Form1 的OnClose 中
    Action:=caFree;关键在每次创建时判断是Form1 是否为nil 
      

  3.   

    使用设计模式的Singleton Pattern
    示例如下:
    TMyForm = class (TForm)
     protected
       constructor CreateInstance;
       class function AccessInstance(Request: Integer): TMyForm;
     public
       constructor Create;
       destructor Destroy; override;
       class function Instance: TMyForm;
       class procedure ReleaseInstance;
    end;{--------------------------------------------------------------}
    implamentationconstructor TMyForm.Create;
    begin
      inherited Create;
      raise Exception.CreateFmt('Access class %s through Instance only',
                               [ClassName]);
    end;constructor TMyForm.CreateInstance;
    begin
      inherited Create;
    end;destructor TMyForm.Destroy;
    begin
      if AccessInstance(0) = Self then AccessInstance(2);
      inherited Destroy;
    end;class function TMyForm.AccessInstance(Request: Integer): TMyForm;
      const FInstance: TMyForm = nil;
    begin
     case Request of
      0 : ;
      1 : if not Assigned(FInstance) then FInstance := CreateInstance;
      2 : FInstance := nil;
      else
        raise Exception.CreateFmt('Illegal request %d in AccessInstance',
        [Request]);
     end;
     Result := FInstance;
    end;class function TMyForm.Instance: TMyForm;
    begin
      Result := AccessInstance(1);
    end;
    class procedure TMyForm.ReleaseInstance;
    begin
      AccessInstance(0).Free;
    end;这样就能保证TMyForm只有一个实例了。假设你的TMyForm有一个X的变量,你就可以这样使用它TMyForm.Instance.X := 11;在你第一次使用TMyForm的时候TMyForm的实例就被创建了。你以后每次使用TMyForm的时候都得用TMyForm.Instance的方法来返回TMyForm的实例。
    要销毁TMyForm请调用TMyForm.ReleaseInstance。
      

  4.   

    to hexenzhou(甲骨文) :
    class function TMyForm.AccessInstance(Request: Integer): TMyForm;
    const FInstance: TMyForm = nil;
    begin
    case Request of
    0 : ;
    1 : if not Assigned(FInstance) then FInstance := CreateInstance;
    2 : FInstance := nil;
    else
    raise Exception.CreateFmt('Illegal request %d in AccessInstance',
    [Request]);
    end;
    Result := FInstance;
    end;
    这段代码不能编译,常量不能fu值,怎么办啊?
      

  5.   

    是的啊,被加了关键字const的常量是受更改保护的。
      

  6.   

    这样就可以了
    class function TMyForm.AccessInstance(Request: Integer): TMyForm;
    {$J+}
    const FInstance: TMyForm = nil;
    {$J-}
    begin
    case Request of
    0 : ;
      

  7.   

    if not Assigned(MyForm) then
      MyForm.Create(Self);procedure TMyForm.FormClose(...);
    begin
      Action:=caFree;
    end;
    procedure TMyForm.FormDestroy(...);
    begin
      MyForm:=nil;
    end;
      

  8.   

    在类的定义中加一个记数的,FORMCOUNT:INTEGER=0;
    当类实力化的时候,先检查他的值,为1表示有一个了,否则增加一次,不可以吗?