Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Drag&drop between objects
Message
Information générale
Forum:
ASP.NET
Catégorie:
Formulaires
Divers
Thread ID:
00867409
Message ID:
00867800
Vues:
16
I have this code that is working for 2 picture box (named PictureBox1 and PictureBox2):
    Private Sub PictureBox_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles PictureBox1.DragDrop, PictureBox2.DragDrop
        'Check if data is present
        If Not e.Data.GetDataPresent(DataFormats.Bitmap) Then Exit Sub

        'Determine if it is a copy or move operation
        If CBool(e.KeyState And 8) Then
            ' Display the copy cursor.
            e.Effect = DragDropEffects.Copy
        Else
            e.Effect = DragDropEffects.Move
        End If

        'Get a reference to the current textbox control
        Dim ctlCurrent As PictureBox = DirectCast(sender, PictureBox)
        'Paste the image to drop
        ctlCurrent.Image = DirectCast(e.Data.GetData(DataFormats.Bitmap), Image)
    End Sub

    Private Sub PictureBox_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles PictureBox1.DragEnter, PictureBox2.DragEnter
        'Check the format of the data being dropped.
        If e.Data.GetDataPresent(DataFormats.Bitmap) Then
            'Checks if the CTRL key is held
            If CBool(e.KeyState And 8) Then
                'Display the copy cursor.
                e.Effect = e.AllowedEffect And DragDropEffects.Copy
            Else
                'Display the move cursor
                e.Effect = e.AllowedEffect And DragDropEffects.Move
            End If
        Else
            'Display the no-drop cursor.
            e.Effect = DragDropEffects.None
        End If
    End Sub

    Private Sub PictureBox_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove, PictureBox2.MouseMove
        'Nothing to do if no mouse button is pressed.
        If e.Button = 0 Then Exit Sub

        'Get a reference to the current PictureBox control
        Dim ctlCurrent As PictureBox = DirectCast(sender, PictureBox)

        'If we get here, it means that the mouse is 
        '   being dragged outside the control.

        'Store the image to be drag into a DataObject object
        Dim objDO As New DataObject
        objDO.SetData(DataFormats.Bitmap, ctlCurrent.Image)

        'Starts the drag operation
        Dim effect As DragDropEffects = DragDropEffects.Copy Or DragDropEffects.Move
        effect = ctlCurrent.DoDragDrop(objDO, effect)

        'This portion of code will run after the code has been dropped.
        'Delete the image if it was a move.
        If effect = DragDropEffects.Move Then
            ctlCurrent.Image = Nothing
        End If
    End Sub
#End Region

    Private Sub Form1_Load( _
        ByVal sender As System.Object, ByVal e As System.EventArgs) _
        Handles MyBase.Load

        'Set here because this property doesn't appear into the properties dialog
        PictureBox1.AllowDrop = True
        PictureBox2.AllowDrop = True
    End Sub
>Thanks Eric, but, I forgot to mention in my previous post, I set this property.
>Actually in the new() event just after the InitializeComponent() call.
>Even adding allowdrop=true in the load() doesn't work, have you other suggestions?
>Keep in mind that the DragEnter, DragLeave, ecc. works!!
>
>Franco
>
>>You need to set the AllowDrop property of the PictureBox to True. But this property doesn't show into the properties dialog. The simplest workaround is to set them into the Load event:
>>
PictureBox1.AllowDrop = True
>>
>>My February column in the UTMag (www.utmag.com) is on this topic!
>>
>>
>>>Hi all,
>>>I'm trying to implement a simple Drag&Drop operation between two PictureBox on the same form (VB.NET), but it seems that the DragDrop event on the Destination object doesn't fire.
>>>If I put a messagebox in the DragEnter, DragLeave, ecc. I can see that those events fire, but the DragDrop no!
>>>Here's some of the code
>>>
>>>Private Sub PictureBoxSource_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBoxSource.MouseMove
>>>   If e.Button = MouseButtons.Left Then
>>>      Dim dropEffect As DragDropEffects = sender.DoDragDrop(sender, DragDropEffects.Copy)
>>>      MessageBox.Show("End drag! " + dropEffect.ToString)
>>>   End If
>>>End Sub
>>>
>>>Private Sub PictureBoxDest_DragOver(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles PictureBoxDest.DragOver
>>>   e.Effect = DragDropEffects.Copy
>>>End Sub
>>>
>>>Private Sub PictureBoxDest_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles PictureBoxDest.DragDrop
>>>   MessageBox.Show("DragDrop ")
>>>End Sub
>>>
>>>
>>>Perhaps I am not understanding how to do Drag&Drop operations, any suggestions?
>>>
>>>Thanks, Franco
Éric Moreau, MCPD, Visual Developer - Visual Basic MVP
Conseiller Principal / Senior Consultant
Moer inc.
http://www.emoreau.com
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform