Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
 
TitleSet the WebBrowser control's contents explicitly
KeywordsWebBrowser, HTMLDocument, navigate
CategoriesControls, VB.NET
 
Place a WebBrowser control on the form.

The WebBrowser control has a Document property that returns an object of type HTMLDocument. To use this object, you need a reference to a Microsoft HTML library. Select the Project menu's References command, select the Microsoft HTML Object Library entry, and click OK.

(Note: You can use the Document property as a generic Object if you don't want to include the reference. It will be a little slower but should still work.)

When the form loads, the Form_Load event handler sets some default HTML text in the txtHtml TextBox. It then makes the WebBrowser control go it the system's defined home page. This is necessary because the WebBrowser's Document property returns Nothing unless the control is displaying something.

When the user clicks Set Contents, the program gets the WebBrowser's Document property. It sets that object's body.innerHTML property to the HTML text in the TextBox.

 
Private Sub Form_Load()
    txtHtml.Text = _
        "<HTML>" & vbCrLf & _
        "  <BODY>" & vbCrLf & _
        "    <H1>Hello World!</H1>" & vbCrLf & _
        "Go to <A HREF=""http://www.vb-helper.com"">VB " & _
            "Helper</A>" & vbCrLf & _
        "  </BODY>" & vbCrLf & _
        "</HTML>"

    WebBrowser1.GoHome
End Sub

Private Sub cmdSetContents_Click()
Dim doc As MSHTML.HTMLDocument

    Set doc = WebBrowser1.Document
    doc.body.innerHTML = _
        "<HTML>" & vbCrLf & _
        "  <BODY>" & vbCrLf & _
        "    <H1>Hello World!</H1>" & vbCrLf & _
        "Go to <A HREF=""http://www.vb-helper.com"">VB " & _
            "Helper</A>" & vbCrLf & _
        "  </BODY>" & vbCrLf & _
        "</HTML>"
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated