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 in VB .NET
KeywordsWebBrowser, HTMLDocument, navigate, VB.NET
CategoriesControls, VB.NET
 
To add the WebBrowser control to your toolbox, open the Tools menu and select Add/Remove Toolbox Items. Select the COM Components tab, wait a long time for the tab to load, check the box next to Microsoft Web Browser, and click OK.

Now use the Toolbox to 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. In Solution Explorer, right click References and select Add Reference. On the .NET tab, double click the Microsoft.mshtml entry and then click OK.

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.

 
' Go home.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal _
    e As System.EventArgs) Handles MyBase.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>"

    AxWebBrowser1.GoHome()
End Sub

' Set the HTML contents.
Private Sub btnSetContents_Click(ByVal sender As _
    System.Object, ByVal e As System.EventArgs) Handles _
    btn.Click
    Dim doc As mshtml.HTMLDocument = _
        AxWebBrowser1.Document()
    doc.body.innerHTML = txtHtml.Text
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated