VB.NET•The DragDrop Event is Key

Listing 1. The DragDrop event of the PictureBox control is where you write the code that interacts with the data the user drops onto the PictureBox. You can use the members of the DragEventArgs class to determine the type of data dropped, and you can retrieve the data once it's dropped.

Private Sub pb_DragDrop _
   (ByVal sender As System.Object, _
   ByVal e As DragEventArgs) _
   Handles pb.DragDrop

   ' Check the DataFormat of the 
   ' data being dropped
   If e.Data.GetDataPresent _
      (DataFormats.FileDrop) Then

      ' Create a string array, and call
      ' the GetData method to load 
      ' the item(s) into the string array
      Dim droppedFiles() As String
      droppedFiles = e.Data.GetData _

      ' Enumerate the array of strings
      For Each file As String In droppedFiles
         ' Get the last 3 characters of the 
         ' file extension 
         Dim s As String
         s = file.Substring _
            (file.Length - 3, 3)

         ' Make sure we accept only certain
         ' Image extensions for saving
         Select Case s.ToLower
            Case "jpg", "tif", "png", _
               ' Save the image to the 
               ' database
            Case Else
               ' Show an error to the 
               ' user
         End Select

   End If

   ' Reload the Listbox images
End Sub