Solved

Drag and drop between two group boxes

Posted on 2007-11-19
7
858 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 

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 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

719 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