Board index » delphi » Closing a form in OnShow

Closing a form in OnShow

This may seem like an obvious question, but what is the best way to close a
form in its OnShow event? Because the form has not been shown yet during
OnShow, calling .Close does nothing. TIA.

Bruce

 

Re:Closing a form in OnShow


Use
  PostMessage(Self.Handle, WM_CLOSE, 0, 0);
It will still show briefly and then close immediately.
It's better to decide if the form should be shown before calling Show, and
if not then don't call Show.

Finn Tolderlund

Bruce <nospamrdxjc8...@juno.com> skrev i en
nyhedsmeddelelse:81c3ek$5...@forums.borland.com...

Quote
> This may seem like an obvious question, but what is the best way to close
a
> form in its OnShow event? Because the form has not been shown yet during
> OnShow, calling .Close does nothing. TIA.

Re:Closing a form in OnShow


Quote
> This may seem like an obvious question, but what is the best way to close
a
> form in its OnShow event? Because the form has not been shown yet during
> OnShow, calling .Close does nothing. TIA.

 Hum... Are you sure ? There is another thing to do :  free the form itself.
You will have some trouble with OLE/ActiveX, which must be taken care
separately : free all the "onboard" component before freeing the form.
(Quite harsh, but effective ;-)

Be aware that the form will not process the close before the application has
gotten into the "process message" loop. That is before Application.Run. (The
form has no opportunuity to receive the message before that point).
 Perhaps this is what you really meant by your "not been shown yet" ?

--Richard.

Re:Closing a form in OnShow


Quote
Bruce wrote:

> This may seem like an obvious question, but what is the best way to close a
> form in its OnShow event? Because the form has not been shown yet during
> OnShow, calling .Close does nothing. TIA.

> Bruce

It would seem that your intention is to not open the form after some
code has been run that has decided that the form really shouldn't be
opened. A better way to do this (never show the form) is to override the
form's constructor...

constructor TForm.Create(AOwner : TComponent);
var lOpenForm : Boolean;
begin
  lOpenForm := true;
  {code to determine if form is to be opened. needs to set lOpenForm to
false if it shouldn't be opened.}
  if not lOpenForm then abort;
  inherited create(AOwner);
end;

by placing this code before the inherited call, you can abort the form
before ever starting the code to show it.

HTH,
Wayne
--
Bogus email address - use wgshef instead, keep the domain.

Re:Closing a form in OnShow


Thanx, the posted message method does what I want. Yeah, it is better to
know before calling Show, but sometimes it is not possible.
Bruce

Finn Tolderlund <XnospamYfinn.tolderlu...@Ymobilixnet.dkXnospamY> wrote in
message news:81c64n$5is8@forums.borland.com...

Quote
> Use
>   PostMessage(Self.Handle, WM_CLOSE, 0, 0);
> It will still show briefly and then close immediately.
> It's better to decide if the form should be shown before calling Show, and
> if not then don't call Show.

> Finn Tolderlund

> Bruce <nospamrdxjc8...@juno.com> skrev i en
> nyhedsmeddelelse:81c3ek$5...@forums.borland.com...
> > This may seem like an obvious question, but what is the best way to
close
> a
> > form in its OnShow event? Because the form has not been shown yet during
> > OnShow, calling .Close does nothing. TIA.

Re:Closing a form in OnShow


Of course it is possible!
What are you doing in the OnShow event?
Running some code to determine if the form should be shown, right?
That code could be executed anywhere you like *before* calling Show.

Finn Tolderlund

Bruce <nospamrdxjc8...@juno.com> skrev i en
nyhedsmeddelelse:81cngr$c...@forums.borland.com...

Quote
> Thanx, the posted message method does what I want. Yeah, it is better to
> know before calling Show, but sometimes it is not possible.
> Bruce

> Finn Tolderlund <XnospamYfinn.tolderlu...@Ymobilixnet.dkXnospamY> wrote in
> message news:81c64n$5is8@forums.borland.com...
> > Use
> >   PostMessage(Self.Handle, WM_CLOSE, 0, 0);

Other Threads