-1

Forgive me if this is a stupid question, but I'm not experienced and didn't find an answer to this problem.

I'm putting labels on a Panel (Form8.Panel1) in code depending on data stored in a datatable (treedata):

    For i = 0 To _tree.treedata.Rows.Count - 1

        Dim tb As New Label

        tb.Name = CStr(i)

        tb.AutoSize = True
        tb.MaximumSize = New Size(tb.Width, 70)
        tb.MinimumSize = New Size(tb.Width, 0)

        tb.Location = New Point(treedata.Rows(i)(11),treedata.Rows(i)(4))

        AddHandler tb.MouseMove, AddressOf obj1_MouseMove
        AddHandler tb.MouseDown, AddressOf obj1_MouseDown

        Form8.Panel1.Controls.Add(tb)

    Next

Using the MouseMove event I want to drag the labels around on the panel following the mouse:

Private Sub obj1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)

    If e.Button = MouseButtons.Left Then
        sender.Location = New Point(Form8.MousePosition.X, Form8.MousePosition.Y)

    End If
End Sub

What now happens is that when I click on a label and want it to follow the mouse it first "jumps away" meaning moves quite a bit away from the location of the mouse and only then follows the mouse. Does anyone know what I have to change in order to avoid this initial jump of the labels?

dunkleosteus
  • 336
  • 1
  • 12
  • `MousePosition` is not what you want, you need to translate those coordinates into control coordinates. – DonBoitnott Sep 05 '17 at 13:33
  • Something like this: https://stackoverflow.com/questions/28531058/find-position-of-mouse-relative-to-control-rather-than-screen#28533224. The part about `PointToClient` might be most relevant. – DonBoitnott Sep 05 '17 at 13:35

1 Answers1

0

Turned out to be quite easy, just MousePosition didn't give the position relative to the panel. This works:

        Dim newloc As Point = Form8.Panel1.PointToClient(Form8.MousePosition)
        sender.Location = newloc
dunkleosteus
  • 336
  • 1
  • 12
  • 1
    It is unfortunate that the VB.NET compiler allows this, really rather best to you use the correct identifier name. It is Control.MousePosition, makes it much more obvious that PointToClient is required and the position it returns has nothing to do with the form coordinates. – Hans Passant Sep 05 '17 at 15:05