简体   繁体   中英

Error when dynamically creating more than one Frame at runtime (C++, FMX, IDE: C++ Builder)

I would like to dynamically create a series of Frame components, then save the pointers into a vector.

I am able to dynamically create TEdit components with no problem, but if I replace TEdit with the name of the frame component (that contains an edit box) the program will error. The first frame will be created, but errors when creating the second one, stating "External exception EEFFACE"

Here is the relevant code. Note that if I replace TFrame2 with TEdit, it works.

class TForm1 : public TForm
{
...
public:     // User declarations
    std::vector<TFrame2*> fields;
...
};

void __fastcall TForm1::Button1Click(TObject *Sender)
{
    TFrame2* temp = new TFrame2 (Layout1);
    temp->Parent = Layout1;
    temp->Align = TAlignLayout::Top;
    fields.push_back(temp);
    count++;
}

This is what it looks like after one click.

在此处输入图片说明

Error message after 2 clicks.

在此处输入图片说明

This is how I want it to look after two clicks.

在此处输入图片说明

This is what it looks like when I replace TFrame2 with TEdit, after 3 clicks.

在此处输入图片说明

-

EDIT

If I try to hard-code the creation of two Frames, I get the same error on the first click.

void __fastcall TForm1::Button1Click(TObject *Sender)
{
    TFrame2* temp = new TFrame2 (Layout1);
    temp->Parent = Layout1;
    temp->Align = TAlignLayout::Top;
    fields.push_back(temp);
    count++;

    TFrame2* temp1 = new TFrame2 (Layout1);
    temp1->Parent = Layout1;
    temp1->Align = TAlignLayout::Top;
    fields.push_back(temp1);
    count++;

}

-

EDIT 2

In this post

Can FireMonkey frames be created dynamically?

I see a comment that states

I should note here that it seems the frame objects need to be assigned a unique Name property manually right after creation, at least when using C++, or the next frame object created of the same type will try to take the same name as the first one.

To fix this issue, I needed to set the name of the Frame at runtime. Adding the following code in the Button1 click method fixes the issue.

temp->Name = std::strcat("TFrame2", std::to_string(count).c_str());

This names every new Frame "TFrame2#" where # is the number frame that has been created.

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