• We just launched and are currently in beta. Join us as we build and grow the community.

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

Nando1l

Red Team Member
N Rep
0
0
0
Rep
0
N Vouches
0
0
0
Vouches
0
Posts
68
Likes
122
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
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.

firstform_11.png


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. With

    ListView1
  5. 'SET THE AllowDrop PROPERTIES OF THE LISTVIEW
  6. .

    AllowDrop

    =

    True
  7. 'SET THE View PROPERTIES OF THE LISTVIEW
  8. .

    View

    =

    View.

    Details
  9. 'ADD A COLUNMS TO THE LISTVIEW
  10. .

    Columns

    .

    Add

    (

    "File"

    )
  11. 'SET THE WIDTH OF THE COLUMN THAT FITS TO THE SIZE OF THE LISTVIEW
  12. ListView1.

    Columns

    (

    0

    )

    .

    Width

    =

    150
  13. End

    With

  14. 'SET THE SizeMode PROPERTIES OF THE PICTUREBOX
  15. PictureBox1.

    SizeMode

    =

    PictureBoxSizeMode.

    StretchImage
  16. 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. 'LOOP THE ARRAY FILE
  8. For

    i As

    Integer

    =

    0

    To

    file.

    Length

    -

    1
  9. lstviewItem =

    New

    ListViewItem(

    file.

    GetValue

    (

    i)

    .

    ToString

    )
  10. Try

  11. 'ADDING THE IMAGE TO THE IMAGELIST OBJECT. AND
  12. 'SET THE NEWLY ADDED IMAGE IN THE LISTVIEWITEMS IMAGEINDEX.
  13. lstviewItem.

    ImageIndex

    =

    lstviewItemImageList.

    Images

    .

    Add

    (

    Image.

    FromFile

    (

    lstviewItem.

    Text

    )

    , Color.

    Transparent

    )

  14. 'ADDING THE LISTVIEWITEM TO LISTVIEW
  15. ListView1.

    Items

    .

    Add

    (

    lstviewItem)
  16. Catch

    ome As

    OutOfMemoryException
  17. 'WHEN THE IMAGE.FROMFILE METHOD FAILES THE ERROR WILL OCCURS
  18. '(WHEN YOU DRAG A NONREADABLE IMAGE FILE)

  19. MsgBox

    (

    "Not valid image file: "

    &

    lstviewItem.

    Text

    )
  20. Catch

    ex As

    Exception
  21. 'CATCH ANY ERRORS
  22. MsgBox

    (

    "Error: "

    &

    ex.

    Message

    )
  23. End

    Try
  24. Next
  25. Catch

    ex As

    Exception
  26. 'CATCH ANY ERRORS IN THE SUB
  27. MsgBox

    (

    "Error: "

    &

    ex.

    Message

    )
  28. End

    Try
  29. 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. If

    ListView1.

    SelectedItems

    .

    Count

    =

    0

    Then

    Return

  3. Try

  4. 'WHEN THE IMAGE FILE AND PATH IS SELECTED
  5. 'IT WILL LOAD TO THE PICTURBOX SO THAT THE IMAGE WILL PREVIEW
  6. PictureBox1.

    Image

    =

    Image.

    FromFile

    (

    ListView1.

    SelectedItems

    (

    0

    )

    .

    Text

    )
  7. Me

    .

    Text

    =

    ListView1.

    SelectedItems

    (

    0

    )

    .

    Text
  8. Catch

    ome As

    OutOfMemoryException
  9. 'THIS ERROR OCCURS WHEN THE SELECTED IMAGE IS NOT READABLE
  10. MsgBox

    (

    "Could not open preview: "

    &

    ListView1.

    SelectedItems

    (

    0

    )

    .

    Text

    )
  11. Catch

    ex As

    Exception
  12. 'CATCH ANY ERRORS
  13. MsgBox

    (

    "Error: "

    &

    ex.

    Message

    )
  14. End

    Try
  15. End

    Sub

Output:

ouput.png


 

452,292

323,340

323,349

Top