Visual Basic Development Bookmark and Share   
 Home > Visual Basic Language > Drag n Drop text file to textbox in VB.NET ?
 

Drag n Drop text file to textbox in VB.NET ?

How do i Drag n Drop text file (.txt or any hoter format ,vbs,bat etc...)to atextbox in VB.NET ?
Step by step tutorial with code so i can copy/paste would be nice.
Thanks in advance!
FFA702  Thursday, November 26, 2009 10:11 PM
You need to process the drop action, get the filename of the dropped fileand read the file contents into the text box.

There is an example of dropping files here:
http://msdn.microsoft.com/en-us/library/aa289508(VS.71).aspx?ppud=4#vbtchimpdragdropanchor6

and a full example here:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.dodragdrop.aspx

Use My.Computer.FileSystem to read the text file into the textbox. See here:
http://msdn.microsoft.com/en-us/library/a77w6kkx(VS.80).aspx


Acamar  Friday, November 27, 2009 12:15 AM
You need to take the two subroutines listed there and convert them to use your form and your text box instead of a listbox, by changing the control namesfrom listbox1 to whatever your textbox is called, and you need to replace the lines that insert the filenames into the listbox with the line from the third example that copies a file contents into a text box. If the textbox for displaying the file contents was TextBoxDrop and the drop should be triggered when the cursor move onto the textbox (not onto the form, as per your description) then the code is:

    Private Sub TextBoxDrop_DragEnter(ByVal sender As Object, _
                ByVal e As System.Windows.Forms.DragEventArgs) _
                Handles TextBoxDrop.DragEnter
        If e.Data.GetDataPresent(DataFormats.FileDrop) Then
            e.Effect = DragDropEffects.All
        End If
    End Sub

    Private Sub TextBoxDrop_DragDrop(ByVal sender As Object, _
                ByVal e As System.Windows.Forms.DragEventArgs) _
                Handles TextBoxDrop.DragDrop
        If e.Data.GetDataPresent(DataFormats.FileDrop) Then
            Dim MyFiles() As String
            ' Assign the files to an array.
            MyFiles = e.Data.GetData(DataFormats.FileDrop)
            ' Display the file Name
            'TextBoxDrop.Text = MyFiles(0)
            ' Display the file contents
            TextBoxDrop.Text = My.Computer.FileSystem.ReadAllText(MyFiles(0))
        End If
    End Sub


Acamar  Friday, November 27, 2009 3:07 AM
How do i Drag n Drop text file (.txt or any hoter format ,vbs,bat etc...)to atextbox in VB.NET ?
Step by step tutorial with code so i can copy/paste would be nice.
Thanks in advance!

in your textbox properties select allowDrop = True
Don't judge me, just Upgrade me. Thanks!
Malange  Thursday, November 26, 2009 10:30 PM
Erm....I did that but noting....
I need to open a text file by draging it to my textbox...
FFA702  Thursday, November 26, 2009 11:23 PM
You need to process the drop action, get the filename of the dropped fileand read the file contents into the text box.

There is an example of dropping files here:
http://msdn.microsoft.com/en-us/library/aa289508(VS.71).aspx?ppud=4#vbtchimpdragdropanchor6

and a full example here:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.dodragdrop.aspx

Use My.Computer.FileSystem to read the text file into the textbox. See here:
http://msdn.microsoft.com/en-us/library/a77w6kkx(VS.80).aspx


Acamar  Friday, November 27, 2009 12:15 AM

I dony understand tose exemple,they dont help me....its simple,drag a file(from a folder or from the deskup...) and drop it in my form,and the text get loaded in my textbox.

EDIT:The part i dont understand is:how do i get the path of the file i drag'n'drop?

  • Edited byFFA702 Friday, November 27, 2009 2:43 AMmissed information
  •  
FFA702  Friday, November 27, 2009 1:13 AM
You need to take the two subroutines listed there and convert them to use your form and your text box instead of a listbox, by changing the control namesfrom listbox1 to whatever your textbox is called, and you need to replace the lines that insert the filenames into the listbox with the line from the third example that copies a file contents into a text box. If the textbox for displaying the file contents was TextBoxDrop and the drop should be triggered when the cursor move onto the textbox (not onto the form, as per your description) then the code is:

    Private Sub TextBoxDrop_DragEnter(ByVal sender As Object, _
                ByVal e As System.Windows.Forms.DragEventArgs) _
                Handles TextBoxDrop.DragEnter
        If e.Data.GetDataPresent(DataFormats.FileDrop) Then
            e.Effect = DragDropEffects.All
        End If
    End Sub

    Private Sub TextBoxDrop_DragDrop(ByVal sender As Object, _
                ByVal e As System.Windows.Forms.DragEventArgs) _
                Handles TextBoxDrop.DragDrop
        If e.Data.GetDataPresent(DataFormats.FileDrop) Then
            Dim MyFiles() As String
            ' Assign the files to an array.
            MyFiles = e.Data.GetData(DataFormats.FileDrop)
            ' Display the file Name
            'TextBoxDrop.Text = MyFiles(0)
            ' Display the file contents
            TextBoxDrop.Text = My.Computer.FileSystem.ReadAllText(MyFiles(0))
        End If
    End Sub


Acamar  Friday, November 27, 2009 3:07 AM
Thanks alot.
FFA702  Friday, November 27, 2009 12:18 PM
Thanks alot.

did it work? acamar e.g? if so propose as answered please

Don't judge me, just Upgrade me. Thanks!
Malange  Friday, November 27, 2009 3:29 PM

You can use google to search for other answers

Custom Search

More Threads

• Encrypting data using a binary formatter
• Rounding Numbers in a String
• Shortcut key
• problem in Create a random number
• Unable to keep records in a database
• VBE 2005 - Don't know how/where to specify the argument for this - don't even know what that means...?
• NullReferenceException when using DirectCast
• VB.Net Name Change
• FileNotFound Exception
• Windows Service OnStart event