Drag Drop A File Into TextBox
October 21, 2010 0 CommentsThis code snippet shows how to drag and drop a file into a textbox and open the file for reading using the StreamReader class.
Public Class Form1
Dim txtData
As New TextBox()
Private
Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
MyBase.Load
txtData.Dock = DockStyle.Fill
txtData.Multiline = True
txtData.AllowDrop = True
AddHandler txtData.DragDrop,
AddressOf
txtData_DragDrop
AddHandler txtData.DragEnter,
AddressOf
txtData_DragEnter
Me.Controls.Add(txtData)
End Sub
Private
Sub
txtData_DragDrop(ByVal sender
As System.Object, ByVal e As
System.Windows.Forms.DragEventArgs)
If
e.Data.GetDataPresent(DataFormats.FileDrop) Then
Dim Files() As String
Files =
e.Data.GetData(DataFormats.FileDrop)
Dim sReader As New
StreamReader(Files(0))
txtData.Text =
sReader.ReadToEnd()
End
If
End Sub
Private
Sub
txtData_DragEnter(ByVal sender
As System.Object, ByVal e As
System.Windows.Forms.DragEventArgs)
If
e.Data.GetDataPresent(DataFormats.FileDrop) Then
e.Effect =
DragDropEffects.All
End
If
End ...
Subscribe