選取數個檔案,拖拉、放置後,列成檔案清單

讓使用者能夠使用滑鼠左鍵按著不放,
拖拉想要的檔案到指定的清單中,
放開滑鼠左鍵後,自動列出檔案的絕對路徑


拖拉檔案



列出檔案路徑

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        With TextBox1
            .Multiline = True
            .AllowDrop = True
            .ScrollBars = ScrollBars.Vertical
            .Height = 200
        End With
    End Sub
    Private Sub TextBox1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TextBox1.DragEnter
        '限定拖拉的檔案類型
        'Ref. GetDataPresent:http://msdn.microsoft.com/query/dev10.query?appId=Dev10IDEF1&l=ZH-TW&k=k(SYSTEM.WINDOWS.FORMS.IDATAOBJECT.GETDATAPRESENT)&rd=true
        If e.Data.GetDataPresent(DataFormats.FileDrop, False) = True Then e.Effect = DragDropEffects.All
    End Sub
    Private Sub TextBox1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TextBox1.DragDrop
        'Ref. DirectCast: http://msdn.microsoft.com/query/dev10.query?appId=Dev10IDEF1&l=ZH-TW&k=k(VB.DIRECTCAST)&rd=true
        'Ref. FileDrop: http://msdn.microsoft.com/zh-tw/library/system.windows.forms.dataformats.filedrop.aspx
        Dim DragDropFiles() As String = DirectCast(e.Data.GetData(DataFormats.FileDrop), String())
        TextBox1.Text = Nothing
        For i As Integer = 0 To DragDropFiles.Length - 1
            TextBox1.Text += DragDropFiles(i) & vbCrLf
        Next
    End Sub

沒有留言:

張貼留言