שתף באמצעות


How can I get x and y coordinates of a control's location on a form relative to the Screens Top Left corner?

Question

Saturday, May 23, 2009 4:54 PM

Hi
How can I get x and y coordinates of a control's location on a form relative to the Screens Top Left corner?
I hope I have as the right question.

For example, if I have 3 labels on a form and I want label2 and label3 to display the x and y of label1 relative to the screens top and left corner.

When running the app, as the form's possition is changed label 2 and 3  update the coordinate of Label1's possition relative to the top left of the screen.

Daniel


I'm new to .Net, OPP and this forum but love it!

All replies (6)

Saturday, May 23, 2009 5:11 PM ✅Answered | 1 vote

Private Sub Form1_Move(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Move
        Label2.Text = (Me.Location.X + Label1.Location.X).ToString
        Label3.Text = (Me.Location.Y + Label1.Location.Y).ToString
End Sub

Thanks

  • Omie

Saturday, May 23, 2009 5:34 PM ✅Answered | 2 votes

Not quite, Label1.Location is in Form1's client coordinates which means that point (0,0) is not Form1's upper left corner but the upper left corner of its client area. You should do this instead:

    Private Sub Form1_Move(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Move
        Label2.Text = Me.PointToScreen(Label1.Location).X.ToString()
        Label3.Text = Me.PointToScreen(Label1.Location).Y.ToString()
    End Sub


Saturday, May 23, 2009 5:34 PM ✅Answered | 2 votes

 Private Sub Form1_Move(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Move

        Dim labelinscreen = Me.PointToScreen(Label1.Location)

        Label2.Text = labelinscreen.X.ToString

        Label3.Text = labelinscreen.Y.ToString

    End Sub


Arjun Paudel


Saturday, May 23, 2009 5:47 PM

umm.. Guys, when I set Form Location as 0,0.. How come PointToScreen(Me.Location) gives 8,28 ?

Thanks

  • Omie

Saturday, May 23, 2009 6:03 PM

Something is working for me, which of x and y evalutes to the Top property?

I'm new to .Net, OPP and this forum but love it!


Saturday, May 23, 2009 6:29 PM | 1 vote

X is Left and Y is Top.

Omie: You're calling PointToScreen( 0,0 ) which should return screen coordinates of the upper left corner of the client area of your form. The client area does not include the form borders and title bar. So this basically means that the border is 8 pixels wide and title bar is 28 pixels tall.