|
void __fastcall TForm1::Button1Click(TObject *Sender)
{
TForm2 *Form2; //잘 못 선언된 부분(지역변수로 선언된 부분)
Form2=new TForm2(Application);
Form2->Show(); //위의 선언이 맞다면 이분은 ShowModal로 처리하구 작업이 끝나면 Free시켜줘야함.
}
void __fastcall TForm2::Button1Click(TObject *Sender)
{
Form1->Edit1->Text=Edit1->Text;
}
위의 소소를 보게 되면 우선 From2를 선언할 때 지역변수로 선언했습니다.
지역변수로 선언하게 되면 Button1Click함수를 벋어나게 되면 제어권이 자연소멸
됩니다. (함수 외부에서는 사용이 불가)
그런데... 아래부분을 보면..
From2->ShowModal();이 아니라 Show()를 사용했습니다....
결과적으로 폼을 보여주고 포커스를 보내구 난 후 대기하는 것이 아니라
함수를 이탈하게 됩니다.
작업을 하기 전에 생성을 시켰는지 확인하고 작업하면 문제가 없을거 같네요...
//--------------------------------------------------
// ShowModal()을 사용할 경우
//--------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
TForm2 *Form2; //지역변수로 선언할 경우에는 선언된 지역안에서
//생성과 소멸을 책임져야 합니다.
if( (Form2=new TForm2(Application)) == NULL ) return;
try
{
Form2->ShowModal();
}__finally
{
delete Form2;
Form2 = NULL;
}
}
void __fastcall TForm2::Button1Click(TObject *Sender)
{
Form1->Edit1->Text=Edit1->Text;
}
//--------------------------------------------------
// Show()를 사용할 경우
//--------------------------------------------------
TForm2 *Form2 = NULL;//전역으로 선언
void __fastcall TForm1::Button1Click(TObject *Sender)
{
if( Form2 == NULL )
if( (Form2=new TForm2(Application)) == NULL ) return;
Form2->Show();
}
void __fastcall TForm1::Button2Click(TObject *Sender)
{
if( Form2 != NULL )
{
delete = Form2;
Form2 = NULL;
}
}
void __fastcall TForm2::Button1Click(TObject *Sender)
{
Form1->Edit1->Text=Edit1->Text;
}
|