Q&A

  • 동적으로 생성된 컴포넌트 삭제....
밑의 코딩에서

button1을 누르면 mybutton이 생성됩니다..

그리고 나서 button2를 누르면 mybutton이 삭제됩니다.

그런데 만약 button1을 연속해서 두번 누른후

button2를 누르면 mybutton은 삭제되지 않고(메모리상에서는 삭제된것 같음)

그대로 남아 있습니다..

그리고 나서 mybutton을 누르면 에러가 발생합니다...

button1을 한번만 누른상태에서는 절대 에러가 발생하지 않습니다...

그런데 왜 2번이상 누르면 에러가 발생하는지....

아시는분 계시면 답변부탁드립니다...







interface



uses

Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,

StdCtrls;



type

TForm1 = class(TForm)

Button1: TButton;

Button2: TButton;

mybutton : tbutton;

procedure Button1Click(Sender: TObject);

procedure Button2Click(Sender: TObject);

private

procedure myclick(sender: tobject);

{ Private declarations }

public

{ Public declarations }

end;



var

Form1: TForm1;



implementation



{$R *.DFM}



procedure TForm1.Button1Click(Sender: TObject);

var



mypositon : integer;

begin

mybutton := tbutton.Create(nil);

mybutton.left := 300;

mybutton.top := 100;

mybutton.Height := 20;

mybutton.Width := 100;

mybutton.Parent := form1;

mybutton.OnClick := myclick;

mybutton.Caption := 'mybutton';



end;



procedure tform1.myclick(sender : tobject);

begin

showmessage(mybutton.name);

end;



procedure TForm1.Button2Click(Sender: TObject);

begin

if mybutton <> nil then



mybutton.Free;

mybutton := nil;



end;



end.

1  COMMENTS
  • Profile
    에벤에셀 2001.07.03 00:47
    아래 코딩대로 한 번 해보세요...

    도움이 될지 모르겠습니다. 잘 안되면 연락주세요. ^^;



    unit Unit1;



    interface



    uses

    Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,

    StdCtrls;



    type

    TForm1 = class(TForm)

    Button1: TButton;

    Button2: TButton;

    procedure FormCreate(Sender: TObject);

    procedure Button1Click(Sender: TObject);

    procedure Button2Click(Sender: TObject);

    private

    { Private declarations }

    MyButton: TButton;

    ButtonCnt: integer;

    procedure myclick(sender: tobject);

    public

    { Public declarations }

    end;



    var

    Form1: TForm1;



    implementation



    {$R *.DFM}



    procedure TForm1.FormCreate(Sender: TObject);

    begin

    ButtonCnt := 0;

    end;



    procedure TForm1.Button1Click(Sender: TObject);

    begin

    inc(ButtonCnt);



    MyButton := Tbutton.Create(Self);



    with MyButton do begin

    Left := 300;

    Top := 100 + (ButtonCnt * 20);

    Height := 20;

    Width := 100;

    Caption := 'MyButton' + IntToStr(ButtonCnt);

    Name := 'MyButton' + IntToStr(ButtonCnt);

    Parent := Form1;

    OnClick := MyClick;

    end;

    end;



    procedure TForm1.MyClick(sender : tobject);

    begin

    ShowMessage(TButton(Sender).Name);

    end;



    procedure TForm1.Button2Click(Sender: TObject);

    var

    I: Integer;

    Temp: TComponent;

    begin

    for I := ComponentCount - 1 downto 0 do begin

    Temp := Components[I];



    if (Temp is TButton) and (Copy(TButton(Temp).Name,1,8) = 'MyButton') then

    Temp.Free;

    end;



    ButtonCnt := 0;

    end;



    end.