drag file into Picturebox

Jimmythegreat

Member
Joined
Jul 19, 2006
Messages
17
Programming Experience
3-5
I want to be able to drag a .exe or some other executable file into a pictureBox. Get the files path and save it to a varible. Then set the pictureBox's image to the executable files image. I have no clue how to even start doing this.
 
Last edited by a moderator:
The PictureBox control is not designed to allow drag and drop procedures. You can for example use the form itself as drop target (set AllowDrop=True). Example code:
VB.NET:
Private Sub frmDiv_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Me.DragEnter
    If e.Data.GetDataPresent(DataFormats.FileDrop) Then e.Effect = e.AllowedEffect
End Sub

Private Sub frmDiv_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Me.DragDrop
    Dim filedrops() As String = e.Data.GetData(DataFormats.FileDrop)
    Me.PictureBox1.Image = Icon.ExtractAssociatedIcon(filedrops(0)).ToBitmap
End Sub
 
After messing with the code you gave me I came up with this:

VB.NET:
Private Sub pb1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles pb1.DragDrop
  Try
    Dim filedrops() As String = e.Data.GetData(DataFormats.FileDrop)
    Me.pb1.Image = Icon.ExtractAssociatedIcon(filedrops(0)).ToBitmap
    pb1Loc = CType(e.Data.GetData(DataFormats.FileDrop), Array).GetValue(0).ToString
    Catch ex As Exception
  End Try
End Sub

Private Sub pb1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles pb1.DragEnter
  If (e.Data.GetDataPresent(DataFormats.FileDrop)) Then
    e.Effect = DragDropEffects.Copy
  End If
End Sub

And it works nicely, Thanks again!

JimmyTheGreat
 
"filedrops" is the String array containing all paths dropped.
 
Back
Top