如何在Winform中实现Panel,当用户在其上拖动文件(一个简单的.txt文件)时,它应该接受它并将其路径存储到一个名为filepathname等的变量中,这个变量可以在之前使用。我可以找到有关如何实现拖放的示例,但不能找到如何获取路径并将其存储以供稍后在程序中使用的示例。 使用:Visual Studio 2008 - Vb.net
谢谢!
答案 0 :(得分:2)
我在this MSDN page找到了这个 下面的代码是修改后的代码
Private Sub Panel1_DragEnter(ByVal sender As Object, ByVal e As _
System.Windows.Forms.DragEventArgs) Handles Panel1.DragEnter
If e.Data.GetDataPresent(DataFormats.FileDrop) Then
e.Effect = DragDropEffects.All
End If
End Sub
Private Sub Panel1_DragDrop(ByVal sender As Object, ByVal e As _
System.Windows.Forms.DragEventArgs) Handles Panel1.DragDrop
If e.Data.GetDataPresent(DataFormats.FileDrop) Then
Dim MyFiles() As String
Dim i As Integer
' Assign the files to an array.
MyFiles = e.Data.GetData(DataFormats.FileDrop)
'If there are more than one file, set first only
'If you want another restrictment, please edit this.
filepathname = MyFiles(0)
End If
End Sub