Console Application Sample

Imports System.Threading
Imports DotNetBrowser

Module Module1

    Sub Main()
        Dim browser As Browser
        browser = BrowserFactory.Create()
        browser.DownloadHandler = New SampleDownloadHandler
        DownloadFile(browser, "http://www.7-zip.org/a/7z1604.exe")
        DownloadFile(browser, "http://www.7-zip.org/a/7z1604-src.7z")
        Console.In.ReadLine()
        browser.Dispose()
    End Sub

    Class SampleDownloadHandler
        Implements DownloadHandler

        Public Event AllowDownloadCalled(download As DownloadItem)

        Public Function AllowDownload(download As DownloadItem) As Boolean Implements DownloadHandler.AllowDownload
            AddHandler download.DownloadEvent, Sub(s, e)
                                                   If e.Item.Completed Then
                                                       Console.WriteLine("Downloaded: " + e.Item.DestinationFile)
                                                   End If
                                               End Sub

            Console.WriteLine("Download allowed: " + download.URL)
            RaiseEvent AllowDownloadCalled(download)
            Return True
        End Function
    End Class

    Private Sub DownloadFile(browser As Browser, url As String)
        Dim downloadHandler As SampleDownloadHandler
        downloadHandler = browser.DownloadHandler

        Dim downloadEvent As New ManualResetEvent(False)
        Dim allowDownloadEvent As New SampleDownloadHandler.AllowDownloadCalledEventHandler(Sub(d As DownloadItem) downloadEvent.Set())

        AddHandler downloadHandler.AllowDownloadCalled, allowDownloadEvent
        browser.LoadURL(url)
        downloadEvent.WaitOne()
        RemoveHandler downloadHandler.AllowDownloadCalled, allowDownloadEvent
    End Sub
End Module

WinForms Sample

Imports System.Threading
Imports DotNetBrowser

Public Class Form1
    Private browser As Browser
    Private downloadHandler As SampleDownloadHandler

    Public Sub New()
        InitializeComponent()

        downloadHandler = New SampleDownloadHandler

        AddHandler downloadHandler.AllowDownloadCalled, AddressOf AllowDownloadEvent
        AddHandler downloadHandler.DownloadUpdated, AddressOf DownloadUpdatedEvent
        AddHandler downloadHandler.DownloadCompleted, AddressOf DownloadCompletedEvent

        browser = BrowserFactory.Create()
        browser.DownloadHandler = downloadHandler
    End Sub

    Private Sub AllowDownloadEvent(download As DownloadItem)
        Dim url = download.URL
        If Me.InvokeRequired Then
            Me.BeginInvoke(Sub() StatusBox.AppendText("Download started: " + url + Environment.NewLine))
        End If
    End Sub

    Private Sub DownloadUpdatedEvent(download As DownloadItem)
        Dim url = download.URL
        Dim complete = download.PercentComplete
        If Me.InvokeRequired Then
            Me.BeginInvoke(Sub() StatusBox.Text += "Download updated: " + complete.ToString + " percent complete (" + url + ")" + Environment.NewLine)
        End If
    End Sub

    Private Sub DownloadCompletedEvent(download As DownloadItem)
        Dim url = download.URL
        If Me.InvokeRequired Then
            Me.BeginInvoke(Sub() StatusBox.Text += "Download completed: " + url + Environment.NewLine)
        End If
    End Sub

    Private Sub LoadButton_Click(sender As Object, e As EventArgs) Handles LoadButton.Click
        browser.LoadURL(URLTextBox.Text)

        'Testing several files download. Replace the LoadURL() call above with the commented code below to try.
        'Task.Run(Sub() DownloadFiles("http://www.7-zip.org/a/7z1604.exe", "http://www.7-zip.org/a/7z1604-src.7z", URLTextBox.Text))

    End Sub

    Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
        If Not browser Is Nothing Then
            browser.Dispose()
        End If
    End Sub

    Private Sub DownloadFiles(ParamArray urls() As String)
        For Each url As String In urls
            DownloadFile(url)
        Next
    End Sub

    Private Sub DownloadFile(url As String)

        Dim downloadEvent As New ManualResetEvent(False)
        Dim allowDownloadEvent As New SampleDownloadHandler.AllowDownloadCalledEventHandler(Sub(d As DownloadItem) downloadEvent.Set())

        AddHandler downloadHandler.AllowDownloadCalled, allowDownloadEvent
        browser.LoadURL(url)
        downloadEvent.WaitOne()
        RemoveHandler downloadHandler.AllowDownloadCalled, allowDownloadEvent
    End Sub

    Class SampleDownloadHandler
        Implements DownloadHandler

        Public Event AllowDownloadCalled(download As DownloadItem)
        Public Event DownloadUpdated(download As DownloadItem)
        Public Event DownloadCompleted(download As DownloadItem)

        Public Function AllowDownload(download As DownloadItem) As Boolean Implements DownloadHandler.AllowDownload
            AddHandler download.DownloadEvent, Sub(s, e)
                                                   RaiseEvent DownloadUpdated(e.Item)

                                                   If e.Item.Completed Then
                                                       RaiseEvent DownloadCompleted(e.Item)
                                                   End If
                                               End Sub
            RaiseEvent AllowDownloadCalled(download)
            Return True
        End Function
    End Class

End Class