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?
Mike TomlinsonConnect With a Mentor Middle 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

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

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.