Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 289
  • Last Modified:

How do I drop an EXE file on a picturebox and have the embeded icon be displayed with its name?

In a Visual Basic.net form I have several picture boxes.  I would like to drag an executble file from windows explorer and have its icon displayed along with the file name on one of the picture boxes.  Then when I click on one, I wish to execute that program.  I am new to drag and drop, so I need some example code as a guide.
0
hlominac
Asked:
hlominac
1 Solution
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Based on the code here:
http://support.microsoft.com/kb/319340/

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
        PictureBox1.AllowDrop = True
    End Sub

    Private Sub PictureBox1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles PictureBox1.DragEnter
        If e.Data.GetDataPresent(DataFormats.FileDrop) Then
            e.Effect = DragDropEffects.Link
        Else
            e.Effect = DragDropEffects.None
        End If
    End Sub

    Private Sub PictureBox1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles PictureBox1.DragDrop
        Dim files() As String = e.Data.GetData(DataFormats.FileDrop)
        PictureBox1.Tag = files(0)
        Label1.Text = PictureBox1.Tag

        Dim shinfo As New SHFILEINFO
        shinfo.szDisplayName = New String(Chr(0), 260)
        shinfo.szTypeName = New String(Chr(0), 80)
        SHGetFileInfo(PictureBox1.Tag, 0, shinfo, Marshal.SizeOf(shinfo), SHGFI_ICON Or SHGFI_LARGEICON)
        PictureBox1.Image = Bitmap.FromHicon(shinfo.hIcon)
    End Sub

    Private Sub PictureBox1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles PictureBox1.DoubleClick
        If Not (PictureBox1.Tag Is Nothing) Then
            If TypeOf PictureBox1.Tag Is String Then
                Process.Start(PictureBox1.Tag)
            End If
        End If
    End Sub

End Class
0
 
hlominacAuthor Commented:
Thanks Idle_Mind.  That was fast!  I tried for several hours, and decided I should ask an expert.  I'm glad you fellows have time to help us learners!
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now