c++builderc++builder-xe3

How to dynamically call a form in C++ Builder XE3?


I'm building an application in which Im populating menus using DB. I can create menu items but im having trouble linking "On Click" event to particular forms. I have stored names of the forms classes in my DB and trying to use RTTI to bind them at runtime. Following is the snippet of my code that Im trying to run.

__fastcall TfrmMainMDI::TfrmMainMDI(TComponent *Owner)
    : TForm(Owner)
{
    // Register 2 form classes
    RegisterClass(__classid(TfrmSecurity));
    RegisterClass(__classid(TfrmPassword)); 
} 

Now when I try to run following code to call the form it gives "Access violation" error.

    TForm *frm = (TForm*)TFormClass(FindClass(formName));
    UnicodeString str = frm->Name;
    frm->Show();

Solution

  • Do this:

    TForm *frm = 0;
    Application->CreateForm( TFormClass(FindClass(formName)), &frm );
    

    Then if frm is not null,

    frm->Show();