Board index » cppbuilder » How to close Form in OnShow ?

How to close Form in OnShow ?

Hello everybody,
can someone please give me a method to close a form in the OnShow event,
and the possibility to run after this in the OnClose or OnDestroy or
deconstuctor method. I tried it with cause an exeception but it didn't
work because it skiped the OnClose, OnDestroy and deconstuctor methods.

Thank you
J?rg Gaide

 

Re:How to close Form in OnShow ?


Quote
> can someone please give me a method to close a form in the OnShow event,

Close() ?

I take it you've tried this and you are still having problems.  Can you give a
sample which illustrates the problem. and your compiler version? ( BCB 1 handles
this a bit differently )

For example the following code works:

void __fastcall TForm1::FormShow(TObject *Sender)
{
     Close();

Quote
}

//---------------------------------------------------------------------------

void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
     ShowMessage( "Closing" );

Quote
}

//---------------------------------------------------------------------------

The Form never shows up and all you get to see is a message saying "Closing".

- Tolga
Systems Administrator
Post Time Research
-----------------------------
(   ICQ# 28264566   )

Quote
"Joerg Gaide" <joerg.ga...@metronet.de> wrote in message

news:38E45EF6.F4A89BD1@metronet.de...
<snip>

Re:How to close Form in OnShow ?


Hello Tolga,
your tip works if my form is the main form of the application, but it
failed when a second form is opened from the main form. The second form
is shown and the Close()-command in FormShow() has no effect. The
FormClose() method isn't called. Perhaps you have a solution of this
problem ?
Use your code in the second form and call Form2->ShowModal() from the
first Form and you will see the problem. I'm using C++Builder 5.
Thank you Tolga.

Tolga Balci schrieb:

Quote

> > can someone please give me a method to close a form in the OnShow event,
> Close() ?

> I take it you've tried this and you are still having problems.  Can you give a
> sample which illustrates the problem. and your compiler version? ( BCB 1 handles
> this a bit differently )

> For example the following code works:

> void __fastcall TForm1::FormShow(TObject *Sender)
> {
>      Close();
> }
> //---------------------------------------------------------------------------

> void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
> {
>      ShowMessage( "Closing" );
> }
> //---------------------------------------------------------------------------

> The Form never shows up and all you get to see is a message saying "Closing".

> - Tolga
> Systems Administrator
> Post Time Research
> -----------------------------
> (   ICQ# 28264566   )

> "Joerg Gaide" <joerg.ga...@metronet.de> wrote in message
> news:38E45EF6.F4A89BD1@metronet.de...
> <snip>

Re:How to close Form in OnShow ?


You could do a PostMessage to send a WM_QUIT message to the application.
This should convince the application to shut down as soon as possible.

Quote
J?rg Gaide <joerg.ga...@metronet.de> wrote in message

news:38E7620B.B2FA150A@metronet.de...
<snip>

Re:How to close Form in OnShow ?


Hello J?rg!

Now I see what you mean ! :O)

Quote
>Perhaps you have a solution of this problem ?

Well, yeah :O)

Instead capturing the OnShow event override the Show method instead:

class TForm2 : public TForm
{
__published: // IDE-managed Components
 void __fastcall FormClose(TObject *Sender, TCloseAction &Action);
private: // User declarations
public:  // User declarations
     void __fastcall Show( void ); // <------------------
 __fastcall TForm2(TComponent* Owner);

Quote
};

void __fastcall TForm2::Show( void )
{
    if( Whatever ) TForm::Show();
Quote
}

//---------------------------------------------------------------------------

This way you can control if your form actually shows as requested or not. ( Call
TForm::Show() if you want for the form to show )

But ofcourse since the form never shows in the above method you won't recieve a
OnClose event.
So you can do one of two things:
Actually first show the form than Close it if you need to ( which will trigger
the OnClose event ) or simply call the OnClose event your self.

void __fastcall TForm2::Show( void )
// Have the form show and close which will trigger the OnClose event
{
    TForm::Show();
    if( Whatever ) Close();

Quote
}

//---------------------------------------------------------------------------

or

void __fastcall TForm2::Show( void )
// Trigger the OnClose event your self
{
    if( Whatever ) TForm::Show();
    else
    {
        TCloseAction Fake;
        FormClose( this, Fake );
     }

Quote
}

//---------------------------------------------------------------------------

I know this is not exactly what you were asking for, but hope it still helps.

- Tolga
Systems Administrator
Post Time Research
-----------------------------
(   ICQ# 28264566   )

Quote
"J?rg Gaide" <joerg.ga...@metronet.de> wrote in message

news:38E7620B.B2FA150A@metronet.de...
<snip>

Re:How to close Form in OnShow ?


Hello Tolga,
i'am sorry for late response, i was away on business. Your tip works
great.
Thank you.
J?rg

Tolga Balci schrieb:

Quote

> Hello J?rg!

> Now I see what you mean ! :O)

> >Perhaps you have a solution of this problem ?

> Well, yeah :O)

> Instead capturing the OnShow event override the Show method instead:

> class TForm2 : public TForm
> {
> __published: // IDE-managed Components
>  void __fastcall FormClose(TObject *Sender, TCloseAction &Action);
> private: // User declarations

> public:  // User declarations
>      void __fastcall Show( void ); // <------------------
>  __fastcall TForm2(TComponent* Owner);
> };

> void __fastcall TForm2::Show( void )
> {
>     if( Whatever ) TForm::Show();
> }
> //---------------------------------------------------------------------------

> This way you can control if your form actually shows as requested or not. ( Call
> TForm::Show() if you want for the form to show )

> But ofcourse since the form never shows in the above method you won't recieve a
> OnClose event.
> So you can do one of two things:
> Actually first show the form than Close it if you need to ( which will trigger
> the OnClose event ) or simply call the OnClose event your self.

> void __fastcall TForm2::Show( void )
> // Have the form show and close which will trigger the OnClose event
> {
>     TForm::Show();
>     if( Whatever ) Close();
> }
> //---------------------------------------------------------------------------

> or

> void __fastcall TForm2::Show( void )
> // Trigger the OnClose event your self
> {
>     if( Whatever ) TForm::Show();
>     else
>     {
>         TCloseAction Fake;
>         FormClose( this, Fake );
>      }
> }
> //---------------------------------------------------------------------------

> I know this is not exactly what you were asking for, but hope it still helps.

> - Tolga
> Systems Administrator
> Post Time Research
> -----------------------------
> (   ICQ# 28264566   )
> "J?rg Gaide" <joerg.ga...@metronet.de> wrote in message
> news:38E7620B.B2FA150A@metronet.de...
> <snip>

Other Threads