+92 332 4229 857 99ProjectIdeas@Gmail.com

Form drag drop event (Vb.net)

Form Drag Drop Event

Form Drag Drop is an event which is fired when some thing is dragged onto the form.
Below is the example, which displays a picture which is dragged onto the form.

Source code for Form Drag Drop Event in Vb.net

Private Sub Form1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Me.DragDrop

 If e.Data.GetDataPresent(DataFormats.FileDrop) Then
   Dim pictures() As String
   pictures = e.Data.GetData(DataFormats.FileDrop)
   Dim img As Image
   img = Image.FromFile(pictures(0))
   Me.BackgroundImage = img
 End If

End Sub

pictures = e.Data.GetData(DataFormats.FileDrop)
This line of code is getting the file(s) which is dropped onto the form and saving the path(s) of the dropped file(s).
img = Image.FromFile(pictures(0))
This line of code is getting the picture from the path of the dropped file(s).
If have dropped only one picture onto your form then the array of strings i.e pictures() will have only one path of picture, and that picture is accessed by pictures(0) i.e which is at 0th index of the pictures() array.
And if you have dropped more than one  picture onto your form, then the array of strings will have more than one index i.e there are more than one dropped file, and that group of pictures can be accessed by the index of the picture() array.
For example, if you have dropped 3 files on your form at the same time then the the pictures() have 3 indexes which is 0,1 and 2. If you want to access the first picture then you will refer to pictures(0), if you want to acces the second picture then you will refer to pictures(1), and for accessing the third picture you will use pictures(2).