Solved

Closing a form in Forms' Activate Event

Posted on 1998-07-25
9
185 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
ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Virtuailstring tree compare node issue 14 116
Printing problem 2 93
Reconfigure Delphi Install? 2 51
Twebbrowser in Fire monkey android scrolling to bottom 1 30
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

770 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