Solved

Closing a form in Forms' Activate Event

Posted on 1998-07-25
9
186 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 50 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

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!

Question has a verified solution.

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

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

756 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