|
-
November 26th, 2008, 11:53 PM
#1
Drag and drop file's content into text box
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?
-
November 27th, 2008, 01:33 AM
#2
Re: Drag and drop file's content into text box
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! :
Code:
Imports System.io 'Import IO Namespace when working with files
Then, add these events :
Code:
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 
I hope my advice was useful
-
December 1st, 2008, 01:20 AM
#3
Re: Drag and drop file's content into text box
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.
-
December 1st, 2008, 04:58 AM
#4
Re: Drag and drop file's content into text box
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:
Code:
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.
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rich Cook
0100 1101 0110 1001 0110 0011 0110 1000 0110 0001 0110 0101 0110 1100 0010 0000 0100 0101 0110 1100 0110 1100 0110 0101 0111 0010
-
December 1st, 2008, 05:36 AM
#5
Re: Drag and drop file's content into text box
It doesn't even enter to the DragDrop Sub...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|