求助vb.net拖拽文件到窗体获取文件信息

2025-01-07 05:10:13
推荐回答(1个)
回答1:

这是一个简单的示例,你只需要新建一个项目,并在项目上放置一个文本框:TEXTBOX1,然后,将以下代码复制到代码区:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.AllowDrop = True
End Sub

''建拖曳事件,实现文件拖放
Private Sub Form1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Me.DragEnter
Try
If e.Data.GetDataPresent(DataFormats.FileDrop) = True Then
e.Effect = DragDropEffects.Copy
Else
e.Effect = DragDropEffects.None
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub

Private Sub Form1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Me.DragEnter
Try
Dim filepaht As String() = e.Data.GetData(DataFormats.FileDrop)
For Each File As String In filepaht
textBox1.Text = File
Next
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub

如此,在文本框里将会出现被拖曳文件的路径信息。