Microsoft KB Archive/821771

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

Article ID: 821771

Article Last Modified on 11/26/2007

-

APPLIES TO


 * Microsoft Visual Basic 2005
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition

-





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.  Start Microsoft Visual Studio 2005 or Microsoft Visual Studio .NET. On the File menu, point to New, and then click Project. 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. Add a Button control to Form1. 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. On the COM Components tab, click Microsoft Web Browser, and then click OK. 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.</li>  Add the following code to the Button1_Click event handler. Try AxWebBrowser1.Dock = DockStyle.Fill 'Browse to the specified URL. AxWebBrowser1.Navigate(&quot;http://www.microsoft.com&quot;) Catch ex As Exception MessageBox.Show(&quot;Access Failed&quot; & vbCrLf & ex.Message) End Try </li> On the Build menu, click Build Solution.</li> On the Debug menu, click Start.</li> Click Button1.</li></ol>

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. <ol> Start Visual Studio 2005 or Visual Studio .NET.</li> On the File menu, point to New, and then click Project.</li> 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.</li> 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.</li> On the .NET Framework Components tab, click WebClient, and then click OK.</li> Add a WebClient control to Form1.</li> Add a Button control to Form1.</li>  Add the following code to the Button1_Click event handler. 'Download the file from the specified URL. Try WebClient1.DownloadFile(&quot;http://www.microsoft.com/default.asp&quot;, &quot;c:\TestPage.htm&quot;) MessageBox.Show(&quot;Download Completed.&quot;) Catch ex As Exception MessageBox.Show(&quot;Download Failed&quot; & vbCrLf & ex.Message) End Try </li> On the Build menu, click Build Solution.</li> On the Debug menu, click Start.</li> Click Button1.</li> Locate the TestPage.htm file on drive C. Right-click the file, and then click Open.</li></ol>

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. <ol> <li>Start Visual Studio 2005 or Visual Studio .NET.</li> <li>On the File menu, point to New, and then click Project.</li> <li>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.</li> <li>Add a Button control to Form1.</li> <li>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.</li> <li>On the COM Components tab, click Microsoft Web Browser, and then click OK.</li> <li>Add an Explorer control to Form1.

Note In Visual Studio .NET 2003, add a Microsoft Web Browser control to Form1.</li> <li>Create an HTML document that is named TestPage.htm, and then save the file on drive C.</li> <li> 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(&quot;C:\TestPage.htm&quot;) Catch ex As Exception MessageBox.Show(ex.Message) End Try </li> <li>On the Build menu, click Build Solution.</li> <li>On the Debug menu, click Start.</li> <li>Click Button1.</li></ol>

<div class="references_section">