Monday, October 10, 2011

Suppress WPF Webbrowser Control Script Errors VB.NET

Since the WPF browser control has no suppress script error function and adding a windows form host is just overhead I found a solution in C# and posting the VB version here.

'Add this sub

 Public Sub HideScriptErrors(wb As WebBrowser, Hide As Boolean)
        Dim fiComWebBrowser As FieldInfo = GetType(WebBrowser).GetField("_axIWebBrowser2"BindingFlags.Instance Or BindingFlags.NonPublic)
        If fiComWebBrowser Is Nothing Then
            Return
        End If
        Dim objComWebBrowser As Object = fiComWebBrowser.GetValue(wb)
        If objComWebBrowser Is Nothing Then
            Return
        End If
        objComWebBrowser.[GetType]().InvokeMember("Silent"BindingFlags.SetProperty, Nothing, objComWebBrowser, New Object() {Hide})
    End Sub
 
In WebBrowser1 (or whatever the name of your browser control is)
add this to the Navigated event

 HideScriptErrors(WebBrowser1True)
 
It should look as such

Private Sub WebBrowser1_Navigated(sender As Object, e As System.Windows.Navigation.NavigationEventArgsHandles WebBrowser1.Navigated
        HideScriptErrors(WebBrowser1, True)
End Sub