VB.NET实现拖放文件到textbox获取文件路径的方式比用文件对话框更方便和炫酷一些。下面是实现的代码。

当然首先要开启拖放允许:

 TextBox1.AllowDrop = True

然后是响应DragEnter事件,其代码如下所示:

Private Sub TextBoxDragEnter(sender As Object, e As DragEventArgs) Handles TextBox1.DragEnter
        If e.Data.GetDataPresent(DataFormats.FileDrop) Then
            Dim files As String()
            Try
                files = CType(e.Data.GetData(DataFormats.FileDrop), String())
                Me.TextBox1.Text = files(files.Length - 1)
            Catch ex As Exception
                MessageBox.Show(ex.Message)
                Return
            End Try
        End If
 End Sub

OK,可以试一下了^_^