动态创建N个Panel,在每个Panel上动态创建一个Label,当点击某个Label是就释放倒当前的Label和Panel,按我的写法有错误,请高手指教
代码如下:unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button2Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
i: Integer;
Spanel: TPanel;
Slabel: TLabel;
procedure Spaneldelete(Sender: TObject);
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);
begin
i := 0;
end;procedure TForm1.Button1Click(Sender: TObject);
begin
Spanel := Tpanel.Create(Self);
Spanel.Parent := Form1;
Spanel.Visible := True;
Spanel.Name := 'Panel_'+IntToStr(i);
Spanel.Caption := 'Panel_'+IntToStr(i);
Spanel.Align := albottom;
Spanel.Align := altop; SLabel := TLabel.Create(Self);
SLabel.Parent := TPanel(FindComponent('Panel_'+IntToStr(i)));
SLabel.Align := altop;
SLabel.Visible := True;
SLabel.Cursor := crHandPoint;
SLabel.Name := 'Label_'+IntToStr(i);
SLabel.Caption := 'Label_'+IntToStr(i);
SLabel.OnClick := Spaneldelete; i := i + 1;
end;procedure TForm1.Spaneldelete(Sender: TObject);
begin
(Sender as TLabel).Parent.Free;
end;end;
代码如下:unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button2Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
i: Integer;
Spanel: TPanel;
Slabel: TLabel;
procedure Spaneldelete(Sender: TObject);
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);
begin
i := 0;
end;procedure TForm1.Button1Click(Sender: TObject);
begin
Spanel := Tpanel.Create(Self);
Spanel.Parent := Form1;
Spanel.Visible := True;
Spanel.Name := 'Panel_'+IntToStr(i);
Spanel.Caption := 'Panel_'+IntToStr(i);
Spanel.Align := albottom;
Spanel.Align := altop; SLabel := TLabel.Create(Self);
SLabel.Parent := TPanel(FindComponent('Panel_'+IntToStr(i)));
SLabel.Align := altop;
SLabel.Visible := True;
SLabel.Cursor := crHandPoint;
SLabel.Name := 'Label_'+IntToStr(i);
SLabel.Caption := 'Label_'+IntToStr(i);
SLabel.OnClick := Spaneldelete; i := i + 1;
end;procedure TForm1.Spaneldelete(Sender: TObject);
begin
(Sender as TLabel).Parent.Free;
end;end;
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, DateUtils, OleServer, WordXP, ExtCtrls;type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
I: integer;
procedure LabelClick(Sender: TObject);
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
var
SPanel: TPanel;
sLabel: TLabel;
begin
Spanel := Tpanel.Create(Self);
Spanel.Parent := Form1;
Spanel.Visible := True;
Spanel.Name := 'Panel_' + IntToStr(i);
Spanel.Caption := 'Panel_' + IntToStr(i);
Spanel.Align := albottom;
Spanel.Align := altop; SLabel := TLabel.Create(Self);
SLabel.Parent := Spanel;
SLabel.Align := altop;
SLabel.Visible := True;
SLabel.Cursor := crHandPoint;
SLabel.Name := 'Label_' + IntToStr(i);
SLabel.Caption := 'Label_' + IntToStr(i);
SLabel.OnClick := LabelClick; i := i + 1;
end;procedure TForm1.LabelClick(Sender: TObject);
begin
(Sender as TLabel).Parent.Free;
end;procedure TForm1.FormCreate(Sender: TObject);
begin
i := 0;
end;end.
看看下面的,没调试,请参考!
procedure TForm1.Button1Click(Sender: TObject);
begin
Spanel := Tpanel.Create(Self);
Spanel.Parent := Form1;
Spanel.Visible := True;
Spanel.Name := 'Panel_'+IntToStr(i);
Spanel.Caption := 'Panel_'+IntToStr(i);
Spanel.Align := albottom;
Spanel.Align := altop; SLabel := TLabel.Create(Spanel); //此外把Self 改为 Spanel
SLabel.Parent := TPanel(FindComponent('Panel_'+IntToStr(i)));
SLabel.Align := altop;
SLabel.Visible := True;
SLabel.Cursor := crHandPoint;
SLabel.Name := 'Label_'+IntToStr(i);
SLabel.Caption := 'Label_'+IntToStr(i);
SLabel.OnClick := Spaneldelete; i := i + 1;
end;
SLabel := TLabel.Create(Spanel); //此外把Self 改为 Spanel
这样写了也是不行的,这个问题的关键就是在于在Label的OnClick的事件中释放自己和自己的Parent
高手们帮帮忙啊,这外问题弄了好几天了,试了N种方法都不行
当释放Spanel时应该自动把SLabel释放,应该不需要单独释放!仅供楼主参考!没有完全测试
是不是在这里归定了所属父组件为Form1呢
begin
(Sender as TLabel).Parent.Free;
Halt;
end;
(Sender as TLabel).Parent.Free;的錯, 正確釋放後, 再報錯的, 現在還未找到原因.
不過這樣可以把錯誤去掉
procedure TForm1.Spaneldelete(Sender: TObject);
begin
(Sender as TLabel).Parent.Free;
abort;
end;