?
Solved

Is a form already loaded?

Posted on 2001-08-22
3
Medium Priority
?
251 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 150 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses
Course of the Month14 days, 19 hours left to enroll

771 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