Solved

Drag and drop between two group boxes

Posted on 2007-11-19
7
774 Views
Last Modified: 2008-02-01
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.
0
Comment
Question by:hlominac
  • 3
  • 3
7 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 20321133
Since you know how to do drag and drop operations, what are you looking for help with?

Bob
0
 

Author Comment

by:hlominac
ID: 20321350
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
 
LVL 96

Expert Comment

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

Bob
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:hlominac
ID: 20323101
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
 
LVL 96

Expert Comment

by:Bob Learned
ID: 20323131
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
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 20323612
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
 

Author Comment

by:hlominac
ID: 20324071
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

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now