[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 256
  • Last Modified:

Is a form already loaded?

Is there a way of telling whether or not a form has already been loaded?

I'm writing an app with various forms that the user can open, but I only want the user to be able to open each form once. If it's already open, I just want to bring it to the front instead of loading another copy of it.

Cheers
0
riccohb
Asked:
riccohb
1 Solution
 
CrazyOneCommented:
if Assigned(TheForm) then
  TheForm.Show
else begin
  TheForm := TForm.Create(nil);
  TheForm.Show;
end;

make sure you free the form before you the app terminates.


The Crazy One
0
 
reddarinCommented:
Something that kretzschmar pointed out in a similar question:

Assigned(object) checks for nil. So, when you free a form that the user closes, you'll need to make sure you set it to nil since .free does not set it to nil. He recommended using FreeAndNil(object).

reddarin
0
 
BilceCommented:
Here's a sample program for your problem.

var
  MyHandle : THandle;
  str   : string;
begin
  str :=TMainForm.ClassName;
  MyHandle :=FindWindow(PChar(str),nil);
  if MyHandle <> 0  then
  begin
    ShowMessage('Cannot run this program cause having same application instance');
  end
  else
  begin
    Application.Initialize;
    Application.CreateForm(TMainForm, MainForm);
    Application.Run;
  end;
end.

procedure TMainForm.Button1Click(Sender: TObject);
var str : string;
begin
  str := TMainForm.ClassName;
  ShowMessage('This is the handle for this window : ' + IntToStr(FindWindow(PChar(str),nil)));
end;

For the complete source code and it's example you can email me first and i'll send you the file ...
hope this can help ..

regards...
Billy Lapono
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now