Solved

Closing a form in Forms' Activate Event

Posted on 1998-07-25
9
182 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
 
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

762 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

20 Experts available now in Live!

Get 1:1 Help Now