我在滚动框中创建了7个动态按钮。每行只需要有2个按钮(按钮数量可能会有变化),但下面代码的结果显示第一行有2个按钮,但所有其余按钮在第2行,我应该如何将其固定为2个按钮1行?
procedure TForm1.CreateDynamicBtn2;
var
abtn: TBitBtn;
i, j, iNum, iCount : integer;
begin
if ScrollBox2.ControlCount > 0 then begin
for j := ScrollBox2.ControlCount - 1 downto 0 do begin
with ScrollBox2.Controls[j] AS TBitBtn do begin
Free;
end;
end;
end;
iCount := 0;
for i := 0 to 6 do begin
iNum := i + 1;
abtn := TBitBtn.Create(self);
abtn.Parent := ScrollBox2;
abtn.Visible := True;
abtn.Caption := 'dynamic' + IntToStr(i);
if iNum*abtn.Width > (iCount+2)*abtn.Width then begin
iCount := iCount + 1;
abtn.Left := (iCount * abtn.Width) - abtn.Width;
abtn.Top := abtn.Height;
end else begin
abtn.Left := i * abtn.Width;
abtn.Top := 0;
end;
end;
end;
答案 0 :(得分:3)
因为你让事情太复杂了?
abtn.Left := (i mod 2) * abtn.Width;
abtn.Top := Trunc((i div 2)) * abtn.Height;
应该很好地完成这个伎俩。