470,630 Members | 2,115 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,630 developers. It's quick & easy.

drag-drop file form explorer

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
Nov 21 '05 #1
5 9940
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

"Brian Henry" <no****@nospam.com> wrote in message
news:O9**************@tk2msftngp13.phx.gbl...
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

Nov 21 '05 #2
"Brian Henry" <no****@nospam.com> schrieb:
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


How To Provide File Drag-and-Drop Functionality in a Visual Basic .NET
Application
<URL:http://support.microsoft.com/?scid=kb;EN-US;306969>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
Nov 21 '05 #3
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?

"Robin Tucker" <id*************************@reallyidont.com> wrote in
message news:db*******************@news.demon.co.uk...
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

"Brian Henry" <no****@nospam.com> wrote in message
news:O9**************@tk2msftngp13.phx.gbl...
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


Nov 21 '05 #4
Brian Henry schrieb:
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
Nov 21 '05 #5
On Wed, 13 Jul 2005 08:47:36 -0400, "Brian Henry" <no****@nospam.com>
wrote:
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


Here a fantastic introduction to drag and drop <grin>:

http://www.codeproject.com/vb/net/Tr...ragAndDrop.asp

Hope this helps

Tom
Nov 21 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Jon Cosby | last post: by
1 post views Thread by Chris | last post: by
2 posts views Thread by vunet.us | last post: by
reply views Thread by =?Utf-8?B?RGF2ZQ==?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.