Introduction to Programming Using Visual Basic

W
Document Sample
scope of work template
							 Introduction to Programming Using Visual Basic
        Class 11: Creating a Web Browser

Goals for this Week
Programming Skill:   Learning a new control - WebBrowser

Review
      -   Procedures
          - Functions
          - Subroutines

      -   Objects
          - Properties
          - Events
          - Methods – a method is a procedure that belongs to and acts on one
             particular object
             - Syntax:
                     call objectName.methodName(arguments)
             - Example
                     call cmbGroceries.AddItem(“Carrots”)

The WebBrowser Control
     - Properties
        - (Name)
        - Height
        - Width
     - Methods
        - Navigate(strURL As String)
        - GoForward
        - GoBack
        - Refresh
     - Events
        - NavigateComplete or NavigateComplete2
        - BeforeNavigate or BeforeNavigate2
Lab for Class 11: Web Browser

                                        Part 1
Create a New Project

        1. Start Visual BASIC, select New Project from the File menu and create a
           Standard Exe
        2. Save Form to your floppy as browser.frm
        3. Save Project to your floppy as browser.vbp

Add a New Control to your Toolbox

There are more than just the twenty or so controls on the toolbar. Microsoft ships dozens
more with Visual Basic and other companies create them as well. You can also create
your own controls, but we will leave that for another day.

   1.      Right click on an empty area in the toolbox.
   2.      Select Components
   3.      Scroll down and click on the checkbox next to Microsoft Internet Controls
   4.      Press OK
   5.      Notice that two new controls have been added to your toolbox: WebBrowser
        and ShellFolderViewOC.

Add Control Objects to your Form

   1. Add a ComboBox control named cmbURL
   2. Add a WebBrowser controls named webMain
   3. Add 5 CommandButton controls named cmdBack, cmdForward, cmdReload,
      cmdHome and cmdGo.



Now your form should look something like this:
Set Option Explicit

       1. Click on the code View button or select View Code from the menu to bring up
          the code window
       2. At the top of the code window, add the following lines:
                      Option Explicit

Navigate

       1. Create a procedure for the click event of the Go button:
       2. Call the navigate method of the web browser:
                       webMain.Navigate cmbURL.Text

       3. Run your program.

Forward and Back and Reload

       1. Your webMain object has the methods GoForward, GoBack, and Refresh.
          Connect them to the buttons.
       2. Test your program

Home

       1. Declare a variable, strHome, that is global in scope. Set it to your default
          URL, such as http://www.cnn.com, in the Form_Load() subroutine.

       2. Connect the Home button, so that it navigates to strHome.

       3. Test your program

Make a stand-alone executable

1. Save your work first! Creating an executable can cause problems if your system does
   not have enough disk space or RAM. If you have problems, just skip this section

2. File / Make browser.exe

3. Exit Visual Basic

4. Run browser.exe from Windows Explorer or My Computer.

                                        Part 2
Add Items to the History Combo Box

1. In the code Window, create an event handler for the NavigateComplete event from
   webMain using the two combo boxes at the top of the code window. (On some
   systems, the event is called NavigateComplete2.)
2. The webMain_NavigateComplete is called automatically after a new page is loaded.
   The URL argument is the name of the new page. Add this string to the combo box.

3. Test your program. Does it work? Are there duplicates in the list? Use Forward and
   Back to see the duplicates.


Remove the Duplicates in the History Combo Box

Modify the webMain_NavigateComplete subroutine to first check if the URL exists in
the combo box. You will need to:

1. Create a For .. Next loop.

2. Inside the loop, test the URL using:
       If URL = cmbURL.List(intCurrent) Then

3. If the test succeeds, add the item to the list, as you did previously.

Hint: You might find it useful to use Exit Sub
                                        Part 3
Resize
Add this function:

         Private Sub Form_Resize()
            Dim intNewHeight As Integer
            Dim intNewWidth As Integer

           intNewHeight = frmMain.Height - webMain.Top - 400
           intNewWidth = frmMain.Width - 100
           If intNewHeight > 0 Then
               webMain.Height = intNewHeight
           End If
           If intNewWidth > 0 Then
               webMain.Width = intNewWidth
           End If
         End Sub

Restrict Navigation

Create a Function: BlnAllowAccess . It will take a string as an argument and return a
boolean. Create an event handler for the BeforeNavigate event and add the lines:

            If BlnAllowAccess(URL) = False Then
                       Cancel = True
                       MsgBox "You are not allowed to browse " & URL
                       Exit Sub
            End If

            cmbURL.Text = URL