动态控件的删除,急!!!
我编了一段程序,当鼠标单击时,在单击的地方动态创建一个EDIT,然后输完字符后回车或者这个控件失去焦点清时,在EDIT的位置又动态生成LABEL控件,而且删除掉刚才动态生成的EDIT控件,可为什么下面这段代码在,,,在动态生成的EDIT里面回车就会报错,说什么地址越界,可当它失去焦点时却又没有错了。哪位大哥帮帮忙啊,代码如下
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::PaintBox1MouseDown(TObject *Sender,
TMouseButton Button, TShiftState Shift, int X, int Y)
{
TEdit *newTempStr;
newTempStr=new TEdit(this);
newTempStr->Parent=Form1;
newTempStr->Left=X+this->PaintBox1->Left;
newTempStr->Top=Y+this->PaintBox1->Top;
newTempStr->Font->Size=25;
newTempStr->AutoSize=true;
newTempStr->BorderStyle=bsNone;
newTempStr->SetFocus();
newTempStr->OnKeyPress=TempEditKeyPress;
newTempStr->OnExit=TempEditLoseFocus;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::TempEditLoseFocus(TObject *Sender)
{
TEdit *newEdit;
newEdit=(TEdit *)Sender;
if(newEdit->Text.IsEmpty())
{
delete newEdit;
newEdit=NULL;
return;
}
TLabel *newLabStr;
newLabStr=new TLabel(this);
newLabStr->Parent=Form1;
newLabStr->Left=newEdit->Left;
newLabStr->Top=newEdit->Top;
newLabStr->Font->Size=newEdit->Font->Size;
newLabStr->AutoSize=true;
newLabStr->Transparent=true;
newLabStr->Caption=newEdit->Text;
delete newEdit;
newEdit=NULL;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::TempEditKeyPress(TObject *Sender, char &Key)
{
if(Key=='\r')
{
TempEditLoseFocus(Sender);
}
}