How to Add a Web Browser to a Visual Basic Form

Using a Web Browser in Visual Basic - Mark Alexander Bain
Using a Web Browser in Visual Basic - Mark Alexander Bain
A VB developer may decide to tell their uses how to cut and paste URLs into web browsers or they may build a web browser into their application.

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:

Imports System.Drawing
Imports System.Windows.Forms
Imports System.ComponentModel

Though these groups of classes the programmer can access the WebBrowser class and use it as part of their form class:

Public Class Form1
Inherits Form
Public WithEvents web1 As WebBrowser

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:

Public Sub New
Me.Location = New Point (100, 100)
Me.Size = New Size (800,600)

To add a WebBrowser object to the form:

web1 = New WebBrowser
Me.Controls.Add(web1)

And to set up its size and its location:

web1.Location = New Point(0, 0)
web1.Size = New Size (800,600)

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:

web1.navigate("www.e-a-c.co.uk")
End Sub
End Class

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:

Module Module1
Sub Main
Application.Run (New Form1)
End Sub
End Module

And it can then be compiled:

vbc webcontrol.vb

and run from the command line:

webcontrol

At which point the user will see a web page displayed within a form (as shown in figure 1).

Mark Alexander Bain - Mark Alexander Bain is a writer, Mo Bro and consultant for all aspects of software development at dsquared. He has also written regularly ...

rss
Advertisement
Advertisement