VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



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

by Dave Slinn (2 Submissions)
Category: Internet/HTML
Compatability: VB Script
Difficulty: Beginner
Date Added: Wed 3rd February 2021
Rating: (4 Votes)

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.

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

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


Download this snippet    Add to My Saved Code

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

No comments have been posted about Dynamically Add WebBrowser Control at runtime without a Reference (V. 2). Why not be the first to post a comment about Dynamically Add WebBrowser Control at runtime without a Reference (V. 2).

Post your comment

Subject:
Message:
0/1000 characters