News:

GinGly.com - Used by 85,000 Members - SMS Backed up 7,35,000 - Contacts Stored  28,850 !!

Main Menu

Drag & Drop - Move files from desktop to a textbox

Started by dhilipkumar, Dec 15, 2008, 08:28 PM

Previous topic - Next topic

dhilipkumar

This example moves any files from desktop to a textbox. The example needs a reference to Shell32.dll

  Declarations:

Imports Shell32
Imports System.IO.Path

Code:

Public Class Form1

    Private Structure FileFormat
        Public TypeFormat As String
    End Structure

    Private ff As New FileFormat

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
            Handles MyBase.Load
        With Me
            .CenterToScreen()
            .TextBox1.AllowDrop = True
            .ff.TypeFormat = "FileNameW"
        End With
    End Sub

    Private Function DragAndDrop_MoveLink(ByVal Filename As String) As String
        Const Filetype As String = ".lnk"
        If String.Compare(GetExtension(Filename), Filetype, True) <> 0 Then Return Filename
        Dim shell As Shell32.Shell = New Shell32.Shell
        Dim DirectoryName As Shell32.Folder = shell.NameSpace(GetDirectoryName(Filename))
        Dim Item As Shell32.FolderItem = DirectoryName.Items().Item(GetFileName(Filename))
        Dim LinkObject As Shell32.ShellLinkObject = CType(Item.GetLink, Shell32.ShellLinkObject)
        Return LinkObject.Path
    End Function

    Private Sub TextBox1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) _
            Handles TextBox1.DragDrop
        If ff.TypeFormat = Nothing Then
            Throw New Exception("Error...")
        Else
            If e.Data.GetDataPresent(ff.TypeFormat, False) Then
                Dim Filename As System.Array = CType(e.Data.GetData(ff.TypeFormat, False), System.Array)
                TextBox1.Text = DragAndDrop_MoveLink(CType(Filename.GetValue(0), String))
            End If
        End If
    End Sub

    Private Sub TextBox1_DragOver(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) _
            Handles TextBox1.DragOver
        If ff.TypeFormat = Nothing Then
            Throw New Exception("Error...")
        Else
            If e.Data.GetDataPresent(ff.TypeFormat, False) Then
                e.Effect = DragDropEffects.Copy
            Else
                e.Effect = DragDropEffects.None
            End If
        End If
    End Sub

End Class