Microsoft KB Archive/182033

= How To Use BeforeNavigate2 in VB to Get an Event from HTML =

Article ID: 182033

Article Last Modified on 8/30/2004

-

APPLIES TO


 * Microsoft Visual Basic 5.0 Control Creation Edition
 * Microsoft Visual Basic 5.0 Enterprise Edition
 * Microsoft Visual Basic 4.0 32-Bit Enterprise Edition

-



This article was previously published under Q182033



SUMMARY
In an HTML page displayed in the WebBrowser control, you can trigger an event in the Visual Basic application by using the BeforeNavigate2 event in the WebBrowser control. For example, you can have a command button in the HTML page that closes the Visual Basic application when clicked.



MORE INFORMATION
 Create a Standard EXE project from Visual Basic. Form1 is created by default. Add Microsoft Internet Controls to your Project Components. Add a WebBrowser Control from to Form1.  Add code for Form1 Load event as follows: Private Sub Form_Load WebBrowser1.Navigate "c:\mytest.htm" End Sub

  Add code for WebBrowser1 BeforeNavigate2 event as follows: Private Sub WebBrowser1_BeforeNavigate2(ByVal pDisp As Object, _          URL As Variant, Flags As Variant, _           TargetFrameName As Variant, PostData As Variant, _           Headers As Variant, Cancel As Boolean)

Dim Ev As String

If UCase$(Left$(URL, 6)) = "EVENT:" Then 'this is our event, process it and cancel navigation Ev = UCase$(Mid$(URL, 7)) Select Case Ev                 Case "HELLO" MsgBox "Hello from VB App" 'set a field on HTML page WebBrowser1.Document.All("field1").Value = _ "Hello from VB"

Case "CLOSE" If MsgBox("Are you sure you want to exit?", _                 vbYesNo) = vbYes Then End End If             End Select Cancel = True End If     End Sub

  From Notepad, type in the following text and save it as c:\mytest.htm or another location (be sure to use the same location in step 3): 

 <INPUT type=button name=Hello value="Hello World"> <INPUT type=button name=Close value="Close App">

<SCRIPT LANGUAGE="VBScript"> Sub Close_OnClick location.href = "Event:Close" End Sub

Sub Hello_OnClick location.href = "Event:Hello" End Sub </SCRIPT> </BODY></HTML>

</li> Run your project from Visual Basic.</li></ol>

Keywords: kbhowto kb32bitonly KB182033

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.