读书人

动态生成控件、caption赋值疏失

发布时间: 2012-09-01 09:33:03 作者: rapoo

动态生成控件、caption赋值出错。
动态生成按钮控件。。。//一行4个,现在的问题是只要最后一个按钮数是四的倍数取值就乱了。
单步调试时是正常的。。。
procedure TjobInfoForm.btnCreate();
var
i:Integer;
j:Integer;
heights:Integer;
widths:Integer;
tops:Integer;
num:Integer;
row:Integer;
isActivity:Boolean;

begin
heights:=73;
widths:=89;
tops:=25;
num:=0;
isActivity:=True;
with DataModule2.EmpQuery do
begin
First;
SetLength(controlBtn,RecordCount);
if RecordCount>4 then
begin
if RecordCount mod 4<>0 then //计算出一共要多少行控件组 4个/组
row:=RecordCount div 4+1
else row:=RecordCount div 4;
while isActivity do
begin
for i:=0 to row-1 do
begin
if i=0 then tops:=25
else
tops:=i*(25+heights)+20;
for j:=0 to 3 do
begin
if j=0 then
begin
controlBtn[num] := TsBitBtn.Create(nil);
controlBtn[num].Parent := btnPanel;
controlBtn[num].caption:=Fields.Fields[0].AsString;
Next;
controlBtn[num].height:=heights;
controlBtn[num].width:=widths;
controlBtn[num].left:=60;
controlBtn[num].top:=tops;
end

else
begin
inc(num);
if RecordCount=num+1 then
begin
isActivity:=false;
num:=0;
Break;
end;
controlBtn[num] := TsBitBtn.Create(nil);
controlBtn[num].Parent := btnPanel;
controlBtn[num].caption:=Fields.Fields[0].AsString;
Next;
controlBtn[num].height:=heights;
controlBtn[num].width:=widths;
controlBtn[num].left:=j*(20+widths)+60;
controlBtn[num].top:=tops;
end;
end;

end;
end;
end

else
begin
while isActivity do
begin
for j:=0 to 3 do
begin
if j=0 then
begin
controlBtn[num] := TsBitBtn.Create(nil);
controlBtn[num].Parent := btnPanel;
controlBtn[num].caption:=Fields.Fields[0].AsString;
Next;
controlBtn[num].height:=heights;
controlBtn[num].width:=widths;
controlBtn[num].left:=60;
controlBtn[num].top:=tops;
end
else
begin
inc(num);
controlBtn[num] := TsBitBtn.Create(nil);
controlBtn[num].Parent := btnPanel;
controlBtn[num].caption:=Fields.Fields[0].AsString;
Next;
controlBtn[num].height:=heights;
controlBtn[num].width:=widths;
controlBtn[num].left:=j*(20+widths);
controlBtn[num].top:=tops;
if RecordCount=num+1 then
begin
isActivity:=false;
num:=0;
end;
end;
end;
end;


end;
end;

end;


由于子窗体提交修改后要及时更新界面,控制界面的刷新代码如下。注:
timerResult是一个全局变量,jobInfo接收来自子窗体AjobInfoAdd的修改。

procedure TjobInfoForm.timerTimer(Sender: TObject);
begin
if timerResult then
begin
destoryControl();
treeViewCreate();
btnCreate();
timerResult:=False;
end;
end;



下面是jobInfo控件释放代码:

procedure TjobInfoForm.destoryControl();
var
i:integer;
begin
Node1:=nil;
tv1.Items.Clear;
with DataModule2.EmpQuery do
begin
for i:=0 to RecordCount-1 do
controlBtn[i].Free;
Close;
Open;
end;
end;

jobInfoAdd窗体提交事件
procedure TjobInfoAddForm.enterBtnClick(Sender: TObject);
begin
with DataModule2.tempQuery do
begin
SQL.Clear;
ExecSQL();
end;
timerResult:=True;
jobInfoAddForm.Close;
end;

[解决办法]
请试试用下面的代码代替你的同名过程:

Delphi(Pascal) code
procedure TjobInfoForm.btnCreate;//既然没参数,那对括号就省去也罢(过程定义的地方同时省去)const heights:Integer=73;      widths:Integer=89;      tops:Integer=25;var num:Integer;    row,col:Integer;begin  num:=0;  row:=0;  col:=0;  with DataModule2.EmpQuery do begin    SetLength(controlBtn,RecordCount);    First;    while not Eof do begin      controlBtn[num] := TsBitBtn.Create(nil);      controlBtn[num].Parent := btnPanel;      controlBtn[num].caption:=Fields.Fields[0].AsString;      controlBtn[num].height:=heights;      controlBtn[num].width:=widths;      controlBtn[num].left:=col*(20+widths)+60;;      controlBtn[num].top:=row*(tops+heights)+20;      inc(num);      inc(col);      if col>3 then begin        col:=0;        inc(row);      end;      Next;    end;  end;end; 

读书人网 >.NET

热点推荐