Click here to Skip to main content
15,881,682 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to drag and drop a particular picturebox but I can't get what I want. I want the image seem like it is being hold during mousemove/mousedown like a normal drag and drop but it a "box with +" is the only thing appears during the said events. Please help me. These are my codes:

VB
Private Sub PictureBox2_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles PictureBox2.DragDrop
    Dim bm As Bitmap = _
       DirectCast(e.Data.GetData(DataFormats.Bitmap, True),  _
       Bitmap)


    If PictureBox3.Tag = PictureBox1.Tag And PictureBox4.Tag = PictureBox1.Tag Then
        PictureBox2.Image = bm
    Else
        PictureBox2.Image = OldImage2
        PictureBox1.Image = OldImage1
    End If

End Sub
Private Sub PictureBox2_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles PictureBox2.DragEnter
    If (e.Data.GetDataPresent(DataFormats.Bitmap) AndAlso _
    (e.AllowedEffect And DragDropEffects.Copy) <> 0) _
 Then
        ' Allow this.
        e.Effect = DragDropEffects.Copy

        ' Save the current image.
        OldImage1 = PictureBox1.Image
        OldImage2 = PictureBox2.Image

        PictureBox2.Image = OldImage1
        PictureBox1.Image = OldImage2


    Else
        ' Don't allow any other drop.
        e.Effect = DragDropEffects.None
    End If
End Sub
Private Sub PictureBox2_DragLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles PictureBox2.DragLeave
    ' Restore the saved image.
    PictureBox2.Image = OldImage2
    PictureBox1.Image = OldImage1
End Sub
Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
 

    ' Start the drag if it's the right mouse button.
    If (e.Button = MouseButtons.Left) Then
        Dim source As PictureBox = DirectCast(sender,  _
            PictureBox)
        PictureBox1.DoDragDrop(source.Image, _
            DragDropEffects.Copy)
    End If

End Sub


Private Sub PictureBox1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles PictureBox1.DragDrop
    Dim bm As Bitmap = _
       DirectCast(e.Data.GetData(DataFormats.Bitmap, True),  _
       Bitmap)


    If PictureBox9.Tag = PictureBox1.Tag And PictureBox17.Tag = PictureBox1.Tag Then
        PictureBox1.Image = bm
    Else
        PictureBox1.Image = OldImage2
        PictureBox2.Image = OldImage1
    End If

    'If PictureBox3.Tag = PictureBox1.Tag And PictureBox4.Tag = PictureBox1.Tag Then
    'Else
    '    MsgBox("NOT THE SAME")
    'End If

End Sub
Private Sub PictureBox1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles PictureBox1.DragEnter
    If (e.Data.GetDataPresent(DataFormats.Bitmap) AndAlso _
    (e.AllowedEffect And DragDropEffects.Copy) <> 0) _
 Then
        ' Allow this.
        e.Effect = DragDropEffects.Copy

        ' Save the current image.
        OldImage2 = PictureBox1.Image
        OldImage1 = PictureBox2.Image

        PictureBox1.Image = OldImage1
        PictureBox2.Image = OldImage2


    Else
        ' Don't allow any other drop.
        e.Effect = DragDropEffects.None
    End If
End Sub
Private Sub PictureBox1_DragLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles PictureBox1.DragLeave
    ' Restore the saved image.
    PictureBox2.Image = OldImage1
    PictureBox1.Image = OldImage2
End Sub
Private Sub PictureBox2_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox2.MouseDown



    ' Start the drag if it's the right mouse button.
    If (e.Button = MouseButtons.Left) Then
        Dim source As PictureBox = DirectCast(sender,  _
            PictureBox)
        PictureBox2.DoDragDrop(source.Image, _
            DragDropEffects.Copy)
    End If
End Sub


Thanks.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900