Click to See Complete Forum and Search --> : Drag and drop file's content into text box


biot
November 26th, 2008, 10:53 PM
Hi
I'm trying to find a routine that enables the user to drag and drop a text file from the desktop (or any other location on the hard-drive) into a text box, and the text box will display the text file's content.
Any suggestions?

HanneSThEGreaT
November 27th, 2008, 12:33 AM
Quite easy :)

I'll assume you have a Textbox named TextBox1 and with its Multiline property set to True, and Scrollbars set to Both

Add this code ontop of your form, above everything else! :
Imports System.io 'Import IO Namespace when working with files

Then, add these events :
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TextBox1.AllowDrop = True 'Allow Drop on the textbox
End Sub

Private Sub TextBox1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TextBox1.DragDrop
If e.Data.GetDataPresent(DataFormats.FileDrop) Then 'get Data Present
Dim FileName As String() = CType(e.Data.GetData(DataFormats.FileDrop), String()) 'Obtain FileName
For Each FileContents As String In FileName 'Loop through each file

If File.Exists(FileContents) Then 'If it exists
Dim tr As TextReader = New StreamReader(FileContents) 'TextReader object to read contents
TextBox1.Text = tr.ReadToEnd() 'read contents of file

End If

Next FileContents
End If
End Sub

Private Sub TextBox1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TextBox1.DragEnter
If e.Data.GetDataPresent(DataFormats.FileDrop) Then
e.Effect = DragDropEffects.Copy 'Just copy
Else
e.Effect = DragDropEffects.None
End If
End Sub

Here we just used the TextReader, to read the contents of the file. With pictures, it is basically the same principle, you just need to get the Picture's file name, and set the Picturebox's Image to that Imae file - but that's a different story :p

I hope my advice was useful :)

biot
December 1st, 2008, 12:20 AM
Hi
Thanks for the reply, your suggestion did the job...

Another question: I have files compressed in zip format.
I'm trying to drag&drop a file located inside this zip file using winzip's gui, into my app, but the drag & drop operation isn't being recognized.
Does it suppose to work differently from your suggestion??

Thanks again.

HairyMonkeyMan
December 1st, 2008, 03:58 AM
When you drag-drop from a zip file, it is looking for a location to unzip the file to.

You would need extra code in this part to handle this:
If e.Data.GetDataPresent(DataFormats.FileDrop) Then 'get Data Present
Dim FileName As String() = CType(e.Data.GetData(DataFormats.FileDrop), String()) 'Obtain FileName
* For Each FileContents As String In FileName 'Loop through each file

If File.Exists(FileContents) Then 'If it exists
Dim tr As TextReader = New StreamReader(FileContents) 'TextReader object to read contents
TextBox1.Text = tr.ReadToEnd() 'read contents of file

End If

Next FileContents
End If

I would breakpoint at * to see what is coming from the drop of the zip file.

You probably have to unzip the file before you can display it.

biot
December 1st, 2008, 04:36 AM
It doesn't even enter to the DragDrop Sub...