Rohit Shinde
Rohit Shinde

Reputation: 1603

Dragging and Dropping in VB.NET

I am preparing a chess program in VB.NET. So what I want to create is a drag and drop event. In drag and drop events, the original image is kept intact and the copy is placed wherever you want to place it.

But what I want to do is, I want to remove the original as soon as the image is being picked. Any idea how can I do that?

My user interface consists of 64 picture boxes arranged in rows of 8. And they all have images of their respective pieces on them.

Please help me.

Upvotes: 0

Views: 2440

Answers (1)

APrough
APrough

Reputation: 2701

@Hans is correct; it would be much easier to do this as one PictureBox. If, however, you are stuck on the method you are currently using, change the code in your MouseMove function on the source PictureBox to look like this. It basically copies the image to a variable, and then sets the source image to Nothing. Of course, you will have to handle if the move is not made (setting the source image back to the value of nImage) as well as dealing with disposing of the variable once the move is made.

Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As  _
System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
    If m_MouseIsDown Then
        ' Initiate dragging and allow either copy or move.
        Dim iImage As Image
        iImage = PictureBox1.Image
        PictureBox1.Image = Nothing
        PictureBox1.DoDragDrop(iImage, DragDropEffects.Copy Or _
DragDropEffects.Move)

    End If
    m_MouseIsDown = False
End Sub

Upvotes: 1

Related Questions