Dim mX As Integer
Private Sub PictureBox9_MouseDown(sender As Object, e As MouseEventArgs)
Dim pic As PictureBox = sender
pic.BringToFront()
movingPic = pic
movingPicOriginalLeft = pic.Left
mX = e.X
End Sub
Private Sub PictureBox9_MouseMove(sender As Object, e As MouseEventArgs)
Dim pic As PictureBox = sender
If pic Is movingPic Then
pic.Left += e.X - mX
End If
End Sub
Private Sub PictureBox9_MouseUp(sender As Object, e As MouseEventArgs)
Dim pic As PictureBox = sender
Dim OverlappingPic As PictureBox = GetOvelapping(pic)
RestoreLocation()
If Not OverlappingPic Is Nothing Then
SwapPicture(pic, OverlappingPic)
End If
End Sub
Function GetOvelapping(picturebox As PictureBox) As PictureBox
For Each pic In Pict
If pic.Visible = False Then Continue For
If pic Is picturebox Then Continue For
If IsOverlapping(pic, picturebox) Then Return pic
Next
Return Nothing
End Function
Function IsOverlapping(pic1 As PictureBox, pic2 As PictureBox) As Boolean
Return ((pic1.Left + pic1.Width) > pic2.Left) And
((pic1.Left) < pic2.Left + pic2.Width)
End Function
Sub SwapPicture(pic1 As PictureBox, pic2 As PictureBox)
Dim tmpLeft As Integer
tmpLeft = pic1.Left
pic1.Left = pic2.Left
pic2.Left = tmpLeft
End Sub
Sub RestoreLocation()
If movingPic Is Nothing Then Return
movingPic.Left = movingPicOriginalLeft
movingPic = Nothing
movingPicOriginalLeft = Nothing
mX = Nothing
End Sub