Skip to content

Planet-Source-Code/dave-slinn-dynamically-add-webbrowser-control-at-runtime-without-a-reference-v-2__1-59979

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 

Repository files navigation

Dynamically Add WebBrowser Control at runtime without a Reference (V. 2)

Description

Allows VB applications to determine at run-time if Internet Explorer (4.0 or later) is installed, and if so, creates a WebBrowser. If not, a trappable error allows program to continue. This is an enhanced version of a previous article, showing how to capture the events of the created object.

More Info

Submitted On
By Dave Slinn
Level Beginner
User Rating 5.0 (20 globes from 4 users)
Compatibility VB 6.0
Category Internet/ HTML
World Visual Basic
Archive File

Source Code

On a VB6 Form, add a Button control in the upper left corner and a Listbox control underneath the button and down the left hand side of the form.  Events raised by the WebBrowser will be displayed in the listbox.

Place the following code into a standard VB 6.0 form.


Private WithEvents m_WebControl As VBControlExtender

Private Sub Form_Resize()
On Error Resume Next
   Me.List1.Height = Me.ScaleHeight - Me.List1.Top

   ' resize webbrowser to fill form next to listbox
   If Not m_WebControl Is Nothing Then
     m_WebControl.Move Me.List1.Left + Me.List1.Width + 30, 0, ScaleWidth - (Me.List1.Left + Me.List1.Width + 30), ScaleHeight
   End If
End Sub

Private Sub Command1_Click()
On Error GoTo ErrHandler

   ' attempting to add WebBrowser here ('Shell.Explorer.2' is registered
   ' with Windows if a recent (>= 4.0) version of Internet Explorer is installed
   Set m_WebControl = Controls.Add("Shell.Explorer.2", "webctl", Me)

   ' if we got to here, there was no problem creating the WebBrowser
   ' so we should size it properly and ensure it's visible
   m_WebControl.Move Me.List1.Left + Me.List1.Width + 30, 0, ScaleWidth - (Me.List1.Left + Me.List1.Width + 30), ScaleHeight
   m_WebControl.Visible = True

   ' use the Navigate method of the WebBrowser control to open a
   ' web page
   m_WebControl.object.navigate "http://www.planet-source-code.com"

Exit Sub
ErrHandler:
   MsgBox "Could not create WebBrowser control", vbInformation
End Sub

Private Sub m_WebControl_ObjectEvent(Info As EventInfo)
On Error GoTo ErrHandler

   Dim i As Integer
   Dim evp As EventParameter

   ' display the event that was raised in the listbox
   Me.List1.AddItem "Event Raised: " & Info.Name
   For Each evp In Info.EventParameters
      Me.List1.AddItem " " & evp.Name & " (" & evp.Value & ")"
   Next evp

   Me.List1.ListIndex = Me.List1.NewIndex
Exit Sub
ErrHandler:
   If Err.Number = -2147024809 Then
      Me.List1.AddItem " " & evp.Name & " (#ERROR)"
      Resume Next
   End If
End Sub

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published