tags:

views:

51

answers:

1

i have create 7 dynamic buttons in a scroll box. Each row need to have 2 buttons (number of buttons may change) only, but the result of the code below shows the first row with 2 buttons but all the rest in 2nd row, how should i fixed it to 2 buttons 1 row?

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  A: 

Because you are making things way too complicated?

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

Should do the trick nicely.

Marjan Venema
thanks a lot =)
+1 for remembering that simple (x,y) pair math need not be that complicated!
Warren P