drag-drop file form explorer

B

Brian Henry

I haven't worked much with drag/drop but I am trying to make a form that
accepts files to drug onto it from explorer and droped and have the form
know the full path and file name of the files dropped onto it.. does anyone
have any examples of this? thanks
 
R

Robin Tucker

Key phrases are: Setting component property to "AllowDrop=True", handling
DragOver and DragDrop, fetching available formats with GetDataPresent and
finally getting the data with GetData.

Example below:




Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents ListBox1 As System.Windows.Forms.ListBox
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.ListBox1 = New System.Windows.Forms.ListBox
Me.SuspendLayout()
'
'ListBox1
'
Me.ListBox1.AllowDrop = True
Me.ListBox1.Location = New System.Drawing.Point(8, 8)
Me.ListBox1.Name = "ListBox1"
Me.ListBox1.Size = New System.Drawing.Size(608, 238)
Me.ListBox1.TabIndex = 0
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(624, 253)
Me.Controls.Add(Me.ListBox1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub ListBox1_DragOver(ByVal sender As Object, ByVal e As
System.Windows.Forms.DragEventArgs) Handles ListBox1.DragOver
If e.Data.GetDataPresent("FileDrop") Then
e.Effect = DragDropEffects.Copy
Else
e.Effect = DragDropEffects.None
End If
End Sub

Private Sub ListBox1_DragDrop(ByVal sender As Object, ByVal e As
System.Windows.Forms.DragEventArgs) Handles ListBox1.DragDrop
If e.Data.GetDataPresent("FileDrop") Then
ListBox1.Items.Clear()

Dim theFiles() As String = CType(e.Data.GetData("FileDrop", True), String())
For Each theFile As String In theFiles
ListBox1.Items.Add(theFile)
Next
End If
End Sub
End Class
 
B

Brian Henry

thanks both of you thats a big help, just one last question... how do you
generally find out what type of data is being dropped when you do a drag
drop? i know you know that this is a "FileDrop" bit of data, but if i was to
like drag a message from outlook into my app, how would i find out what the
data type was from that drop event?
 
A

Armin Zingler

Brian said:
thanks both of you thats a big help, just one last question... how do you
generally find out what type of data is being dropped when you do a drag
drop? i know you know that this is a "FileDrop" bit of data, but if i was to
like drag a message from outlook into my app, how would i find out what the
data type was from that drop event?

In DragEnter:

Debug.WriteLine(String.Join(vbCrLf, e.Data.GetFormats))


Armin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top