Drag and dropping

Craig

Well-known member
Joined
Oct 15, 2005
Messages
110
Location
United Kingdom
Programming Experience
Beginner
Hi all,

I am creating a program where there are a number of pictureboxes on the form. What I need it to do is when the user dragdrops the picturebox onto a panel it will create a new, thumbnailed picturebox within that panel.

I searched MSDN and found this on dragging and dropping images, but this only allows you to dragdrop into a picturebox that is already on the form

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_vstechart/html/vbtchImpDragDrop.asp said:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles MyBase.Load
' Enable dropping.
PictureBox2.AllowDrop = True
End Sub

Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As _
System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
If Not PictureBox1.Image Is Nothing Then
' Set a flag to show that the mouse is down.
m_MouseIsDown = True
End If
End Sub

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.
PictureBox1.DoDragDrop(PictureBox1.Image, DragDropEffects.Copy Or _
DragDropEffects.Move)
End If
m_MouseIsDown = False
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) Then
' Check for the CTRL key.
If e.KeyState = 9 Then
e.Effect = DragDropEffects.Copy
Else
e.Effect = DragDropEffects.Move
End If
Else
e.Effect = DragDropEffects.None
End If
End Sub

Private Sub PictureBox2_DragDrop(ByVal sender As Object, ByVal e As _
System.Windows.Forms.DragEventArgs) Handles PictureBox2.DragDrop
' Assign the image to the PictureBox.
PictureBox2.Image = e.Data.GetData(DataFormats.Bitmap)
' If the CTRL key is not pressed, delete the source picture.
If Not e.KeyState = 8 Then
PictureBox1.Image = Nothing
End If

End Sub
Another problem that I think will occur is, when 1 picturebox is already on the form, and the user drags another.. I need it to place the second picturebox below the first

Thanks!
 
this is the code i modified for my needs, however to no avail

VB.NET:
 Dim m_MouseIsDown As Boolean

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Panel1.AllowDrop = True
    End Sub

    Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
        If Not PictureBox1.Image Is Nothing Then
            m_MouseIsDown = True
        End If
    End Sub

    Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
        If m_MouseIsDown Then
            PictureBox1.DoDragDrop(PictureBox1.Image, DragDropEffects.Move)
        End If
        m_MouseIsDown = False
    End Sub

    Private Sub Panel1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Panel1.DragEnter
        e.Effect = DragDropEffects.Move
    End Sub

    Private Sub Panel1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Panel1.DragDrop
        Dim picturebox2 As New PictureBox
        picturebox2.Left = 50
        picturebox2.Top = 50
        picturebox2.Width = 176
        picturebox2.Height = 160
        PictureBox2.Image = e.Data.GetData(DataFormats.Bitmap)
    End Sub
 
Ok well I have part of it sorted, added Panel1.Controls.Add(picturebox2) into the panel1_dragdrop procedure
Now i still need to be able to make the image a thumbnail
AND
if the user drags it again it will add it underneath it
AND
if they drag a different image it will also add it underneath it
 
Back
Top