Recent

Author Topic: canvas i need help  (Read 8706 times)

rvk

  • Hero Member
  • *****
  • Posts: 6163
Re: canvas i need help
« Reply #30 on: September 20, 2017, 11:12:12 am »
works it does not have i in it, how can it increase by one as the i increase 1?
I is the loop and increases each time.
You now have currentLeft := currentLeft + 1; so it also increases by 1 each time.
So that works correctly.

Although it's funny... now you have currentLeft which represents the boxnumber in the current row but you have currentTop which you increase by 20 each time. I would either use 1 each time (so you have boxnumber in row and rownumber in currentLeft and currentTop). Or use currentLeft + 30 each time and use real coordinates. Don't mix them.

In your case I would use  currenttop:=currenttop+1; and MyBoxes[i].Top := currenttop * 20;

And I would still use currentLeft > 5 check below the box :=.

So something like this: (I'll give complete code now because you already have it too)

Code: Pascal  [Select][+][-]
  1. const
  2.   MaxBoxes = 18;
  3.  
  4. procedure TForm1.FormCreate(Sender: TObject);
  5. var
  6.   i: integer;
  7.   currentLeft, currentTop: integer;
  8. begin
  9.   currentleft := 1; // initial box-column position
  10.   currenttop := 1; // initial box-row position
  11.  
  12.   SetLength(MyBoxes, MaxBoxes);
  13.   for i := 0 to MaxBoxes - 1 do // dynamic arrays are 0-based
  14.   begin
  15.     MyBoxes[i] := TShape.Create(Self);
  16.     MyBoxes[i].Parent := Self;
  17.     MyBoxes[i].Width := 20;
  18.     MyBoxes[i].Height := 10;
  19.     MyBoxes[i].Top := 20 { top } + (10 { width }  + 10 { space between } ) * currenttop;
  20.     MyBoxes[i].Left := 20 { left } + (20 { height } + 5 { space between } ) * currentleft;
  21.     MyBoxes[i].Shape := strectangle;
  22.     MyBoxes[i].Brush.Color := clGreen;
  23.     MyBoxes[i].pen.Color := clGreen;
  24.  
  25.     currentleft := currentleft + 1; // set currentLeft to next box
  26.     if currentleft > 5 then // 5 per row
  27.     begin
  28.       currentleft := 1; // back to first column
  29.       currenttop := currenttop + 1; // and next row
  30.     end;
  31.  
  32.   end;
  33.  
  34. end;

You see that initially I set the currentLeft and currentTop to Row 1 and Column 1.
The inside the loop first calculate the Left and Top of your Box.
After that you increase the column (currentLeft) by 1 and see if it exceeds 5.
If it does you reset the column (currentLeft) back to 1 (first column) and increase the row-counter (currentTop).

(Maybe now that you work with row and column-numbers the variables should be currentColumn and currentRow instead of currentLeft and currentRow.)

shs

  • Sr. Member
  • ****
  • Posts: 310
Re: canvas i need help
« Reply #31 on: September 20, 2017, 11:52:39 am »
Thank you so much! it helped a lot i really appreciate you help :))

can you please check this i really need help on that one too

http://forum.lazarus.freepascal.org/index.php/topic,38334.0.html

 

TinyPortal © 2005-2018