ScottishTapWater
ScottishTapWater

Reputation: 4806

How to get the filepath of an image dropped onto a button in VB

I am attempting to get the filepath of an image that is dropped onto a button in VB. I have set the button to allow drop and I have set up this segment to hold the event code:

Private Sub btnDropZone_drop(sender As Object, e As EventArgs) Handles btnDropZone.DragDrop

    End Sub

How would I go about getting the filepath from this event as a string? Thanks in advance!

Upvotes: 1

Views: 93

Answers (1)

A button is a pretty small drop target, but it would work the same as anything else: examine e As DragEventArgs:

Private Sub btnDropZone_drop(sender As Object, 
      e As EventArgs) Handles btnDropZone.DragDrop

    'ToDo check the format to see what was dropped
    Dim Files As String() = CType(e.Data.GetData(DataFormats.FileDrop), String())

    For Each s As String In Files
        ListBox1.Items.Add(Path.GetFileName(s))
    Next
End Sub

When you are trying to figure out what to do in an event, always look up the meaning and contents of the eventargs

Upvotes: 1

Related Questions