Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Closing a form in Forms' Activate Event

Posted on 1998-07-25
9
Medium Priority
?
192 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Independent Software Vendors: 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

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

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…
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…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

721 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