I'm having a bit of a problem with coordinates and mapping PictureBoxes. Here's the situation:

I've got a PictureBox holding an image of a football pitch. The aim is to record the shot positions on this pitch. So whenever the mouse is clicked on the pitch, I am recording the X and Y coordinates of the mouse at that time. This means that when I read from the database, it plots the shots accordingly.
The problem I am facing is when the form is moved from the top left corner of the screen.
I have the form set at 1024x768px - a fixed size. If the form is moved from the left corner of the screen, the shots are placed wrongly due to the coordinates changing.

I understand that the code that I am using is getting me the screen coordinates:

XPos = Control.MousePosition.X - 6
YPos = Control.MousePosition.Y - 27

What I am looking for is a way to get the coordinates relative to the form itself. This would mean that anytime I pull the coordinates from the database, they'll be correct no matter what the form's position is on the screen.

Any help would be appreciated.

Recommended Answers

All 2 Replies

why dont you use scaling??

like scaling 1024 to 1600

Member Avatar

Use PointToClient.

If you want the coordinates relative to the form Use:

Dim clientPoint as point = Me.PointToClient(Cursor.Position)

If you want the coordinates relative to the picBox Use:

Dim clientPoint as point = PictureBox1.PointToClient(Cursor.Position)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.