如果这是一个愚蠢的问题,请原谅我,但我没有经验,也没有找到这个问题的答案。
我正在将标签放在面板上(form8.panel1)在代码中,具体取决于存储的数据(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
使用Mousemove事件,我想在鼠标之后将标签拖到面板上:
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
现在发生的事情是,当我单击标签并希望它遵循鼠标时,它首先"跳跃",这意味着与鼠标位置相当多,然后才遵循鼠标。有人知道我必须更改什么以避免标签的初始跳跃?
事实证明很容易,只是慕斯定位没有给出相对于面板的位置。这有效:
Dim newloc As Point = Form8.Panel1.PointToClient(Form8.MousePosition)
sender.Location = newloc