使用拖放功能创建动态标签 [英] Create Dynamic Label with Drag and Drop Function
问题描述
Hello All,
我想制作一个可以拖放的标签。在我拖动原始标签的同时,它会留下一份自己的副本。
示例:当我拖动Label1时,它会将另一个Label留给其原始位置(让我们将原始位置的New Label1命名为Label2),
然后我也应该是能够拖动Label2并留下另一个标签(Label3),我也想拖动Label1和Label2,但它不应该留下副本。
这是我的代码:我能够对Label和Dragging of Label进行乘法运算,但我无法拖动所有标签:
Hello All,
I want to make a Label that I can drag and drop. At the same time when I drag the original label it will leave a copy of it self.
Example: When I drag Label1 it will leave another Label to its Original Position( Lets Name the New Label1 in Original Position as Label2),
Then I should also be able to drag Label2 and Leave another Label (Label3), I also want to drag Label1 and Label2 but it should not leave a copy.
Here is my code: I been able to do multiplication of Label and Dragging of Label but I cannot drag all Labels:
Public Class Form1
Dim cursorX, cursorY As Integer
Dim dragging As Boolean
Dim labelno As Integer = 1
Private Sub Control_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Label1.MouseDown
dragging = True
cursorX = e.X
cursorY = e.Y
Dim label2 As New Label
labelno += 1
label2.Location = New Point(Label1.Location.X, Label1.Location.Y)
label2.Name = "Label" + labelno.ToString
label2.Text = label2.Location.ToString
Me.Controls.Add(label2)
End Sub
Private Sub Control_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Label1.MouseUp
dragging = False
End Sub
Private Sub control_MouseMOve(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Label1.MouseMove
If dragging Then
Dim ctrl As Control = CType(sender, Control)
ctrl.Left = (ctrl.Left + e.X) - cursorX
ctrl.Top = (ctrl.Top + e.Y) - cursorY
ctrl.BringToFront()
End If
End Sub
如果你有澄清的ase通知我。
谢谢
If you have clarification please inform me.
Thanks
推荐答案
希望这能解决你的问题。
Hi,
Hope this solves your problem.
Dim cursorX, cursorY As Integer
Dim dragging As Boolean
Dim labelno As Integer = 1
Private Sub Label1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Label1.MouseDown
dragging = True
cursorX = e.X
cursorY = e.Y
Dim label2 As New Label
labelno += 1
label2.Location = New Point(Label1.Location.X, Label1.Location.Y)
label2.Name = "Label" + labelno.ToString
label2.Text = label2.Location.ToString
Me.Controls.Add(label2)
End Sub
Private Sub Label1_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Label1.MouseMove
dragging = False
If e.Button = MouseButtons.Left Then
Dim control As Control
control = CType(sender, Control)
Dim nextPosition As New Point()
nextPosition = Me.PointToClient(MousePosition)
nextPosition.Offset(cursorX, cursorY)
control.Location = nextPosition
Invalidate()
End If
End Sub
Private Sub Label1_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Label1.MouseUp
If dragging Then
Dim ctrl As Control = CType(sender, Control)
ctrl.Left = (ctrl.Left + e.X) - cursorX
ctrl.Top = (ctrl.Top + e.Y) - cursorY
ctrl.BringToFront()
End If
End Sub
这篇关于使用拖放功能创建动态标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!