Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Closing a form in Forms' Activate Event

Posted on 1998-07-25
9
Medium Priority
?
193 Views
Last Modified: 2010-04-04
Procedure TExtDetGenForm.FormActivate(Sender: TObject);
Begin

If NOT FirstTime Then Close;

Try
..
..
Finally
..
End;

END;

When I try the above code it does not work. The form just stays there.
Can somebody help me please. Thank You
0
Comment
Question by:rickytong
9 Comments
 
LVL 4

Expert Comment

by:erajoj
ID: 1358684
Is this what you need?:

procedure TExtDetGenForm.FormCreate(Sender: TObject);
begin
  Application.OnActivate := FormActivate;
end;

/// John
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1358685
As John already mentioned you forgot to assign the OnActive procedure, and the code he gave you above should work in order to get to work your procedures... : )    C'Ya...

Regards,
Viktor Ivanov
0
 
LVL 6

Expert Comment

by:Holger101497
ID: 1358686
I think you two misunderstood the question - it's the FORM's onActivate that should close the form and I assume that it's assigned.

I've run into the same problem before and didn't come up with a really decent solution either. Strange behaviour. I guess you can't close a form before it's opened. Maybe the Close sets some flag that is reset later in the process of opening it (which isn't aborted by the Close)?
My "workaround" looked like this:

TForm1.CloseMe(Sender:TObject); BEGIN Timer1Enabled:=false;Close; END;

TForm1.FormActivate(Sender: TObject);
  BEGIN
    IF (IwanttoCloseYou) THEN BEGIN
      Timer1.OnTimer:=CloseMe; //timer1.Interval:=100; {closing in 1/10 of a second)}
      Timer1.Enabled:=true;
    END;
  END;

well, that's probably not the best solution and that's why it's a comment instead of an answer, but I hope it helps you :-))
0
Technology Partners: 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!

 
LVL 3

Expert Comment

by:Matvey
ID: 1358687
Well, closing a form when it gets activeted shouldn't be a good idea.
A guess: if you want to stop your application from activating some instances of itself, than there are simpler ways to do it. You can find tons of components.
If you want to close it before it shows, you better not create it in the first place or not show it if possible. You can put some condition into the project source (.DPR), or before you show your form.
0
 
LVL 5

Expert Comment

by:JimBob091197
ID: 1358688
Just one more comment, similar to Holger's.
Another way to close a form on OnActivate, but without using a timer is to post a message to the form.

E.g.
const
  WM_CLOSE_THE_FORM = WM_USER + 1234;

procedure TMyForm.OnActivate(Sender: TObject);
begin
  PostMessage(Self.Handle, WM_CLOSE_THE_FORM, 0, 0);
end;

Then add the WndProc to the form:
type
  TMyForm = class(TForm)
  public
    procedure WndProc(var Msg: TMessage); override;
  end;

procedure TMyForm.WndProc(var Msg: TMessage);
begin
  inherited;
  case Msg.Msg of
    WM_CLOSE_THE_FORM: begin
      Application.ProcessMessages;  // Get rid of all other msgs.
      Close;  // Close the form.
    end;
  end;
end;

Cheeers,
JB
0
 

Expert Comment

by:donaldw
ID: 1358689
Ricky,

Is your  flag variable 'FirstTime' intended to prevent the form from being opened more than one time?  If so, I have a much more workable approach. If not, please let us know what you're trying to accomplish.

>> Procedure TExtDetGenForm.FormActivate(Sender: TObject);
>>Begin
>> If NOT FirstTime Then Close;

--- Don
0
 

Accepted Solution

by:
buboi earned 100 total points
ID: 1358690
FormActivate is occured when you focus the other form of this project. If you focus the other application, the Application.OnActivate event would be triggered.

please pay attention to difference of these two events.

0
 
LVL 4

Expert Comment

by:erajoj
ID: 1358691
I hate to say; I told you so the first time...

/// John
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1358692
Yup John is right! He said exactly the same thing the first time, and it was with an example too
Talk to y'all later...
 
Regards,
Viktor Ivanov
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

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…
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…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…

972 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