我想动态创建一系列Frame组件,然后将指针保存到 vector 中。
我能够动态创建TEdit组件,没有问题,但是如果我用框架组件的名称(包含编辑框)替换TEdit,则程序将出错。将创建第一个框架,但是在创建第二个框架时出错,并指出“外部异常EEFFACE”
这是相关的代码。请注意,如果我用TEdit替换TFrame2,它将起作用。
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++;
}
一键显示就是这样。
单击2次后出现错误消息。
这就是我希望它在单击两次后的外观。
这是当我单击3次后用TEdit替换TFrame2时的样子。
--
编辑
如果我尝试对两个框架的创建进行硬编码,则在第一次单击时会遇到相同的错误。
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++;
}
--
编辑2
在这篇文章中
Can FireMonkey frames be created dynamically?
我看到一条评论指出
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.
最佳答案
要解决此问题,我需要在运行时设置框架的名称。在Button1 click方法中添加以下代码可解决此问题。
temp->Name = std::strcat("TFrame2", std::to_string(count).c_str());
这将命名每个新框架“TFrame2#”,其中#是已创建的数字框架。
关于c++ - 在运行时动态创建多个框架时出错(C++,FMX,IDE:C++ Builder),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60855262/