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();
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top