vb.netdrag-and-dropalignmentsnapping

Snap PictureBox to Buttons after Drag and Drop


I'm making a Battleship game, and I'm using buttons as the grid(playerboard). I'm using a picture box as the ship, and I'm trying to make it so that the picturebox snaps to the buttons that it is colliding with.

I have already done the drag and drop, and collision part, but I'm struggling with the snapping to buttons part. The picture box is the size of two buttons. I tried to align the picturebox with the buttons, by using picturebox.left = button.left, but it chooses the wrong button of the two.

Dim Off As Point
Private Sub picDestroyer_MouseDown(sender As Object, e As MouseEventArgs) Handles picDestroyer.MouseDown
    Off.X = MousePosition.X - sender.Left 'Click and Drag ship
    Off.Y = MousePosition.Y - sender.Top
End Sub

Private Sub picDestroyer_MouseMove(sender As Object, e As MouseEventArgs) Handles picDestroyer.MouseMove
    If e.Button = MouseButtons.Left Then
        sender.Left = MousePosition.X - Off.X 'Click and Drag ship
        sender.Top = MousePosition.Y - Off.Y
    End If
End Sub
Private Sub picDestroyer_DoubleClick(sender As Object, e As EventArgs) Handles picDestroyer.DoubleClick
    If picDestroyer.Size = New Size(52, 21) Then 'Rotate Pic if double clicked
        picDestroyer.Size = New Size(21, 52)
    ElseIf picDestroyer.Size = New Size(21, 52) Then
        picDestroyer.Size = New Size(52, 21)
    End If
End Sub
Private Sub picDestroyer_MouseLeave(sender As Object, e As EventArgs) Handles picDestroyer.MouseLeave

    For Each button In Me.Controls
        If picDestroyer.Bounds.IntersectsWith(button.bounds) Then
            button.backcolor = Color.Red
            picDestroyer.BackColor = SystemColors.Control
        End If
        If picDestroyer.Bounds.IntersectsWith(button.bounds) And picDestroyer.Size = New Size(52, 21) Then
            picDestroyer.Top = button.top
        End If
        If picDestroyer.Bounds.IntersectsWith(button.bounds) And picDestroyer.Size = New Size(21, 52) Then
            picDestroyer.Left = button.left
        End If
    Next

Solution

  • To get the button which you want to snap to you can temporarily disable the PictureBox and call GetChildAtPoint() on the form to get the child control at the location of the PictureBox, specifying GetChildAtPointSkip.Disabled as the second parameter so that the search won't include your disabled PictureBox but all the other controls above/below it.

    After that you can just set the PictureBox's coordinates to that of the button.

    There are also a few improvements that can be made to your code:

    All that said, this code should work:

    If e.Button = Windows.Forms.MouseButtons.Left Then
    
        picDestroyer.Enabled = False 'Temporarily disable the picture box so that it isn't included in the child search.
        Dim ChildBelowDestroyer As Control = Me.GetChildAtPoint(picDestroyer.Location, GetChildAtPointSkip.Disabled) 'Look for any child control that isn't disabled.
        picDestroyer.Enabled = True 'Re-enable the picture box.
    
        If ChildBelowDestroyer Is Nothing OrElse _
            ChildBelowDestroyer.GetType() IsNot GetType(Button) Then
            Return 'Do not continue if no child was found below the picture box, or if the child is not a button.
        End If
    
        picDestroyer.Location = ChildBelowDestroyer.Location 'Snap the picture box to the button (sets the X- and Y-coordinates).
    
        For Each button In Me.Controls.OfType(Of Button)() 'OfType() to only look for buttons.
            If picDestroyer.Bounds.IntersectsWith(button.Bounds) Then
                button.BackColor = Color.Red
                picDestroyer.BackColor = SystemColors.Control
            End If
        Next
    
    End If
    

    Another thing to keep in mind is to use AndAlso instead of And, and OrElse instead of Or in If-statements since AndAlso and OrElse are short-circuited.