1

I want to change the position of the cursor over a particular label. I use:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    Cursor.Position = Label17.Location

End Sub

but it doesn't change where I want it. I tried:

   Label16.Location = Label17.Location

And this move the label16 properly.

So How do we move the cursor to the location of label17 or any label/object.

Alpagut
  • 1,173
  • 5
  • 15
  • 21

1 Answers1

3

The problem is that Label.Location (and any other Control.Location) refers to location relative to the upper-left corner of its container. This mean that you must call the PointToScreen method of the parent container. In a simple application that just have a Form without any other container, it will be:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    Cursor.Position = Me.PointToScreen(Label17.Location)

End Sub

A more elegant one:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    Cursor.Position = Label17.Parent.PointToScreen(Label17.Location)

End Sub
Jamby
  • 1,886
  • 2
  • 20
  • 30