Assigning multiple snap to points using New Point

Hi All

I have found the following line of code that allows me to declare allowed locations for picture boxes to snap to, when moved by the user.

Private _allowedLocations As Point() = {New Point(1, 1), New Point(36, 1), New Point(71, 1), New Point(106, 1), New Point(141, 1)}

The problem is that my grid is 15 x 15, which would mean having to set 225 New Point coordinates.

Is there any way to do this using, for example, a loop, or some other way of assigning the x and y coordinates?


Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fernando SotoRetiredCommented:
Hi Terrygordon;

The below code should get you started on what you need to do to create all 225 Point objects dynamically. You will need to adjust xIncrement and yIncrement for the width and height of your PictureBox.
Dim x = 1
Dim y = 1
Dim index = 0
Dim xIncrement = 35        ' Number of pixels wide of Picture box
Dim yIncrement = 35        ' Number of pixels High of Picture box
Dim columns = 15           ' Number of Picture box per row
Dim rows = 15              ' Number of rows of Picture boxs 
' Array to hold all picture boxes
Dim _allowedLocations(224) As Point

Code to create all 255 Point's for the picture boxes
For i As Integer = 0 To columns - 1
    For j As Integer = 0 To rows - 1
        _allowedLocations(index) = New Point(x, y)
        index += 1
        x += xIncrement
    x = 1
    y += yIncrement

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
I would use a List(Of Point) instead.

...but why use an Array/List at all if it is a fixed grid?

Just compute the nearest point from the mouse position.

Integer Division ("\" as opposed to "/") might come in handy...
TerrygordonAuthor Commented:
Thanks Fernando - works perfectly...and you even used my picturebox sizes.
Fernando SotoRetiredCommented:
Not a problem Terrygordon, glad I was able to help.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.