【问题标题】:how to drag a file and get path of it如何拖动文件并获取它的路径
【发布时间】:2013-08-19 19:53:46
【问题描述】:

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

谢谢!

【问题讨论】:

  • 我投赞成票并不意味着这很好,但我不同意有人想投反对票。

标签: vb.net visual-studio visual-studio-2008


【解决方案1】:

我在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

【讨论】:

  • 但是根据你的需要改变这个:“Handles ListBox1.DragDrop”
  • @AbhinavGauniyal Opps,对不起。我是 C# 程序员,没有注意到 Handles ListBox1.DragDrop,因为在 C# 中我们不使用它。
猜你喜欢
  • 2012-07-26
  • 2016-01-18
  • 2021-02-07
  • 2016-10-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多