Solved

Is a form already loaded?

Posted on 2001-08-22
3
248 Views
Last Modified: 2010-04-06
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
Comment
Question by:riccohb
3 Comments
 
LVL 44

Expert Comment

by:CrazyOne
ID: 6413509
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
 
LVL 6

Expert Comment

by:reddarin
ID: 6417140
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
 

Accepted Solution

by:
Bilce earned 50 total points
ID: 6439494
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question