How to use a WebClient class or a WebBrowser control to display an HTML page in Visual Basic (821771)



The information in this article applies to:

  • Microsoft Visual Basic 2005
  • Microsoft Visual Basic .NET (2003)
  • Microsoft Visual Basic .NET (2002)

SUMMARY

This article describes how to display an HTML page by using a WebBrowser control or a WebClient class.

Information about the WebBrowser control

The WebBrowser control enables your application to browse the Web, to view documents, and to download data. Users of applications that use this control can browse to Web sites, to folders on the local computer, and to folders on a network drive.

The WebBrowser control supports Web browsing through both point-and-click browsing and URL navigation. The control maintains a history list that enables the user to browse forward and backward through sites, folders, and files that the user has browsed to previously.

Information about the WebClient class

The WebClient class provides common methods to send data to or to receive data from any local, intranet, or Internet resource that is identified by a Uniform Resource Identifier (URI).

The WebClient class provides four methods for uploading data to a resource:
  • The OpenWrite method returns a stream that is used to send data to the resource.
  • The UploadData method sends a byte array to the resource and then returns a byte array that contains any response.
  • The UploadFile method sends a local file to the resource and then returns a byte array that contains any response.
  • The UploadValues method sends a NameValueCollection constructor to the resource and then returns a byte array that contains any response.
The WebClient class also provides three methods for downloading data from a resource:
  • The DownloadData method downloads data from a resource and then returns a byte array.
  • The DownloadFile method downloads data from a resource to a local file.
  • The OpenRead method returns the data from the resource as a stream.

Use the WebBrowser control to browse to a Web site

This step-by-step example describes how to browse a site by using the Navigate method of the WebBrowser control.
  1. Start Microsoft Visual Studio 2005 or Microsoft Visual Studio .NET.
  2. On the File menu, point to New, and then click Project.
  3. Under the Project Types section, click Visual Basic Projects. Under the Templates section, click Windows Application, and then click OK. By default, Form1 is created.

    Note In Visual Studio 2005, click Visual Basic under Project types.
  4. Add a Button control to Form1.
  5. On the Tools menu, click Customize Toolbox.
    Note In Microsoft Visual Studio .NET 2003, click Add/Remove Toolbox Items on the Tools menu.

    In Visual Studio 2005, click Choose Toolbox Items on the Tools menu.
  6. On the COM Components tab, click Microsoft Web Browser, and then click OK.
  7. Add an Explorer control to Form1.

    Note In Visual Studio 2005 or in Visual Studio .NET 2003, add a Microsoft Web Browser control to Form1.
  8. Add the following code to the Button1_Click event handler.
    Try
       AxWebBrowser1.Dock = DockStyle.Fill
       'Browse to the specified URL.
       AxWebBrowser1.Navigate("http://www.microsoft.com")
    Catch ex As Exception
       MessageBox.Show("Access Failed" & vbCrLf & ex.Message)
    End Try
  9. On the Build menu, click Build Solution.
  10. On the Debug menu, click Start.
  11. Click Button1.

Use the WebClient class to download a file from the Web

This step-by-step example describes how to download a file from a Web site onto your local computer.
  1. Start Visual Studio 2005 or Visual Studio .NET.
  2. On the File menu, point to New, and then click Project.
  3. Under the Project Types section, click Visual Basic Projects. Under the Templates section, click Windows Application, and then click OK. By default, Form1 is created.

    Note In Visual Studio 2005, click Visual Basic under Project types.
  4. On the Tools menu, click Customize Toolbox.
    Note In Visual Studio .NET 2003, click Add/Remove Toolbox Items on the Tools menu.

    In Visual Studio 2005, click Choose Toolbox Items on the Tools menu.
  5. On the .NET Framework Components tab, click WebClient, and then click OK.
  6. Add a WebClient control to Form1.
  7. Add a Button control to Form1.
  8. Add the following code to the Button1_Click event handler.
    'Download the file from the specified URL.
    Try
        WebClient1.DownloadFile("http://www.microsoft.com/default.asp", "c:\TestPage.htm")
        MessageBox.Show("Download Completed.")
    Catch ex As Exception
        MessageBox.Show("Download Failed" & vbCrLf & ex.Message)
    End Try
  9. On the Build menu, click Build Solution.
  10. On the Debug menu, click Start.
  11. Click Button1.
  12. Locate the TestPage.htm file on drive C. Right-click the file, and then click Open.

Use the WebBrowser control to browse to a file on the local computer

This step-by-step example describes how to browse the downloaded file on your local computer by using the WebClient class.
  1. Start Visual Studio 2005 or Visual Studio .NET.
  2. On the File menu, point to New, and then click Project.
  3. Under the Project Types section, click Visual Basic Projects. Under the Templates section, click Windows Application, and then click OK. By default, Form1 is created.

    Note In Visual Studio 2005, click Visual Basic under Project types.
  4. Add a Button control to Form1.
  5. On the Tools menu, click Customize Toolbox.
    Note In Visual Studio .NET 2003, click Add/Remove Toolbox Items on the Tools menu.

    In Visual Studio 2005, click Choose Toolbox Items on the Tools menu.
  6. On the COM Components tab, click Microsoft Web Browser, and then click OK.
  7. Add an Explorer control to Form1.
    Note In Visual Studio .NET 2003, add a Microsoft Web Browser control to Form1.
  8. Create an HTML document that is named TestPage.htm, and then save the file on drive C.
  9. Add the following code to the Button1_Click event handler.
      
    Try
         AxWebBrowser1.Dock = DockStyle.Fill
         'Browse to the specified file on your computer.
         AxWebBrowser1.Navigate("C:\TestPage.htm")
    Catch ex As Exception
         MessageBox.Show(ex.Message)
    End Try
  10. On the Build menu, click Build Solution.
  11. On the Debug menu, click Start.
  12. Click Button1.

REFERENCES

For more information about the WebClient class, visit the following Microsoft Developer Network (MSDN) Web site:For more information about the WebBrowser control, visit the following MSDN Web site:

Modification Type:MinorLast Reviewed:10/3/2006
Keywords:kbvs2005swept kbvs2005applies kbCOMInterop kbinterop kbWindowsForms kbWebClasses kburl kbWebBrowser kbweb kbCtrl kbControl kbHOWTOmaster kbhowto KB821771 kbAudDeveloper