3

这是一个我无法上班的例子。我按照说明进行操作,但是当我尝试将文件拖放到表单时,它不允许我,并给了我不可用的光标。

这很容易。只需drap-and-dropAllowDrop属性设置为 True 并处理 DragEnter 和 DragDrop 事件即可启用。在DragEnter事件处理程序中,您可以使用 DataFormats 类检查数据是否属于您想要的类型。在DragDrop事件处理程序中,使用 的 Data 属性DataEventArgs来接收实际数据。

例子:

Private Sub Form1_Load(sender As System.Object, _
                       e As System.EventArgs) _
  Handles MyBase.Load

    Me.AllowDrop = True
End Sub

Private Sub Form1_DragDrop(sender As System.Object, _
                           e As System.Windows.Forms.DragEventArgs) _
  Handles Me.DragDrop

    Dim files() As String = e.Data.GetData(DataFormats.FileDrop)
    For Each path In files
        MsgBox(path)
    Next
End Sub

Private Sub Form1_DragEnter(sender As System.Object, _
                            e As System.Windows.Forms.DragEventArgs) _
  Handles Me.DragEnter

    If e.Data.GetDataPresent(DataFormats.FileDrop) Then
        e.Effect = DragDropEffects.Copy
    End If
End Sub
4

1 回答 1

4
Imports System.IO

Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.AllowDrop = True
    End Sub
    Private Sub Form1_DragDrop(sender As System.Object, e As System.Windows.Forms.DragEventArgs) Handles Me.DragDrop
        Dim theFiles() As String = CType(e.Data.GetData("FileDrop", True), String())
        For Each theFile As String In theFiles
            MsgBox(theFile)
        Next
    End Sub

    Private Sub Form1_DragEnter(sender As System.Object, e As System.Windows.Forms.DragEventArgs) Handles Me.DragEnter
        If e.Data.GetDataPresent(DataFormats.FileDrop) Then
            e.Effect = DragDropEffects.Copy
        End If
    End Sub
End Class
于 2013-02-23T04:48:11.177 回答