0

我想拖动以 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

我想拖动创建的标签。那可能吗?

4

1 回答 1

2

在运行时创建控件时,除了填充属性之外,您必须将相应的方法与您想要的所有事件相关联。如果要添加obj1_MouseMovelbl则必须编写:

AddHandler lbl.MouseMove, AddressOf obj1_MouseMove

Handles obj1.MouseMove并从方法声明中删除该位。

于 2013-10-11T15:07:29.103 回答