Results 1 to 40 of 40

Thread: [RESOLVED] Drag And Drop Shortcut

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2009
    Posts
    968

    Resolved [RESOLVED] Drag And Drop Shortcut

    Is it possible to drag an drop a desktop shourtcut to the form and then launch the program from there ? Is so how would this be done ?

  2. #2

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2009
    Posts
    968

    Re: Drag Drop files from windows into VB.NET

    Bump !!

    Just want to raise the above question again.

  3. #3
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Drag And Drop Shortcut

    Split into its own thread and moved to VB.NET

  4. #4
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Drag And Drop Shortcut

    Check this:
    vb.net Code:
    1. Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.     '' first allow the form to accept drop.
    3.     Me.AllowDrop = True
    4. End Sub
    5.  
    6. Private Sub Form3_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Me.DragEnter
    7.     '' when a file is dragged into our form, we change the mouse icon to signify that it can be dropped there.
    8.     If (e.Data.GetDataPresent(DataFormats.FileDrop)) Then
    9.         e.Effect = DragDropEffects.Copy
    10.     End If
    11. End Sub
    12.  
    13. Private Sub Form3_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Me.DragDrop
    14.     '' when the file is dropped on the form, we get the filename and open it.
    15.     Dim file As String = e.Data.GetData(DataFormats.FileDrop)(0)
    16.     Process.Start(file)
    17. End Sub

    I used a form for simplicity. But you can use any control on your form instead of the form surface itself as drop target.
    I also took only the first file to open for demo. If the user is dropping more than one file in a group, you might want to iterate through all the files and open all of them.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2009
    Posts
    968

    Re: Drag And Drop Shortcut

    Thanks m8 ill give that a go and let you know how i get on.

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2009
    Posts
    968

    Re: Drag And Drop Shortcut

    That works great , how would i display the shortcut image in a picturebox , when i drag and drop it to a picturebox on the form?

  7. #7
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Drag And Drop Shortcut

    That's a different question all together. Have a look here to see how to get the associated icon with any file type:
    http://www.codeguru.com/vb/gen/vb_mi...icle.php/c5597

    If you meant how to open an image file into your picture box then it's easy.
    Just modify the code in my last post as follows:
    vb.net Code:
    1. Private Sub Form3_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Me.DragDrop
    2.     Dim file As String = e.Data.GetData(DataFormats.FileDrop)(0)
    3.     'Process.Start(file)
    4.  
    5.     Me.PictureBox1.Image = Image.FromFile(file)
    6. End Sub
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2009
    Posts
    968

    Re: Drag And Drop Shortcut

    What im looking to do is , if i drop the firefox shortcut icon onto a picturebox , it displays the firefox shortcut icon.

  9. #9
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Drag And Drop Shortcut

    ok.. so ignore the code in last post. Have a look at the link that describes how to get a file's icon.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  10. #10

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2009
    Posts
    968

    Re: Drag And Drop Shortcut

    Ok m8 thanks alot , ill check that out now.

  11. #11

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2009
    Posts
    968

    Re: Drag And Drop Shortcut

    Can make head nor tail of that , but thanks anyway m8.

  12. #12
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Drag And Drop Shortcut

    Actually a file has no information about the icon that we see for that file. It is determined by file extension list that windows has.

    The code in that link uses the SHGetFileInfo API to get that icon. You may google around for "SHGetFileInfo" and might hit upon an easier example from some other source. Or this forum might already have some threads dedicated to this topic; just search and see
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  13. #13

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2009
    Posts
    968

    Re: Drag And Drop Shortcut

    Ok ill try that , thanks

  14. #14

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2009
    Posts
    968

    Re: Drag And Drop Shortcut

    Can u take a look at this snippet of code m8 , can u work it out ? Ive been trying to use it but cant seem to get it working.

    http://www.vbforfree.com/?p=396

  15. #15
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Drag And Drop Shortcut

    No.. that's of no help because you don't know the icon file path.

    The link I had posted earlier has exactly what you need. And I think the original author has fully described step by step how to get it working. Of course instead of ListView/ImageList, you would have to redirect it towards your picturebox.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  16. #16

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2009
    Posts
    968

    Re: Drag And Drop Shortcut

    Yea that works , but its not exactly what im looking for.
    This code add the desktop icon (small icon) to a listview and also the file path which i dont want.

    Code:
    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 Auto 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 = &H100
        Private Const SHGFI_SMALLICON = &H1
        Private Const SHGFI_LARGEICON = &H0    ' Large icon
        Private nIndex = 0
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim hImgSmall As IntPtr  'The handle to the system image list.
            Dim hImgLarge As IntPtr  'The handle to the system image list.
            Dim fName As String      'The file name to get the icon from.
            Dim shinfo As SHFILEINFO
            shinfo = New SHFILEINFO()
            Dim openFileDialog1 As OpenFileDialog
            openFileDialog1 = New OpenFileDialog()
    
            openFileDialog1.InitialDirectory = "c:\temp\"
            openFileDialog1.Filter = "All files (*.*)|*.*"
            openFileDialog1.FilterIndex = 2
            openFileDialog1.RestoreDirectory = True
    
            ListView1.SmallImageList = ImageList1
            ListView1.LargeImageList = ImageList1
    
            shinfo.szDisplayName = New String(Chr(0), 260)
            shinfo.szTypeName = New String(Chr(0), 80)
    
            If (openFileDialog1.ShowDialog() = DialogResult.OK) Then
                fName = openFileDialog1.FileName
    
                'Use this to get the small icon.
                hImgSmall = SHGetFileInfo(fName, 0, shinfo, _
                            Marshal.SizeOf(shinfo), _
                            SHGFI_ICON Or SHGFI_SMALLICON)
    
                'Use this to get the large icon.
                ''hImgLarge = SHGetFileInfo(fName, 0,
                ''ref shinfo, (uint)Marshal.SizeOf(shinfo),
                ''SHGFI_ICON | SHGFI_LARGEICON);
    
                'The icon is returned in the hIcon member of the
                'shinfo struct.
                Dim myIcon As System.Drawing.Icon
                myIcon = System.Drawing.Icon.FromHandle(shinfo.hIcon)
    
                ImageList1.Images.Add(myIcon)        'Add icon to
                'imageList.
    
                ListView1.Items.Add(fName, nIndex)  'Add file name and
                'icon to listview.
                nIndex = nIndex + 1
            End If
    
        End Sub
    End Class

  17. #17
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Drag And Drop Shortcut

    ok. Try this:
    vb.net Code:
    1. Imports System.Runtime.InteropServices
    2.  
    3. Public Class Form3
    4.     Private Structure SHFILEINFO
    5.         Public hIcon As IntPtr            ' : icon
    6.         Public iIcon As Integer           ' : icondex
    7.         Public dwAttributes As Integer    ' : SFGAO_ flags
    8.         <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=260)> _
    9.         Public szDisplayName As String
    10.         <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=80)> _
    11.         Public szTypeName As String
    12.     End Structure
    13.  
    14.     Private Declare Auto 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
    15.  
    16.     Private Const SHGFI_ICON = &H100
    17.     Private Const SHGFI_SMALLICON = &H1
    18.     Private Const SHGFI_LARGEICON = &H0    ' Large icon
    19.  
    20.     Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    21.         '' first allow the form to accept drop.
    22.         Me.AllowDrop = True
    23.     End Sub
    24.  
    25.     Private Sub Form3_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Me.DragEnter
    26.         '' when a file is dragged into our form, we change the mouse icon to signify that it can be dropped there.
    27.         If (e.Data.GetDataPresent(DataFormats.FileDrop)) Then
    28.             e.Effect = DragDropEffects.Copy
    29.         End If
    30.     End Sub
    31.  
    32.     Private Sub Form3_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Me.DragDrop
    33.         Dim file As String = e.Data.GetData(DataFormats.FileDrop)(0)
    34.  
    35.         Dim hImgLarge As IntPtr  'The handle to the system image list.
    36.         Dim shinfo As SHFILEINFO
    37.         shinfo = New SHFILEINFO()
    38.         Dim openFileDialog1 As OpenFileDialog
    39.         openFileDialog1 = New OpenFileDialog()
    40.  
    41.         shinfo.szDisplayName = New String(Chr(0), 260)
    42.         shinfo.szTypeName = New String(Chr(0), 80)
    43.  
    44.         'Use this to get the small icon.
    45.         'hImgSmall = SHGetFileInfo(fName, 0, shinfo, Marshal.SizeOf(shinfo), SHGFI_ICON Or SHGFI_SMALLICON)
    46.  
    47.         'Use this to get the large icon.
    48.         hImgLarge = SHGetFileInfo(file, 0, shinfo, Marshal.SizeOf(shinfo), SHGFI_ICON Or SHGFI_LARGEICON)
    49.  
    50.         'The icon is returned in the hIcon member of the shinfo struct.
    51.         Dim myIcon As System.Drawing.Icon = System.Drawing.Icon.FromHandle(shinfo.hIcon)
    52.         PictureBox1.Image = myIcon.ToBitmap
    53.     End Sub
    54. End Class
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  18. #18

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2009
    Posts
    968

    Re: Drag And Drop Shortcut

    Wow , hey , that works great +1 to you , thanks alot Pradeep1210

  19. #19

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2009
    Posts
    968

    Re: Drag And Drop Shortcut

    1 more thing , what i drag and drop the icon , how do i double click the picturebox to open the file , i tried this but e.data has a blue line under it.

    Code:
     Private Sub PictureBox1_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.DoubleClick
            Dim file As String = e.Data.GetData(DataFormats.FileDrop)(0)
            Process.Start(file)
        End Sub

  20. #20

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2009
    Posts
    968

    Re: Drag And Drop Shortcut

    Also how would i drag n drop different images to to different pictureboxes ?

  21. #21
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Drag And Drop Shortcut

    Quote Originally Posted by dunlop03 View Post
    1 more thing , what i drag and drop the icon , how do i double click the picturebox to open the file , i tried this but e.data has a blue line under it.

    Code:
     Private Sub PictureBox1_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.DoubleClick
            Dim file As String = e.Data.GetData(DataFormats.FileDrop)(0)
            Process.Start(file)
        End Sub
    No.. It won't work this way.
    In the Form DragDrop event, you have the name of the file. Store it in some form level variable, so that it is accessible here. And then use that variable here in the PictureBox DoubleClick event.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  22. #22

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2009
    Posts
    968

    Re: Drag And Drop Shortcut

    How do i do that lol ? and how can i place different images in different pictureboxes , theres 10 pictureboxes and i nee to be able to drag and drop a different icon to each picturebox.

  23. #23
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Drag And Drop Shortcut

    Quote Originally Posted by dunlop03 View Post
    Also how would i drag n drop different images to to different pictureboxes ?
    Just as I said in my post #4, the example shown was for the form for simplicity. You can use any control instead of the form.

    Replace all instances of Me / Form3 with PictureBox1 (or whatever is the name of your PictureBox) and that should work the way you want it to.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  24. #24

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2009
    Posts
    968

    Re: Drag And Drop Shortcut

    Ok , got u on the #4 Post , but as for replace all instances of Me / Form3 with PictureBox1 , im not sure what u mean , sry. Heres what im using so far

    Code:
    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 Auto 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 = &H100
        Private Const SHGFI_SMALLICON = &H1
        Private Const SHGFI_LARGEICON = &H0    ' Large icon
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            '' first allow the form to accept drop.
            Me.AllowDrop = True
        End Sub
    
        Private Sub Form1_DragEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles MyBase.DragEnter
            '' when a file is dragged into our form, we change the mouse icon to signify that it can be dropped there.
            If (e.Data.GetDataPresent(DataFormats.FileDrop)) Then
                e.Effect = DragDropEffects.Copy
            End If
        End Sub
    
        Private Sub Form1_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles MyBase.DragDrop
            Dim file As String = e.Data.GetData(DataFormats.FileDrop)(0)
            Dim hImgLarge As IntPtr  'The handle to the system image list.
            Dim shinfo As SHFILEINFO
            shinfo = New SHFILEINFO()
            Dim openFileDialog1 As OpenFileDialog
            openFileDialog1 = New OpenFileDialog()
            shinfo.szDisplayName = New String(Chr(0), 260)
            shinfo.szTypeName = New String(Chr(0), 80)
            'Use this to get the small icon.
            'hImgSmall = SHGetFileInfo(fName, 0, shinfo, Marshal.SizeOf(shinfo), SHGFI_ICON Or SHGFI_SMALLICON)#
            'Use this to get the large icon.
            hImgLarge = SHGetFileInfo(file, 0, shinfo, Marshal.SizeOf(shinfo), SHGFI_ICON Or SHGFI_LARGEICON)
            'The icon is returned in the hIcon member of the shinfo struct.
            Dim myIcon As System.Drawing.Icon = System.Drawing.Icon.FromHandle(shinfo.hIcon)
            PictureBox1.Image = myIcon.ToBitmap
        End Sub
    
    End Class

  25. #25

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2009
    Posts
    968

    Re: Drag And Drop Shortcut

    Ive tried this for to allow drops in all the picture boxes but its not working ?

    Code:
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            '' first allow the form to accept drop.
            Me.AllowDrop = True
            PictureBox1.AllowDrop = True
            PictureBox2.AllowDrop = True
            PictureBox3.AllowDrop = True
            PictureBox4.AllowDrop = True
            PictureBox5.AllowDrop = True
            PictureBox6.AllowDrop = True
            PictureBox7.AllowDrop = True
       End Sub

  26. #26
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Drag And Drop Shortcut

    vb.net Code:
    1. Imports System.Runtime.InteropServices
    2.  
    3. Public Class Form1
    4.     Private Structure SHFILEINFO
    5.         Public hIcon As IntPtr            ' : icon
    6.         Public iIcon As Integer           ' : icondex
    7.         Public dwAttributes As Integer    ' : SFGAO_ flags
    8.         <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=260)> _
    9.         Public szDisplayName As String
    10.         <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=80)> _
    11.         Public szTypeName As String
    12.     End Structure
    13.  
    14.     Private Declare Auto 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
    15.  
    16.     Private Const SHGFI_ICON = &H100
    17.     Private Const SHGFI_SMALLICON = &H1
    18.     Private Const SHGFI_LARGEICON = &H0    ' Large icon
    19.  
    20.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    21.         '' first allow the picturebox to accept drop.
    22.         PictureBox1.AllowDrop = True
    23.     End Sub
    24.  
    25.     Private Sub PictureBox1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles PictureBox1.DragEnter
    26.         '' when a file is dragged into our picturebox, we change the mouse icon to signify that it can be dropped there.
    27.         If (e.Data.GetDataPresent(DataFormats.FileDrop)) Then
    28.             e.Effect = DragDropEffects.Copy
    29.         End If
    30.     End Sub
    31.  
    32.     Private Sub PictureBox1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles PictureBox1.DragDrop
    33.         Dim file As String = e.Data.GetData(DataFormats.FileDrop)(0)
    34.  
    35.         Dim hImgLarge As IntPtr  'The handle to the system image list.
    36.         Dim shinfo As SHFILEINFO
    37.         shinfo = New SHFILEINFO()
    38.         Dim openFileDialog1 As OpenFileDialog
    39.         openFileDialog1 = New OpenFileDialog()
    40.  
    41.         shinfo.szDisplayName = New String(Chr(0), 260)
    42.         shinfo.szTypeName = New String(Chr(0), 80)
    43.  
    44.         'Use this to get the small icon.
    45.         'hImgSmall = SHGetFileInfo(fName, 0, shinfo, Marshal.SizeOf(shinfo), SHGFI_ICON Or SHGFI_SMALLICON)
    46.  
    47.         'Use this to get the large icon.
    48.         hImgLarge = SHGetFileInfo(file, 0, shinfo, Marshal.SizeOf(shinfo), SHGFI_ICON Or SHGFI_LARGEICON)
    49.  
    50.         'The icon is returned in the hIcon member of the shinfo struct.
    51.         Dim myIcon As System.Drawing.Icon = System.Drawing.Icon.FromHandle(shinfo.hIcon)
    52.         PictureBox1.Image = myIcon.ToBitmap
    53.         PictureBox1.Tag = file        '' we save the filename in the PictureBox Tag property, to be used later when doubleclicking.
    54.     End Sub
    55.  
    56.     Private Sub PictureBox1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles PictureBox1.DoubleClick
    57.         If Not String.IsNullOrEmpty(PictureBox1.Tag) Then
    58.             Process.Start(PictureBox1.Tag)
    59.         End If
    60.     End Sub
    61. End Class
    You would need to repeat these for all pictureboxes. Since we want the same operation for all pictureboxes, you may want to to generalize it by adding common event handlers or functions etc.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  27. #27

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2009
    Posts
    968

    Re: Drag And Drop Shortcut

    Amazing , thats working great , now ive another problem , how do i save it so that when i close my form , it saves all the drag and drops ?

  28. #28
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Drag And Drop Shortcut

    Since you had put the filenames in PictureBox Tag property, pick them up from there and save it wherever you like - file, registry, settings etc.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  29. #29

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2009
    Posts
    968

    Re: Drag And Drop Shortcut

    How would i use "my.computer.settings" ?

  30. #30

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2009
    Posts
    968

    Re: Drag And Drop Shortcut

    Ive tried this , but its no use.

    Code:
        Private Sub Form1_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
            My.Settings.pic1 = PictureBox1.Tag
            My.Settings.pic2 = PictureBox2.Tag
            My.Settings.Save()
        End Sub

  31. #31
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Drag And Drop Shortcut

    You should have those pic1 and pic2 keys before trying to do that.
    Check this out to see how to create those keys:
    http://msdn.microsoft.com/en-us/library/25zf0ze8.aspx
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  32. #32

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2009
    Posts
    968

    Re: Drag And Drop Shortcut

    Yes ive added the to my settings but can get them to save.

    Name = pic1 | Type = String | Scope = User

  33. #33

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2009
    Posts
    968

    Re: Drag And Drop Shortcut

    Am i even saving the correct thing here. (PictureBox2.Tag) or should i be saving something else ?

  34. #34
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Drag And Drop Shortcut

    What happens if you try to see it in debug window?

    Code:
    Debug.Print (My.Settings.pic1)
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  35. #35

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2009
    Posts
    968

    Re: Drag And Drop Shortcut

    Dam , not sure what you mean , i added that to a button and debuged it and in the immidiate window i got this "C:\Users\Richie\Desktop\Microsoft Visual Basic 2008 Express Edition.lnk"

  36. #36
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Drag And Drop Shortcut

    Yes.. so that's working. It is saving the file name and path correctly.
    Since you are dropping a file shortcut, it is saving the path/filename of the file shortcut.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  37. #37

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2009
    Posts
    968

    Re: Drag And Drop Shortcut

    do you mean is it displaying the path/filename in the picturebox when i drop it in ? No its only displaying the icon , but on double clicking it, it opens the program.

  38. #38

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2009
    Posts
    968

    Re: Drag And Drop Shortcut

    This is what im using in the form load event

    Code:
      PictureBox1.Tag = My.Settings.pic1
            PictureBox2.Tag = My.Settings.pic2

  39. #39
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Drag And Drop Shortcut

    Quote Originally Posted by dunlop03 View Post
    do you mean is it displaying the path/filename in the picturebox when i drop it in ? No its only displaying the icon , but on double clicking it, it opens the program.
    No.. What I mean is it is saving the file name and path in your My.Settings correctly.
    You saved it in the PictureBox Tag property, so it is in the Tag property. It won't be printed in the picturebox unless you put some code to do that, and that's a totally different question.

    Since your original question of dragging and dropping file into your form/PictureBox is resloved, I would advice you to mark this thread resolved and start a fresh thread, to get more attention on your new requirements.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  40. #40

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2009
    Posts
    968

    Re: Drag And Drop Shortcut

    Ok m8 ill do that , thanks for that.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width