Drag and Drop

 Drag and Drop

Providing your users with Drag and Drop options will make your applications more user friendly.

The ability to drag and drop is common for Windows  applications.   Those users who prefer mouse operations to the  keyboard expect to be able to drag and drop items both within an application and  between applications.

Dragging and dropping helps to reduce errors and speeds up  certain types of user actions.  It is particularly important for those  users that prefer the mouse to the keyboard.   The example provided  here covers one of the most common uses of drag and drop – dragging a collection  of items from Windows Explorer – but it includes an extra twist to demonstrate  one techinque for controlling the drop operation.

The example will show how to drop a list of folder (directory)  names onto a listbox.   The code checks that the dragged object is a  list of strings.  It then creates a FileInfo object from each string in the  list, and checks whether it refers to a valid folder.  If it does, it is  added to the list.   If it does not refer to a valid folder, it is  ignored.   This code would enable a user to select all items in an  Explorer window, drop them onto the list box, and have the folders (only)  listed.

Note that the DragEnter method code simply checks that there  is something to be dropped, and sets the effects to All.   As nothing  is going to be copied or moved, it doesn’t matter at this stage whether the  effect is Copy or Move – if this is important, the effect should be set  appropriately at this point.     The DragDrop method then  gets the list of strings, and creates a DirectoryInfo object from each one.   Note that the DirectoryInfo object simply requires a non-null string – the  subsequent check with the .Exists method is required to separate the filenames  from the folder names.  Only the foldernames are added to the list.

Private Sub ListBox1_DragEnter(ByVal sender As Object,  ByVal e As _
      System.Windows.Forms.DragEventArgs) Handles  ListBox1.DragEnter
   If e.Data.GetDataPresent(DataFormats.FileDrop) Then
     e.Effect = DragDropEffects.All
   End If
 End Sub
Private Sub ListBox1_DragDrop(ByVal sender As Object, ByVal e As _
      System.Windows.Forms.DragEventArgs) Handles  ListBox1.DragDrop
   If e.Data.GetDataPresent(DataFormats.FileDrop) Then
     Dim MyFiles() As String     ' Assign the files to an array.
     MyFiles = e.Data.GetData(DataFormats.FileDrop)
     ' Loop through the array and add the files to the list.
     For Each F As String In MyFiles
       Dim di As DirectoryInfo = New DirectoryInfo(F)
       If di.Exists Then
       End If
   End If
 End Sub


  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s