我想从一个字符串数组里取最长的一个字符串的长度该怎么取?根据字符串数组动态生成了很多按钮,点关闭时如何自动删除按钮记录???
解决方案 »
- 如何判断线程关系
- 一个ADOQuery怪的不能再怪的问题,大家请看....
- 几个关于 ado 连接 dbf (vfp) 数据库的问题!
- 关于dll调用ado时初始化和善后问题,折磨我几天了。请高手帮我解决。
- 删除记录
- delphi中的函数是否可以定义成不确定的参数,因为每次调用时参数不一样
- 100分求救!SQLServer连接Delphi6
- click一个dbgrid的某一cell时,如何得到鼠标点在这个cell上的相对位置。
- 大家快来帮忙! !想找一个VCL控件!!!!来着有分!!!
- 为什么DBRICHEDIT显示乱码
- vpn 网络文件传输问题
- ■■■如何突破string[255]的限制?■■■
2 在OnClose事件里用FreeAndNil释放掉按钮对象
var btn:array of Tbutton;procedure TForm2.FormShow(Sender: TObject);
var
count:integer;
top:integer;
i:integer;
begin
top:=0;
count := 1;
for i :=0 to high(s) do
begin
if s[i]<>'' then
begin
setlength(btn,Count); btn[Count-1]:=TButton.Create(self);
btn[count-1].parent:=self;
btn[Count-1].Caption :=s[i];
btn[count-1].Visible :=true;
btn[count-1].Width:=btn[i].Width;
btn[count-1].Height :=30;
btn[count-1].OnClick :=btn_click;
if count mod 2= 1 then
begin
top:=top+30;
btn[count-1].Left :=75;
btn[count-1].Top :=top ;
end
else
begin
btn[count-1].Left :=75+btn[i].Width;
btn[count-1].Top :=top ;
end;
Count:=Count+1;
if i>=6 then
begin
panel1.Height:=14+30*((count div 2)+(count mod 2));
end
else
begin
panel1.Height:=106;
end;
end;
end;
end;
麻烦写给我看下啊,我试了好多种方法都没成功~!!
====================================================================================如果按钮已经InsertControl到窗口,不用自己删除
=============================================================
表示已经插到窗口,不用自己删除
==================================================================
这就是你的代码问题了,如果你在窗口建立时可以确定,你可以把代码写在OnCreate时间中,如果写在OnShow中,可以在OnClose中删除:RemoveControl(btn[count-1]);
btn[count-1].Free;
setlength(btn,0)也可以。。然后再btn:=nil;
如果要关闭按纽 得调用按纽的free方法 记录一下按纽对象 循环释放一下就可以了
btn[count-1].Free;
btn[count-1].Free;
---------------------------------------------------------------
就把这句写在ONCLOSE里吗?可是它报错啊
btn[count-1].Free;
写哪呀?
=========================================================================
不会吧?那你得检查其它代码,在OnCreate与OnDestyoy对应,OnShow与OnClose对应,如果无其它原因,应该不错,刚试过:unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
procedure FormShow(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
Btn: TButton;
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.FormShow(Sender: TObject);
begin
Btn := TButton.Create(Self);
Btn.Parent := Self;
end;procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Btn.Parent := Self;
Btn.Free;
end;procedure TForm1.Button1Click(Sender: TObject);
begin
Close;
end;end.
begin
Btn.Parent := Self;
Btn.Free;
end;改为procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Btn.Parent := nil;
Btn.Free;
end;
[Warning] Unit2.pas(63): For loop control variable must be simple local variable
var btn:array of Tbutton;procedure TForm2.FormShow(Sender: TObject);
var
count:integer;
top:integer;
i:integer;
B: TButton;
begin
top:=0;
Btn := TList.Create;
for i :=low(s) to high(s) do
begin
if s[i]<>'' then
begin
B := TButton.Create(self);
B.Caption :=s[i];
B.Width := ????; // 自己决定吧
// btn[count-1].Visible :=true;
//这句本人不懂 btn[count-1].Width:=btn[i].Width;
B.Height :=30;
B.OnClick :=btn_click;
if count mod 2= 1 then
begin
top:=top+30;
B.Left :=75;
B.Top :=top ;
end
else
begin
B.Left :=75+btn[i].Width;
B.Top :=top ;
end;
B.parent:=self;
Btn.Add(B);
Count:= Btn.Count;
if i>=6 then
begin
panel1.Height:=14+30*((count div 2)+(count mod 2));
end
else
begin
panel1.Height:=106;
end;
end;
end;
end;procedure TForm2.FormClose(Sender: TObject);
var
I: Interger;
begin
for I := 0 to Btn.Count - 1 do
begin
TButton(Btn[I]).Parent := nil;
TButton(Btn[I]).Free;
end;
Btn.Free;
end;
========================================================
不要了.