|
: choco-milford 님이 쓰신 글 :
: : 제 코드의 개념을 적어보죠.
: :
: : 1.Form1->Edit1에 정수 입력.
: :
: : 2.Form2에서 Edit DBEdit, Label 들을 동적 생성.(FormActivate 이벤트에서)
: :
: : 문제점 : 첨 생성은 되는데 런타임중에 Form1->Edit1 수정하고 Form을 다시 열면(활성화)
: : 이전에 생성됐던 Edit DBEdit, Label 들과 이름이 충동합니다.
: : 당연 기존 것들의 이름을 동적 생성하였으므로...
: :
: : 창을 닫아도 일단 값들은 유지해야 하므로 바로 Form2가 닫힐 때
: : 이전 생성된 컴포넌트들을 삭제할 수는 없습니다.
: :
: : 따라서 Form1->Edit1을 수정하고 버튼을 눌러 Form2->Show()하면
: : 이전에 생성되었던 것들을 삭제하고 다시 생성시켜야 할 것 같은데...
: : 잘 모르겠네요.
: :
: :
: : void __fastcall TForm2::FormActivate(TObject *Sender)
: : {
: : int Count;
: : Count = StrToInt(Form1->Edit->Text);
: :
: : for (int i=1; i<=Count; i++)
: : {
: : /*Edit 생성*/
: : TEdit *edit1;
: : edit1 = new TEdit(this);
: : edit1->Parent = this;
: : edit1->Left = 38;
: : edit1->Top = 24*i;
: : edit1->Name = "EdtSession" + String(i);
: : }
: : }
: :
: : Edit 생성전에 삭제하는 방법을 좀 알려주세요....전 너무 초보라는 사실을 감안하시고 ㅜㅜ
: :
skjoon 님의 답변 감사드립니다.
하지만 궁극적으로 제 플그램에서 폼을 닫아도
동적생성된 Edit에 입력된 값들을 다른 폼에서 계산에 이용되야 된다는 문제가 있어서..
기초 자료 입력을 하는 과정인 이 부분에서는 내부적으론 번거롭더라도
동적 생성과 삭제를 하도록 하려고 했습니다.
일단 해결은 했구요^^
해결은 Tip'N Tricks 14 / 팁! 동적생성된 컴퍼넌트를 TList에서 삭제... (박지훈.임프 님)와
Q&A 24010 / Re:[질문]edit box 동적으로 생성하기 (임문환 님)의 글을 참고하였습니다.
뭐낙 제가 초보인 관계로... 너무 잡다하게 올리는 건 아닌지...^^
=========
unit1.h
=========
public:
int PreNumber;
int Number;
=========
unit1.cpp
=========
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
#include "Unit2.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
PreNumber = 0;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btnForm2Click(TObject *Sender)
{
Number = StrToInt(Edit1->Text);
if( PreNumber > 0 )
{
while(Form2->MyEditList->Count)
{
delete (TEdit *)(Form2->MyEditList->Items[0]);
Form2->MyEditList->Delete(0);
}
}
Form2->Show();
}
//---------------------------------------------------------------------------
=========
unit2.h
=========
public:
TList *MyEditList;
=========
unit2.cpp
=========
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
#include "Unit2.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm2 *Form2;
//---------------------------------------------------------------------------
__fastcall TForm2::TForm2(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm2::FormActivate(TObject *Sender)
{
MyEditList = new TList;
for(int i=0; i<(Form1->Number); i++)
{
edit1 = new TEdit(this);
edit1->Parent = this;
edit1->Left = 38;
edit1->Top = 24*i;
MyEditList->Add(edit1);
Form1->PreNumber = i+1;
}
}
|