Board index » cppbuilder » What is the best way to exit from within the main form constructor

What is the best way to exit from within the main form constructor


2004-05-31 11:21:29 PM
cppbuilder54
Hello
What is the best way to exit the application if something is detected
during the constructor of the main form (i.e. user is not authorized
or something like that).
What would be the repercussions of using exit?
Ciao
Bill
 
 

Re:What is the best way to exit from within the main form constructor

"William Charles Nickerson" < XXXX@XXXXX.COM >wrote:
Quote
What is the best way to exit the application if something is
detected during the constructor of the main form (i.e. user
is not authorized or something like that.
It depends on how you code. I would set a flag and then in the
OnActivate event, check the flag.
void __fastcall TForm1::Activate(TObject *Sender)
{
if( flag )
{
MessageDlg( "No Way Jose!!", mtError, TMsgDlgButtons() << mbOK, 0 );
Close();
}
}
The result is that the user gets some visual feedback instead
of launching the application and nothing (visually) happening.
~ JD
 

Re:What is the best way to exit from within the main form constructor

Thanks JD
"JD" < XXXX@XXXXX.COM >wrote in message
Quote
It depends on how you code. I would set a flag and then in the
OnActivate event, check the flag.

Why would you wait until the OnActivate event? Is there a problem with
terminating
immediately? Can you call Close () within the constructor?
Quote
void __fastcall TForm1::Activate(TObject *Sender)
{
if( flag )
{
MessageDlg( "No Way Jose!!", mtError, TMsgDlgButtons() << mbOK,
0 );
Close();
}
}

The result is that the user gets some visual feedback instead
of launching the application and nothing (visually) happening.
There is visual feedback in the authorization dialog class so there is no
need for
further messages at this point.
Ciao
Bill
 

{smallsort}

Re:What is the best way to exit from within the main form constructor

"William Charles Nickerson" < XXXX@XXXXX.COM >wrote:
Quote
Why would you wait until the OnActivate event?
It gives me the chance to tell the user something and it
ensures that the form has finished the code in the constructor.
Quote
Is there a problem with terminating immediately?
There could be depending on what your code is doing and how
you terminate.
Quote
Can you call Close () within the constructor?
Yes. However, I would code it some thing like:
if( every thing checks ok )
{
// finish constructing
}
else
{
Close();
}
~ JD
 

Re:What is the best way to exit from within the main form constructor

Thanks JD
I appreciate the insight and info. I plan to follow your suggestion to move
it to the OnActivate event.
Bill
 

Re:What is the best way to exit from within the main form constructor

"William Charles Nickerson" < XXXX@XXXXX.COM >wrote in message
Quote
What is the best way to exit the application if something is
detected during the constructor of the main form (i.e. user
is not authorized or something like that).
Either 1) throw an exception, or 2) just call Application->Terminate().
Quote
What would be the repercussions of using exit?
Never call exit() from a VCL project. It does not allow the VCL to clean up
after itself properly.
Gambit
 

Re:What is the best way to exit from within the main form constructor

"JD" < XXXX@XXXXX.COM >wrote in message
Quote
It depends on how you code. I would set a flag and then
in the OnActivate event, check the flag.
<snip>
The result is that the user gets some visual feedback
instead of launching the application and nothing (visually)
happening.
You can do all of that from the constructor directly:
__fastcall TForm1::TForm1(TComponent *Owner)
{
if( some condition )
{
::MessageBox(NULL, "No Way Jose!!", "ERROR!", MB_OK);
Application->ShowMainForm = false;
Application->Terminate();
}
}
Even better would be if you do your validation inside of WinMain() directly
before the VCL even begins creating forms at all.
Gambit
Gambit
 

Re:What is the best way to exit from within the main form constructor

Thanks Remy
That is the information that I need.
Ciao
Bill
"Remy Lebeau (TeamB)" < XXXX@XXXXX.COM >wrote in message
Quote

"William Charles Nickerson" < XXXX@XXXXX.COM >wrote in message
news:40bb4d79$ XXXX@XXXXX.COM ...

>What is the best way to exit the application if something is
>detected during the constructor of the main form (i.e. user
>is not authorized or something like that).

Either 1) throw an exception, or 2) just call Application->Terminate().

>What would be the repercussions of using exit?

Never call exit() from a VCL project. It does not allow the VCL to clean
up
after itself properly.


Gambit


 

Re:What is the best way to exit from within the main form constructor

"William Charles Nickerson" < XXXX@XXXXX.COM >wrote in message news:40bb4d79$ XXXX@XXXXX.COM ...
Quote
What is the best way to exit the application if something is detected
during the constructor of the main form (i.e. user is not authorized
or something like that).
Do not use exit. In fact, I have had problems using anything except for the FormPaint event (the first time it
is called for the main form). The problem with Application->Terminate is that it bypasses the normal OnClose
and OnCloseQuery events, so I use the following method (invented by me and free for general use - cheeky). Put
a timer on your form with an interval of 300 ms and enabled as false. Then :-
//---------------------------------------------------------------------------
void __fastcall TForm1::FormPaint(TObject *Sender)
{
static bool ever_called=false;
if (ever_called) return;
ever_called=true;
if (the conditions that require the program to close are met) { Timer1->Enabled=true; return;}
// ... any other init stuff can go here
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
Timer1->Enabled=false; Close();
}
//---------------------------------------------------------------------------
HTH
--
Mark Jacobs
DK Computing
www.dkcomputing.co.uk
markj atty critical dotty co dotty uk