Webbrowser control drag drop Multiple Files

 

In Web Browser control of .NET Winform, there is no drag & drop event. But we had a requirement to catch the dropped files. By using before navigate event, we can catch only one dropped file and not able to pick up all the dropped files.

The simple trick is to disable the Web browser control while dropping, so that the form drag drop event will be fired, from where we can get all the files. But there is no disable property for Web browser control. To overcome that, place the web browser control inside a Panel & disable the panel, so that the web browser control will also be disabled.

Next question will be, When to disable the Panel control ?
Form Activate / Deactivate, since while we start dragging a file obviously the form will be deactivated & we can disable the Panel. On(after) drop, the form will be activated & can enable the Panel control.

The code is :
1. In Form1, placed the web browser control in the “Panel1” panel control.
2. Applied the following code in form

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
     Me.AllowDrop = True
End Sub

Private Sub Form1_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Me.DragDrop
     Dim files() As String = e.Data.GetData(DataFormats.FileDrop)
     For Each filePath In files
          MsgBox(filePath)
     Next
End Sub

Private Sub Form1_DragEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Me.DragEnter
     If e.Data.GetDataPresent(DataFormats.FileDrop) Then
          e.Effect = DragDropEffects.Copy
     End If
End Sub

Private Sub Form1_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated
     ' To Enable Web Browser control
     Panel1.Enabled = True
End Sub

Private Sub Form1_Deactivate(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Deactivate
     ' To Disable Web Browser control
     Panel1.Enabled = False
End Sub
Durai Prasanna
Latest posts by Durai Prasanna (see all)