Sunday, August 19, 2007

Use the WebBrowser control

TitleMake a Web browser that can only view certain URLs
KeywordsWeb browser, restrict, URL
CategoriesUtilities, Controls, Internet

Use the WebBrowser control. In its BeforeNavigate2 event handler, examine the URL. If the URL is not allowed, set Cancel to True.

Also set Cancel to True in the NewWindow2 event handler so the user cannot open a link in a new window.

This example allows only URLs that begin with "http://mcp-vb.blogspot.com/"



Private Sub Form_Load()
WebBrowser1.Navigate "http://mcp-vb.blogspot.com/"
End Sub

' Cancel any navigation that moves outside VB helper.
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)
Const TARGET = "http://mcp-vb.blogspot.com/"

Cancel = (LCase$(Left$(URL, Len(TARGET))) <> TARGET)
If Cancel Then MsgBox URL & " is blocked"
End Sub

' Don't let the user open a new window.
Private Sub WebBrowser1_NewWindow2(ppDisp As Object, Cancel _
As Boolean)
Cancel = True
MsgBox "You cannot open a new window."
End Sub

1 comment:

Anonymous said...

Good words.