Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Drag and drop between two group boxes

Posted on 2007-11-19
7
Medium Priority
?
923 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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 86

Accepted Solution

by:
Mike Tomlinson earned 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

609 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