2010-07-30 44 views
0

我在滚动框中创建了7个动态按钮。每行需要有2个按钮(按钮数量可能会变化),但下面的代码结果显示第一行有2个按钮,但其余所有在第二行,我应该如何将它固定到2个按钮1行?如何修复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; 

回答

3

因为你让事情太复杂了吗?

abtn.Left := (i mod 2) * abtn.Width; 
abtn.Top := Trunc((i div 2)) * abtn.Height; 

该做的很好。

+0

非常感谢=) – user367856 2010-07-30 07:31:25

+0

+1记住简单的(x,y)对数学不需要那么复杂! – 2010-07-30 12:58:03