Solved

Is a form already loaded?

Posted on 2001-08-22
3
244 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

746 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now