How to Drag/Drop an External File to the ListView Using Visual Basic 2008

Today, I will teach you how to drag/drop an external file to the ListView by using Visual Basic 2008. You can drag/drop the external images that you like in the ListView and you can also preview the images that you have selected in the ListView. Let’s begin: Open Visual Basic 2008, create a new Windows Application and drag a ListView and a PictureBox and do the Form just like this. First Form Double click the Form and declares all the variable that are needed above the Form1_Load.
  1. Private lstviewItem As ListViewItem 'MAKE THE LISTVIEWITEM OBJECT
  2. Private lstviewItemImageList As New ImageList 'MAKE THE IMAGELIST OBJECT
After that, do the following code to fire it on the first load.
  1. Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
  2. 'TO OUR IMAGELIST OBJECT, SET THE IMAGELIST FOR THE LISTVIEW
  3. ListView1.SmallImageList = lstviewItemImageList
  4.  
  5. With ListView1
  6. 'SET THE AllowDrop PROPERTIES OF THE LISTVIEW
  7. .AllowDrop = True
  8. 'SET THE View PROPERTIES OF THE LISTVIEW
  9. .View = View.Details
  10. 'ADD A COLUNMS TO THE LISTVIEW
  11. .Columns.Add("File")
  12. 'SET THE WIDTH OF THE COLUMN THAT FITS TO THE SIZE OF THE LISTVIEW
  13. ListView1.Columns(0).Width = 150
  14. End With
  15.  
  16. 'SET THE SizeMode PROPERTIES OF THE PICTUREBOX
  17. PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
  18. End Sub
Then, do this following code for creating a method of the drag and drop events in the ListView.
  1. Private Sub listviewDragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ListView1.DragDrop
  2. Try
  3. 'SET AN ARRAY VARIABLE
  4. Dim file As Array
  5. 'GET AN ARRAY OF THE FILES THAT ARE BEING DRAGGED IN
  6. file = CType(e.Data.GetData(DataFormats.FileDrop), Array)
  7.  
  8. 'LOOP THE ARRAY FILE
  9. For i As Integer = 0 To file.Length - 1
  10. lstviewItem = New ListViewItem(file.GetValue(i).ToString)
  11. Try
  12.  
  13. 'ADDING THE IMAGE TO THE IMAGELIST OBJECT. AND
  14. 'SET THE NEWLY ADDED IMAGE IN THE LISTVIEWITEMS IMAGEINDEX.
  15. lstviewItem.ImageIndex = lstviewItemImageList.Images.Add(Image.FromFile(lstviewItem.Text), Color.Transparent)
  16.  
  17. 'ADDING THE LISTVIEWITEM TO LISTVIEW
  18. ListView1.Items.Add(lstviewItem)
  19. Catch ome As OutOfMemoryException
  20. 'WHEN THE IMAGE.FROMFILE METHOD FAILES THE ERROR WILL OCCURS
  21. '(WHEN YOU DRAG A NONREADABLE IMAGE FILE)
  22.  
  23. MsgBox("Not valid image file: " & lstviewItem.Text)
  24. Catch ex As Exception
  25. 'CATCH ANY ERRORS
  26. MsgBox("Error: " & ex.Message)
  27. End Try
  28. Next
  29. Catch ex As Exception
  30. 'CATCH ANY ERRORS IN THE SUB
  31. MsgBox("Error: " & ex.Message)
  32. End Try
  33. End Sub
Lastly, do this following code for the selecting events of the ListView. When you select the image in the ListView, it will then preview in the PictureBox.
  1. Private Sub listview1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView1.SelectedIndexChanged
  2.  
  3. If ListView1.SelectedItems.Count = 0 Then Return
  4.  
  5. Try
  6.  
  7. 'WHEN THE IMAGE FILE AND PATH IS SELECTED
  8. 'IT WILL LOAD TO THE PICTURBOX SO THAT THE IMAGE WILL PREVIEW
  9. PictureBox1.Image = Image.FromFile(ListView1.SelectedItems(0).Text)
  10. Me.Text = ListView1.SelectedItems(0).Text
  11. Catch ome As OutOfMemoryException
  12. 'THIS ERROR OCCURS WHEN THE SELECTED IMAGE IS NOT READABLE
  13. MsgBox("Could not open preview: " & ListView1.SelectedItems(0).Text)
  14. Catch ex As Exception
  15. 'CATCH ANY ERRORS
  16. MsgBox("Error: " & ex.Message)
  17. End Try
  18. End Sub
Output: Output

Add new comment