Drag and drop

Status
Niet open voor verdere reacties.

bartwebdesign

Gebruiker
Lid geworden
16 jul 2006
Berichten
443
Na lange tijd hier niet meer actief te zijn geweest, heb ik weer een vraag waar ik niet op uitkom.

Ik ben een programma aan het maken, waarin ik vanuit een listview een afbeelding naar één van de pictureboxen wil dragen.

Zogauw ik een item uit de listview naar een van de acht pictureboxen heb gesleept, moet hij daar ook verdwijnen.

Ik wil ze ook kunnen terugslepen.

Hoe doe ik dit?
 
Bedankt voor de link.

Maar dat is van picturebox naar Picturebox.

Ik wil dan van een listview naar een van de 8 pictureboxen.
Deze mag jezelf kiezen.
 
Je kan in ieder geval een deel van dat artikel gebruiken, en drag and drop gedeelte.

Nou moet ik zeggen dat dit drag and drop gebeuren nogal lastig is, ik heb zelf een poging gedaan, maar kwam er toen niet uit.

Misschien een idee om eerst drag and drop op twee pictureboxen te proberen, als dat eenmaal werkt kan je kijken of je de code kan aanpassen zodat het ook met listboxen werkt.
 
hi zet een listbox en 5 pictureboxen op een form

zet de volgende code er in pas het path naar je afbeeldingen aan.

Code:
Imports System.IO

Public Class Form1
    Dim m_MouseIsDown
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' Enable dropping.
        PictureBox1.AllowDrop = True
        PictureBox2.AllowDrop = True
        PictureBox3.AllowDrop = True
        PictureBox4.AllowDrop = True
        PictureBox5.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

    Private Sub PictureBox3_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles PictureBox3.DragDrop
        ' Assign the image to the PictureBox.
        PictureBox3.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

    Private Sub PictureBox3_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles PictureBox3.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 PictureBox4_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles PictureBox4.DragDrop
        ' Assign the image to the PictureBox.
        PictureBox4.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

    Private Sub PictureBox4_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles PictureBox4.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 PictureBox5_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles PictureBox5.DragDrop
        ' Assign the image to the PictureBox.
        PictureBox5.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

    Private Sub PictureBox5_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles PictureBox5.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 PictureBox1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles PictureBox1.DragDrop
        ' Assign the image to the PictureBox.
        PictureBox1.Image = e.Data.GetData(DataFormats.Bitmap)
        ' If the CTRL key is not pressed, delete the source picture.
        If Not e.KeyState = 8 Then
            PictureBox2.Image = Nothing

        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) 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 ListBox1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListBox1.MouseClick
        Dim FlashBestanden As New DirectoryInfo("C:\images")
        For Each FlashBestand As FileInfo In FlashBestanden.GetFiles("*.jpg")
            ListBox1.Items.Add(Path.GetFileNameWithoutExtension(FlashBestand.ToString))
        Next
    End Sub

    Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
        PictureBox1.Image = System.Drawing.Image.FromFile("C:\images\" & ListBox1.Text & ".jpg")
    End Sub


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

    Private Sub PictureBox2_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox2.MouseMove
        If m_MouseIsDown Then
            ' Initiate dragging and allow either copy or move.
            PictureBox2.DoDragDrop(PictureBox2.Image, DragDropEffects.Copy Or DragDropEffects.Move)
        End If
        m_MouseIsDown = False
    End Sub
End Class
 
Status
Niet open voor verdere reacties.
Terug
Bovenaan Onderaan