Showing a modal form at program startup

This is the forum for miscellaneous technical/programming questions.

Moderator: 2ffat

Showing a modal form at program startup

Postby pedand » Thu Sep 30, 2004 9:00 am

What is the recommended way to show a modal form when a program starts? ShowModal->TForm2 can not be used in the constructor or in FormCreate for TForm1 but I guess this can be done in the project.cpp file (in WinMain just as for splash windows). Surely there must be a better way to collect user data for things like validating the user's permissions for an application etc.
Regards
Anders
pedand
 

Two ways I've done it.

Postby 2ffat » Wed Oct 06, 2004 6:28 am

I've used two different ways to show forms at startup. The first comes from Borland C++ Builder How-To: The Definitive C++ Builder Problem-Solver by John Miano, Tom Cabanski, and Harold Howe. They show some code for making a form, in this case a splash screen, at startup. They put their code in the Form1.cpp. This is the C++ source created by Builder automatically. You usually don't edit this code but they recommend doing something like:
Code: Select all
WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
    try
    {
         Application->Initialize();
         Form2 = new TForm2(Application); // You add this
         Form2->Show(); // You add this
         Form2->Update(); // You add this
         Application->CreateForm(__classid(TForm1), &Form1);
         Application->Run();
    }
    catch (Exception &exception)
    {
         Application->ShowException(&exception);
    }
    return 0;
}


I prefer to do the following in my form's initialzation function. This is the first function in the C++ source code that you usually edit.
Code: Select all
__fastcall MyForm::TMyForm(TComponent* Owner)
    : TForm(Owner)
{
   
    // The following section of code is what you add
    TForm2 *My2Form = new TForm2(Application);
    try
    {
        My2Form->Show();
    }
    __finally
    {
        delete My2Form;
    }
    // The above section of code is what you add

}
//---------------------------------------------------------------------------


I prefer the second mainly because the __finally code will delete the form whether there is a problem or not thus freeing memory. I suppose you do the same in the previous example.

This is probably as clear as mud but it works for me.
James P. Cottingham

Look at me still talking
when there is science to do.
User avatar
2ffat
Forum Mod
Forum Mod
 
Posts: 363
Joined: Wed Jun 23, 2004 7:07 am
Location: South Hill, VA

Thanks

Postby Guest » Thu Oct 07, 2004 8:38 am

Hi James,

I've used the technique in your first example before and it works fine but I don't like it. It seems, at least om my pc, that you should not update the Project|Options|Forms tab if you update the project.cpp file this way since the code behind the tab can change (=overwrite) the file contents. I once lost some work this way.

Your second example is much safer, more logical and clear as a mountain stream. Again thanks.
Anders
Guest
 


Return to Technical

Who is online

Users browsing this forum: Bing [Bot] and 2 guests