Drag and drop between two group boxes

I have a panel with several group boxes on it.  Each group box contains a pcturebox and a label.  I can drag and drop an image and and label form various sources such as Explorer.  Now I would like to be able to rearragne them my dragging one to another that has no image or label.  This question follows a previous question: "How do I drop an EXE file on a picturebox and have the embeded icon be displayed with its name?"   I wopuld like to click on a picturebox and drag it to another, and have the text box be copied as well.
hlominacAsked:
Who is Participating?
 
Mike TomlinsonConnect With a Mentor Middle School Assistant TeacherCommented:
Assuming you have "Panel1" on your form, and that there is only ONE Label inside each GroupBox, the code below will handle any number of GroupBoxes.  Simply drag the PictureBoxes to change their order...

Imports System.Runtime.InteropServices
Public Class Form1
 
    Private Structure SHFILEINFO
        Public hIcon As IntPtr ' : icon
        Public iIcon As Integer ' : icondex
        Public dwAttributes As Integer ' : SFGAO_ flags
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=260)> _
        Public szDisplayName As String
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=80)> _
        Public szTypeName As String        
    End Structure
 
    Private Declare Ansi Function SHGetFileInfo Lib "shell32.dll" (ByVal pszPath As String, _
        ByVal dwFileAttributes As Integer, ByRef psfi As SHFILEINFO, ByVal cbFileInfo As Integer, _
        ByVal uFlags As Integer) As IntPtr
 
    Private Const SHGFI_ICON As Integer = &H100
    Private Const SHGFI_SMALLICON As Integer = &H1
    Private Const SHGFI_LARGEICON As Integer = &H0
 
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim pb As PictureBox
        For Each ctl As Control In Panel1.Controls
            If TypeOf ctl Is GroupBox Then
                For Each gbCtl As Control In ctl.Controls
                    If TypeOf gbCtl Is PictureBox Then
                        pb = DirectCast(gbCtl, PictureBox)
                        pb.AllowDrop = True
                        AddHandler pb.MouseMove, AddressOf pb_MouseMove
                        AddHandler pb.DragEnter, AddressOf pb_DragEnter
                        AddHandler pb.DragDrop, AddressOf pb_DragDrop
                        AddHandler pb.DoubleClick, AddressOf pb_DoubleClick
                    End If
                Next
            End If
        Next
    End Sub
 
    Private Sub pb_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        If e.Button = Windows.Forms.MouseButtons.Left Then
            Dim pb As PictureBox = DirectCast(sender, PictureBox)
            If Not (pb.Tag Is Nothing) Then
                Dim myData As New DataObject("MyDataFormat", pb)
                pb.DoDragDrop(myData, DragDropEffects.Move)
            End If
        End If
    End Sub
 
    Private Sub pb_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs)
        If e.Data.GetDataPresent(DataFormats.FileDrop) Then
            e.Effect = DragDropEffects.Link
        ElseIf e.Data.GetDataPresent("MyDataFormat") Then
            e.Effect = DragDropEffects.Move
        Else
            e.Effect = DragDropEffects.None
        End If
    End Sub
 
    Private Sub pb_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs)
        If e.Data.GetDataPresent(DataFormats.FileDrop) Then
            Dim pb As PictureBox = DirectCast(sender, PictureBox)
            Dim files() As String = e.Data.GetData(DataFormats.FileDrop)
            pb.Tag = files(0)
            SetLabel(pb)
 
            Dim shinfo As New SHFILEINFO
            shinfo.szDisplayName = New String(Chr(0), 260)
            shinfo.szTypeName = New String(Chr(0), 80)
            SHGetFileInfo(pb.Tag, 0, shinfo, Marshal.SizeOf(shinfo), SHGFI_ICON Or SHGFI_LARGEICON)
            pb.Image = Bitmap.FromHicon(shinfo.hIcon)
        ElseIf e.Data.GetDataPresent("MyDataFormat") Then
            Dim sourcePB As PictureBox = DirectCast(e.Data.GetData("MyDataFormat"), PictureBox)
            Dim targetPB As PictureBox = DirectCast(sender, PictureBox)
            Dim tmpImg As Image = targetPB.Image
            Dim tmpTag As String = targetPB.Tag
            targetPB.Image = sourcePB.Image
            targetPB.Tag = sourcePB.Tag
            SetLabel(targetPB)
            sourcePB.Image = tmpImg
            sourcePB.Tag = tmpTag
            SetLabel(sourcePB)
        End If
    End Sub
 
    Private Sub SetLabel(ByVal pb As PictureBox)
        For Each ctl As Control In pb.Parent.Controls
            If TypeOf ctl Is Label Then
                ctl.Text = System.IO.Path.GetFileName(pb.Tag)
            End If
        Next
    End Sub
 
    Private Sub pb_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim pb As PictureBox = DirectCast(sender, PictureBox)
        If Not (pb.Tag Is Nothing) Then
            If TypeOf pb.Tag Is String Then
                Process.Start(pb.Tag)
            End If
        End If
    End Sub
 
End Class

Open in new window

0
 
Bob LearnedCommented:
Since you know how to do drag and drop operations, what are you looking for help with?

Bob
0
 
hlominacAuthor Commented:
I am not able to drag from one of the group boxes.  I have written code to drag it using code from EE, but the code will not let me drop, eventhough I can drop from an external source.  I have come up with a work-around which is copy and paste, but I still would like to drag and drop if I could.
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
Bob LearnedCommented:
Did you set AllowDrop for the control, and add an event handler for DragDrop and DragOver (to set the allowable effects)?

Bob
0
 
hlominacAuthor Commented:
Yes.  I patched the new code into the old code I was using before.  Could this interfere?  I used DragEnter, DragDrop and Double Click.  I also used AllowDrop.
0
 
Bob LearnedCommented:
There is no telling what is "interfering".  I use the MouseMove event, and check for Button = Left Mouse Button to initiate a drag drop.  

Bob
0
 
hlominacAuthor Commented:
Thanks Idle_Mind for the code.  I have had to learn VB.Net strictly from the book, so I appreciate the good code.  I can learn from you guys!  What is best, You did it in one day, now I can rest through Thanksgiving Holidays!
Harold
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.