Board index » delphi » Preventing Multiple Instances of a FORM (NOT whole app)

Preventing Multiple Instances of a FORM (NOT whole app)

Hi. How do I prevent multiple instances of a form from being created after
my program is already running.  ie.  I have a button with the OnClick
event handler containing Application.CreateForm(TForm1, Form1).  If the
user hits this button multiple times, I get multiple instances of Form1,
whereas I would just like it to come to the front if it has already been
created...

TIA,
Tom.

--
Tom MacAdam (lmsv0...@linden.msvu.ca)

 

Re:Preventing Multiple Instances of a FORM (NOT whole app)


Simple soultion...  Just disable your button after you click it and reenable
it when the form closes.

In article <slrn5sk548.8h6e.LMSV0...@LINDEN.MSVU.CA>, lmsv0...@linden.msvu.ca
wrote:

Quote
>Hi. How do I prevent multiple instances of a form from being created after
>my program is already running.  ie.  I have a button with the OnClick
>event handler containing Application.CreateForm(TForm1, Form1).  If the
>user hits this button multiple times, I get multiple instances of Form1,
>whereas I would just like it to come to the front if it has already been
>created...

>TIA,
>Tom.

Re:Preventing Multiple Instances of a FORM (NOT whole app)


On 14 Jul 1997 LMSV0...@LINDEN.MSVU.CA (Tom Macadam) wrote:

Quote
> Hi. How do I prevent multiple instances of a form from being created after
> my program is already running.  ie.  I have a button with the OnClick
> event handler containing Application.CreateForm(TForm1, Form1).  If the
> user hits this button multiple times, I get multiple instances of Form1,
> whereas I would just like it to come to the front if it has already been
> created...

That's easy:

procedure TMainForm.MyButtonClick(Sender: TObject);
begin
  if not assigned(Form1) then
    Form1 := TForm1.Create(self);
  Form1.BringToFront;
end;

TForm1.Create(self) is recommended over Application.CreateForm(TForm1,
Form1) outside the project file, since it allows your main form to close
the Form1 legally if it needs to.

Happy coding,
--
Eugene I Levin
Bielefeld, Germany

I'm sorry for corrupting my Email in the header.
Here is the right one:
L e v i n @ l h . b i c o s . d e

Other Threads