简体   繁体   中英

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();

Do this:

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

Then if frm is not null,

frm->Show();
TForm *frm = new TForm(this);

if( frm != NULL )
{
   frm->ShowModal();

   //or

   frm->Show();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM