如何使用vb.net在面板中移动图片框

如何使用vb.net在面板中移动图片框,vb.net,picturebox,Vb.net,Picturebox,我试图移动面板中的图片框 这是我的代码: Private dragging As Boolean Private beginX, beginY As Integer Private Sub Control_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) dragging = True beginX = CType(sender, PictureBo

我试图移动面板中的图片框

这是我的代码:

Private dragging As Boolean
Private beginX, beginY As Integer

Private Sub Control_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        dragging = True
        beginX = CType(sender, PictureBox).Location.X
        beginY = CType(sender, PictureBox).Location.Y
End Sub

Private Sub Control_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        Dim cntrl As Control = CType(sender, Control)
        If dragging = True Then
            cntrl.Location = New Point(cntrl.Location.X + e.X - beginX, cntrl.Location.Y + e.Y - beginY)
            'Me.Refresh()
        End If
End Sub

Private Sub Control_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        dragging = False
End Sub

我不明白为什么这不起作用。

您的子例程缺少它们的处理程序,即末尾的handles语句

例:

试试这个:

    Dim cmd As Boolean = False     
    Dim sp As Point
    Private Sub Form1_Load() Handles MyBase.Load 
        For Each Control As Picturebox In Me.Controls.OfType(Of Picturebox)
        AddHandler Control.MouseDown, Sub(sender As Object, e As MouseEventArgs)
                                          cmd = True
                                          sp = e.Location
                                      End Sub
        AddHandler Control.MouseMove, Sub(sender As Object, e As MouseEventArgs)
                                          If cmd Then
                                              Control.Location = Control.Location - sp + e.Location
                                          End If
                                      End Sub
        AddHandler Control.MouseUp, Sub(sender As Object, e As MouseEventArgs)
                                        cmd = False
                                    End Sub 
        Next
    End Sub

我在循环中为循环btn=新建PictureBox AddHandler btn.MouseUp,AddressOf Control\u MouseUp AddHandler btn.MouseDown,AddressOf Control\u MouseDown AddHandler btn.MouseMove创建了图片框,AddressOf Control\u MouseMove pnlLocker.Controls.Addbtn下一次我点击图片框时,它会闪烁,当我移动鼠标时,图片框不会改变它的位置……也许你可以说一点你的代码,让其他人理解,而不是仅仅复制。
    Dim cmd As Boolean = False     
    Dim sp As Point
    Private Sub Form1_Load() Handles MyBase.Load 
        For Each Control As Picturebox In Me.Controls.OfType(Of Picturebox)
        AddHandler Control.MouseDown, Sub(sender As Object, e As MouseEventArgs)
                                          cmd = True
                                          sp = e.Location
                                      End Sub
        AddHandler Control.MouseMove, Sub(sender As Object, e As MouseEventArgs)
                                          If cmd Then
                                              Control.Location = Control.Location - sp + e.Location
                                          End If
                                      End Sub
        AddHandler Control.MouseUp, Sub(sender As Object, e As MouseEventArgs)
                                        cmd = False
                                    End Sub 
        Next
    End Sub