Creating a Web Browser
If you're creating a custom browser, you may wish to restrict certain Web sites. You do this by intercepting the request to visit a Web site with the BeforeNavigate2 event.
Here's a chunk of sample code that doesn't allow the user to visit playboy.com:
Private Sub WebBrowser1_BeforeNavigate2 (ByVal pDisp As Object, _ URL As Variant, Flags As _ Variant, TargetFrameName As Variant, _ PostData As Variant, Headers As Variant, _ Cancel As Boolean) If InStr(1, URL, "playboy.com") Then Cancel = True MsgBox "Sorry, that site is restricted!" End If End Sub
This works by checking the passed URL (Web Address). If it contains the string 'playboy.com', the action is cancelled and a message box displayed. Otherwise, if everything is A-OK, access to the site is allowed.
It's worth noting that this code will work equally as well in either the BeforeNavigate or BeforeNavigate2 events. Users of Visual Basic 6 will only have BeforeNavigate2, whilst Visual Basic 5 folk may have both. There is very little different between the two methods. For more information, consult the help file.
Page 6 of 8
This article was originally published on November 20, 2002