如何销毁动态生成的控件
有一个 combobox,作为 控件的容器
用 下边的代码生成很多label
for(int j=0;j<MaxNum;j++) { TLabel *tl = new TLabel(Application); tl->Left = i*100+100; tl->Top = j*70+50; tl->Caption = "测试label"; tl->AutoSize = true; tl->Enabled = true; tl->Parent = gbox1; labelNum++; }for(int j=0;j<gbox1->ControlCount;j++) { if(gbox1->Controls[j]->ClassType() == __classid(TLabel)) { gbox1->Controls[j]->Parent = NULL; gbox1->Controls[j]->Visible = false; free(gbox1->Controls[j]); } }for(int j=0;j<gbox1->ControlCount;j++) { delete gbox1->Controls[j]; }
[解决办法]
动态创建了多少个,就用多少个delete来释放,这有什么不解呢?