0

如何在 Winform 中实现 Panel,当用户在其上拖动文件(一个简单的 .txt 文件)时,它应该接受它并将其路径存储到一些名为 filepathname 等的变量中,这些变量可以更早地使用。我可以找到有关如何实现拖放的示例,但找不到有关如何获取路径并将其存储以供稍后在程序中使用的示例。使用:Visual Studio 2008 - Vb.net

谢谢!

4

1 回答 1

2

我在这个 MSDN 页面中找到了这个 下面的代码是修改过的

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
于 2013-08-17T14:22:22.940 回答