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

303 Views Asked by At

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.

enter image description here

Error message after 2 clicks.

enter image description here

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

enter image description here

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

enter image description here

-

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.

1

There are 1 best solutions below

1
On

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.