Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to round values to a multiple of "X"

Status
Not open for further replies.

Nifrabar

Programmer
Mar 16, 2003
1,343
NL
Hi!
I am playing around a bit with drag and drop.
I am having a form with e.g. 10 equal sized buttons.
Now I want after dragging a button and drop onto the form to position the button in kind of grid with dimensions as dimensions of the buttons. Because I want to position the buttons in a neat way besides each others or under/above each other.
From the help-file I learnt the control will be positioned according:
Code:
LPARAMETERS oSource, nXCoord, nYCoord
oSource.Move ((nXCoord - oSource.Width / 2), (nYCoord - oSource.Height / 2))
So for proper locating on the form nYcoord should be rounded to a multiple of button.width
Same for vertical nXcoord must be a multiple of button.height.

I don't know how to. Must be not be that difficult but just don't know the syntax.

-Bart
 
With a little Searching of this forum area you would have found Olaf's "math "secret"" in thread184-1702057

It is probably worth your time to look it over.

Good Luck,
JRB-Bldr
 
Thanks jrbbldr,
I remember I have seen this before but my 58+ brains could not recall.
Tamars function did the job.
-Bart
 
Hi,

Are you placing the buttons visualy on the from or are you doing this by code?
In case you are doing it visualy you should use the align option:
First you select all the buttons, than you activate in the VFP menu Format -> here you have multiple options as you can see.

Groet,

Jockey(2)
 
Hi Jockey(2)
I am experimenting with drag and drop.
With the rounded values I can simulate kind of snap to grid.
-Bart
 
Instead of Tamars function you can also use the shorter
Code:
Procedure RoundToUnit(tnNumber, tnUnit)
   Return Round(tnNumber/tnUnit,0)*tnUnit
EndProc

Eg snap to grid with a 10 pixel grid you'd call xsnapped = RoundToUnit(xcoord,10) and ysnapped = RoundToUnit(ycoord,10) and xsnapped,ysnapped would then be coordinates multiples of 10, snapped to grid positions.

It doesn't need MOD(), MOD() is slow with large volumes of data or many calls per second. In todays systems it doesn't really matter, though.

Bye, Olaf.

 
And if the unit "X" is actually 10, you can also use Reound(x,-1). -1 decimal places mean, you round in units of 10. eg Round(5,-1) is 10, and Round(4,-1) is 0.

Bye, Olaf.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top