关于DELPHI删除动态创建的组件的语句问题
typeTForm1=class(TForm)Creatbtn:TButton;RemoveBtn:TButton;procedureCreatbtnClick(Send...
type
TForm1 = class(TForm)
Creatbtn: TButton;
RemoveBtn: TButton;
procedure CreatbtnClick(Sender: TObject);
procedure RemoveBtnClick(Sender: TObject);
private
IsLableCreated:Boolean;
Labels:array[0..2] of TLabel;
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
I: Integer;
implementation
{$R *.dfm}
procedure TForm1.CreatbtnClick(Sender: TObject);
begin
if not IsLableCreated then
begin
for I := 0 to 2 do
begin
Labels[I]:=TLabel.Create(Self);
with Labels[I] do
begin
Parent:=self;
Caption := 'Label ' + IntToStr(I);
Top := 175;
Width := 75;
Height :=75;
Left := I*Width +10;
end;
IsLableCreated := True;
end;
end;
end;
procedure TForm1.RemoveBtnClick(Sender: TObject);
begin
if IsLableCreated then
begin
for I := 0 to 2 do
Labels[I].Free;
IsLableCreated := False;
end;
end;
请高人点拨两个问题:1.出错语句:[Warning] Unit1.pas(35): For loop control variable must be simple local variable请解决。
2.以上只能创建和删除3个组件,若N个呢
大侠们,到底怎样传递参数,写详细呀。最好在delphi上验证一下。我是业余学的,好说不好做呀 展开
TForm1 = class(TForm)
Creatbtn: TButton;
RemoveBtn: TButton;
procedure CreatbtnClick(Sender: TObject);
procedure RemoveBtnClick(Sender: TObject);
private
IsLableCreated:Boolean;
Labels:array[0..2] of TLabel;
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
I: Integer;
implementation
{$R *.dfm}
procedure TForm1.CreatbtnClick(Sender: TObject);
begin
if not IsLableCreated then
begin
for I := 0 to 2 do
begin
Labels[I]:=TLabel.Create(Self);
with Labels[I] do
begin
Parent:=self;
Caption := 'Label ' + IntToStr(I);
Top := 175;
Width := 75;
Height :=75;
Left := I*Width +10;
end;
IsLableCreated := True;
end;
end;
end;
procedure TForm1.RemoveBtnClick(Sender: TObject);
begin
if IsLableCreated then
begin
for I := 0 to 2 do
Labels[I].Free;
IsLableCreated := False;
end;
end;
请高人点拨两个问题:1.出错语句:[Warning] Unit1.pas(35): For loop control variable must be simple local variable请解决。
2.以上只能创建和删除3个组件,若N个呢
大侠们,到底怎样传递参数,写详细呀。最好在delphi上验证一下。我是业余学的,好说不好做呀 展开
6个回答
展开全部
//窗体中放一个ScrollBox,一个Edit,两个Button
var
lx :integer;//全局变量
procedure TForm1.FormCreate(Sender: TObject);
begin
lx := 0;//初始化全局变量
end;
//Button1的单击事件,用来创建Label,创建的Label的数目由edit1来决定
procedure TForm1.Button1Click(Sender: TObject);
var
i :integer;
begin
Button2.Click;//创建时先删除原来已创建的
for i := 1 to strtoint(edit1.Text) do
begin
with TLabel.Create(self) do
begin
Parent := ScrollBox1;
Left := lx;
lx := lx + Width + 10;
Caption := 'Label ' + inttostr(i);
end;
end;
end;
//Button2的单击事件用来释放删除已经创建的Label
procedure TForm1.Button2Click(Sender: TObject);
var
i :integer;
begin
for i := ScrollBox1.ControlCount - 1 downto 0 do
if ScrollBox1.Controls[i] is TLabel then
(ScrollBox1.Controls[i] as TLabel).Free;
lx := 0;
end;
应该很清楚了吧?不清楚再问吧
var
lx :integer;//全局变量
procedure TForm1.FormCreate(Sender: TObject);
begin
lx := 0;//初始化全局变量
end;
//Button1的单击事件,用来创建Label,创建的Label的数目由edit1来决定
procedure TForm1.Button1Click(Sender: TObject);
var
i :integer;
begin
Button2.Click;//创建时先删除原来已创建的
for i := 1 to strtoint(edit1.Text) do
begin
with TLabel.Create(self) do
begin
Parent := ScrollBox1;
Left := lx;
lx := lx + Width + 10;
Caption := 'Label ' + inttostr(i);
end;
end;
end;
//Button2的单击事件用来释放删除已经创建的Label
procedure TForm1.Button2Click(Sender: TObject);
var
i :integer;
begin
for i := ScrollBox1.ControlCount - 1 downto 0 do
if ScrollBox1.Controls[i] is TLabel then
(ScrollBox1.Controls[i] as TLabel).Free;
lx := 0;
end;
应该很清楚了吧?不清楚再问吧
展开全部
for I := 0 to 2 do
Labels[I].Free;
IsLableCreated := False;
end;
把i换掉:
procedure TForm1.RemoveBtnClick(Sender: TObject);
var
k: integer;
begin
if IsLableCreated then
begin
for k := 0 to 2 do
......
2.将Labels定义成动态数组
Labels : array of TLable;
Labels[I].Free;
IsLableCreated := False;
end;
把i换掉:
procedure TForm1.RemoveBtnClick(Sender: TObject);
var
k: integer;
begin
if IsLableCreated then
begin
for k := 0 to 2 do
......
2.将Labels定义成动态数组
Labels : array of TLable;
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
count:=PanelTop.ControlCount;
for i:=0 to count-1 do
begin
PanelTop.RemoveControl(PanelTop.Controls[0]);
end;
for i:=0 to count-1 do
begin
PanelTop.RemoveControl(PanelTop.Controls[0]);
end;
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
试试将变量i的设置放在过程体内;
如果要创建N个,传个参数不可以吗?
如果要创建N个,传个参数不可以吗?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你自定义的变量(I)多次出现在同一个循环中就会这样,你应该多定义变量
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询