5,379
社区成员
发帖
与我相关
我的任务
分享
for i:=sb1.ControlCount-1 downto 0 do
begin
if (sb1.Controls[i] is TEdit) then
TEdit(sb1.Controls[i]).Free; //注意这里你遍历的是sb1
end;
你把我这个单元测试一下看看你那里不对
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
s:Integer;
procedure CreateNewEdit(count:Integer);
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
i:Integer;
begin
for i:=self.ControlCount-1 downto 0 do
begin
if (self.Controls[i] is TEdit) then
TEdit(Controls[i]).Free
end;
if s>0 then
CreateNewEdit(s);
end;
procedure TForm1.CreateNewEdit(count: Integer);
var
MyEdit:TEdit;
i:Integer;
begin
for i:=0 to count-1 do
begin
MyEdit:= TEdit.Create(Owner);
MyEdit.Top:=i*50;
MyEdit.Parent:=self;
MyEdit.Width:=100;
MyEdit.Height:=30;
MyEdit.Visible:=True;
end;
inc(s,-1);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
s:=10;
end;
end.
procedure Tfmfaliao.RefreshForm;
var
i:Integer;
begin
// for i:=sb1.ControlCount-1 Downto 0 do
// begin
// if (sb1.Controls[i] is TEdit) then
// TEdit(Controls[i]).Free;
// end;
for i:=sb1.ControlCount-1 downto 0 do
begin
if (sb1.Controls[i] is TEdit) then
TEdit(Controls[i]).Free
end;
end;
function Tfmfaliao.CreateNewTurnInRecord(const ATop: Integer): TBaseTurnInData;
begin
with Result do
begin
Result := TBaseTurnInData.Create(Self);
CheckBox := CreateNewCheckBox(Result, sb1, ATop-19, 8, 17);
PartNOEdit := CreateNewEdit(Result,sb1,ATop-19,28,121);
QtyEdit := CreateNewEdit(Result,sb1,ATop-19,159,57);
Qty2Edit := CreateNewEdit(Result,sb1,ATop-19,226,57);
Qty3Edit := CreateNewEdit(Result,sb1,ATop-19,294,57);
MOEdit := CreateNewEdit(Result,sb1,ATop-19,361,121);
Qty4Edit := CreateNewEdit(Result,sb1,ATop-19,496,57);
CheckBox.OnClick := chkTurnInClick;
Qty4Edit.OnKeyPress := IntegerEditKeyPress;
Qty4Edit.OnChange := chkQty4OnChange;
end;
end;
procedure Tfmfaliao.tlRefreshClick(Sender: TObject);
var
I:Integer;
ATurnIn:TBaseTurnInData;
begin
RefreshForm; //這就是要刷新Form的方法了。
I := 1;
TWip_Runcard.GetWipTurnByWO3(quGet,quGet1,lsPNQuery.Items);
while not quGet.Eof do
begin
ATurnIn := CreateNewTurnInRecord(i*27) ;//這裡是新建控件
with ATurnIn,quGet do //quGet查到多少條記錄,就要創建多少行Edit,每行是5個Edit.有時候查詢出來1條記錄,但有時候查詢出來2條,但有時候可能一條記錄都沒有,所以,當沒有記錄的時候,之前顯示在Form上的Edit要全部Free掉,
begin
PartNOEdit.Text := FieldByName('Part_NO').AsString;
QtyEdit.Text := FieldByName('TOTAL').AsString;
Qty2Edit.Text := FieldByName('Yifa').AsString;
Qty3Edit.Text := FieldByName('Kefa').AsString;
CheckBox.Enabled := StrToInt(Qty3Edit.Text) <> 0;
TurnInList.Add(ATurnIn);
Inc(i);
quGet.Next;
end;
end;
end;
var
i:Integer;
MyEdit:TEdit;
begin
for i:=self.ComponentCount-1 downto 0 do
begin
if (self.Components[i] is TEdit) then
TEdit(Components[i]).Free
end;
for i:=0 to 5 do
begin
MyEdit:= TEdit.Create(Owner);
MyEdit.Top:=i*50;
MyEdit.Parent:=self;
MyEdit.Width:=100;
MyEdit.Height:=30;
MyEdit.Visible:=True;
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
i:integer;
begin
for i:=self.ControlCount-1 downto 0 do
begin
if (self.Controls[i] is TEdit) then
TEdit(Controls[i]).Free
end;
end;
修改成这样
for i:=self.ComponentCount-1 downto 0 do
begin
if (self.Components[i] is TEdit) then
TEdit(Components[i]).Free
end;