Displaying HTML Content with a Web Browser Control in Visual Basic - Creating a user interface for testing
(Page 3 of 4 )
Create a UI as shown in Fig.2, displaying the design view with a command button, a text box, a label and the web browser. The web browser control can be dragged from the tool box and dropped on the form as shown below the button. It can be sized to your liking. The figure shows all these items on the form "Displaying with web browser." Move to the web page, look at the code view, and you will see all these items in the "General" drop down menu where the web browser control is referred to as WebBrowser1.
Fig.2
Now take a look at the control's methods, properties, and so forth by bringing up the Object browser window by clicking View -> Object Browser as shown in Fig.3. If you click on SHDocVwCtl you will see the various classes and class members of the web browser control. Although we will be using just one method of this control, you can see that there is rich functionality associated with this control, including the history of the page through Go back, Go forward and so on; the down loading of content; data binding, and more. Since the control is seen in the object browser you could access the properties and methods in the code assisted by intellisense. Out of all these properties and methods we will use one method, called Navigate(), which takes a URL as an argument.
Fig.3
Now click on the button and add the following code. Since intellisense is in effect, if you type webbrowser1 and insert a period after it, you will get the drop down menu with all associated applicable methods, properties, and events for this control as shown in Fig.4. Here is the complete reference to all the properties and methods for this control.
Private Sub Command1_Click()
WebBrowser1.Navigate (Text1.Text)
End Sub
Fig.4

Now passing a URL as a reference you can display the web page as shown in Fig.5.
Fig.5
In addition to a URL you may also use a full file path, or a path on a network drive for a display. For example each of the following will display the same page without an error.
URL: http://localhost/URLRef.htm
UNC Path: hodentekstagerURLRef.htm
Full reference: C:URLRef.htm
In addition to the .htm file you may also display *.xml, *.rtf and *.doc files in the web browser by navigating to the file, and when required the Open or Save option is presented, choose open. The document will be opened inside the web browser.
Next: How to display HTML content generated by a program >>
More Visual Basic.NET Articles
More By Jayaram Krishnaswamy