Often Visual Basic programmers need the users of their applications to be able to access the Internet. For example, the programmer may want their users to access: on-line documentation; important financial information (such as Google Finance); information for particular projects (such as the Environmental Archaeology Consultancy). In such such cases the developer may tell their users to copy and paste URLs into their web browsers. However, a more effective method may to be to build the web browser into one of the forms in their Visual Basic application.
Creating a Simple Visual Basic Form
The article Creating Visual Basic Applications from the Command Line discusses the creation of a simple Visual Basic form using just the Notepad text editor and the Microsoft .NET framework Visual Basic compiler. It also shows how easy it is to add components (such as buttons and text boxes) to the VB form. Fortunately it is just as easy to a add a web browser to a form.
Adding a Web Browser to a Visual Basic Form
The Web Browser is simply a component that can be added to any Visual Basic form. The first step is, therefore, to import the appropriate namespaces:
Though these groups of classes the programmer can access the WebBrowser class and use it as part of their form class:
The form class must contain a "New" subroutine which is run when the class is instantiated. This method can be used for a number of useful activities, for example it can be used to set the position and size of the form itself:
To add a WebBrowser object to the form:
And to set up its size and its location:
It's worth noting at this point that the form's location is with regards to the top left corner of the computer screen and the WebBrowser's location is with regards to the form's top left corner
Navigating to a Web Site
At this point a blank web browser would be displayed but the programmer can change this by updating the WebBrowser's navigate method:
And now a web site will be displayed in the form's web browser when the application is run.
Running the Web Browser Application
The application requires a module section to run the form:
And it can then be compiled:
and run from the command line:
At which point the user will see a web page displayed within a form (as shown in figure 1).