我想拖动以VB形式创建的任何标签。如果使用代码创建标签,那可能吗?
例如:我有20个标签,它们都是不同的名称。是否有一个代码可以让我拖动我用光标点击的任何标签我使用它的代码但是它只拖动我之前创建的标签但是如果我使用代码创建标签有一种方法可以拖动它们:
拖动1个标签的代码
单击并拖动仅在编辑视图中添加的1个标签的代码
Private Sub obj1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) 'varocarbas
' Check if the mouse is down
If Go = True Then
' Set the mouse position
HoldLeft = (Control.MousePosition.X - Me.Left)
HoldTop = (Control.MousePosition.Y - Me.Top)
' Find where the mouse was clicked ONE TIME
If TopSet = False Then
OffTop = HoldTop - sender.Top
' Once the position is held, flip the switch
' so that it doesn't keep trying to find the position
TopSet = True
End If
If LeftSet = False Then
OffLeft = HoldLeft - sender.Left
' Once the position is held, flip the switch
' so that it doesn't keep trying to find the position
LeftSet = True
End If
' Set the position of the object
sender.Left = HoldLeft - OffLeft
sender.Top = HoldTop - OffTop
End If
End Sub
用于在表单上创建标签的代码
Public Class Form1
Dim counter As Integer = 1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim lbl As New Label
lbl.Name = "Label" & counter
lbl.Size = New Size(80, 20)
lbl.Location = New Point(80, counter * 22)
lbl.Text = TextBox1.Text
AddHandler lbl.MouseMove, AddressOf obj1_MouseMove 'varocarbas
Me.Controls.Add(lbl)
counter += 1
End Sub
End Class
我想拖动创建的标签。这可能吗?
答案 0 :(得分:2)
在运行时创建控件时,除了填充属性外,还必须将相应的方法与所需的所有事件相关联。如果您要将obj1_MouseMove
添加到lbl
,则必须写:
AddHandler lbl.MouseMove, AddressOf obj1_MouseMove
从方法声明中删除Handles obj1.MouseMove
位。