Getting the absolute position of a control on a form?

I am trying to figure out how to get the absolute position of a control. I know about the Top/Left properties. The scenario is that I have a MDI form and ChildForms within it.

I'm trying to figure out what the Top/Left values are for a control on a ChildForm in relation to where the ChildForm is located within the MDI Form.

Who is Participating?
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.

Mike TomlinsonMiddle School Assistant TeacherCommented:
Use Form.PointToScreen() in the ChildForm to convert the controls client coords to screen coords.  Then pass that to the MdiParent and use Form.PointToClient() to convert those screen coords back to client coords.

*You can also use the Control itself to get its own screen coords.  Pass the Point (0, 0) to its PointToScreen() method:

    Dim pt As Point = PictureBox1.PointToScreen(New Point(0, 0))
BlakeMcKennaAuthor Commented:
Can you show me an example please...
Mike TomlinsonMiddle School Assistant TeacherCommented:
I ~think~ this what you want.

It shows the location of the button with respect to its parent (the MdiChild), also known as Client Coordinates.  Next it shows that same location in Screen Coordinates.  Then it converts that screen coords back to Client Coords but with respect to the MdiParent:
    ' ... running in an MdiChild Form ...
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim pt As Point = Button1.Location
        Debug.Print("Child Form Coords: " & pt.ToString)
        Dim ptScreen As Point = Button1.PointToScreen(New Point(0, 0))
        Debug.Print("Screen Coords: " & ptScreen.ToString)
        Dim ptMdiParent = Me.MdiParent.PointToClient(ptScreen)
        Debug.Print("MdiParent Coords: " & ptMdiParent.ToString)
    End Sub

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
BlakeMcKennaAuthor Commented:
Thanks Idle_Mind....exactly what I was looking for!
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.