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)
SUMMARYThis article describes how to display an HTML page by using
a WebBrowser control or a WebClient class. Information about the WebBrowser controlThe 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 classThe 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 siteThis 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. - 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 - On the Build menu, click Build
Solution.
- On the Debug menu, click
Start.
- Click Button1.
Use the WebClient class to
download a file from the WebThis step-by-step example describes how to download a file
from a Web site onto your local computer.
- Start Visual Studio 2005 or 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. - 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. - On the .NET Framework Components tab,
click WebClient, and then click
OK.
- Add a
WebClient control to Form1.
- Add a Button control to Form1.
- 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 - On the Build menu, click Build
Solution.
- On the Debug menu, click
Start.
- Click Button1.
- 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 computerThis step-by-step example describes how to browse the
downloaded file on your local computer by using the WebClient class.
- Start Visual Studio 2005 or 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 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 .NET 2003, add a Microsoft Web Browser control to Form1. - Create an HTML document that is named TestPage.htm, and then save the file on drive C.
- 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 - On the Build menu, click Build
Solution.
- On the Debug menu, click
Start.
- Click Button1.
REFERENCESFor 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: | Minor | Last Reviewed: | 10/3/2006 |
---|
Keywords: | kbvs2005swept kbvs2005applies kbCOMInterop kbinterop kbWindowsForms kbWebClasses kburl kbWebBrowser kbweb kbCtrl kbControl kbHOWTOmaster kbhowto KB821771 kbAudDeveloper |
---|
|
|
©2004 Microsoft Corporation. All rights reserved.
|
|