none
Webbrowser Blocking ads

    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

Answers

  • 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
        WebBrowser1.Navigate(TextBox1.Text)
      End Sub
    
      Public Sub New()
    
        ' This call is required by the designer.
        InitializeComponent()
    
        ' 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() = "http://www.microsoft.com/" Then
          Cancel = True
          MessageBox.Show("The request has been canceled")
        End If
      End Sub
    
    End Class

    Download: http://cid-51b2fdd068799d15.office.live.com/self.aspx/.Public/Samples%5E_2011/20110413%5E_CancelWebBrowserNavigationRequest.zip

     


    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
    Moderator