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

2 comments:

  1. thanks man i was looking for this.

    you rock!

    ReplyDelete
  2. Very nice article. I really enjoyed it reading. And it also cleared lot of my doubts about WPF WebBrowser control. Some other article too helped me lot in completing my task. These article are...
    http://mindstick.com/Articles/3610ee37-4b56-41fd-99b1-c4805b016901/?WebBrowser%20control%20in%20WPF

    http://www.c-sharpcorner.com/uploadfile/dhananjaycoder/web-browser-control-in-wpf/

    http://msdn.microsoft.com/en-us/library/system.windows.controls.webbrowser.aspx

    ReplyDelete