Webbrowser Blocking ads RRS feed

  • Question

  • ok well im useing visual basic 2008 and a made a fully functional webbrowser useing the control. i have a huge list of ad servers and every different one is separated by an enter. i need a way for the program to read the file and block ads from loading. please help
    Monday, April 11, 2011 9:18 PM


  • Please implement the BeforeNavigate2 event of the WebBrowser, and you could get the request URL from the event argument, then you could check the URL if it is in the huge list. If it is, you could cancel the navigation.

    A simple sample to block the navigation:

    Imports SHDocVw
    Public Class Form1
      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
      End Sub
      Public Sub New()
        ' This call is required by the designer.
        ' Add any initialization after the InitializeComponent() call.
        AddHandler DirectCast(WebBrowser1.ActiveXInstance, SHDocVw.WebBrowser).BeforeNavigate2, AddressOf WebBrowser1_BeforeNavigate2
      End Sub
      Private Sub WebBrowser1_BeforeNavigate2(ByVal pDisp As Object, ByRef URL As Object, ByRef Flags As Object, ByRef TargetFrameName As Object, ByRef PostData As Object, ByRef Headers As Object, ByRef Cancel As Boolean)
        If URL.ToString() = "" Then
          Cancel = True
          MessageBox.Show("The request has been canceled")
        End If
      End Sub
    End Class



    Bob Bao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, April 13, 2011 8:07 AM