Monday, March 24, 2014

Webbrowser control drag drop Multiple Files

In WebBrowser control of .NET Winform, there is no drag & drop event. But we had a requirement to catch the dropped files. By using beforenavigate 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 dragdrop event will be fired, from where we can get all the files. But there is no disable property for Webbrowser control. To overcome that, place the webbrowser control inside a Panel & disable the panel, so that the webbrowser 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 webbrowser control in the “Panel1″ panel control.
2. Applied the following code in your 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

No comments: