WebView2 Control Events Reference
Basic Events
Create Event
- Description: Fires just before the creation of the WebView2 control
- Purpose: Used to control environment options such as UserDataFolder
- Example:
vb
Private Sub WebView21_Create()
WebView21.EnvironmentOptions.UserDataFolder = Environ("TEMP") & "\myapp\webview2"
End Sub
Ready Event
- Description: Fires after the WebView2 control is created and ready for use
- Purpose: Indicates when navigation and other operations can begin
- Example:
vb
Private Sub WebView21_Ready()
' Control is ready, start navigation
WebView21.Navigate "https://www.example.com"
End Sub
Error Event
- Description: Catches errors that occur during WebView2 control initialization
- Parameters:
code
(Long) - Error codemsg
(String) - Error message
- Example:
vb
Private Sub WebView21_Error(ByVal code As Long, ByVal msg As String)
MsgBox "WebView2 Error: " & msg & " (Error Code: " & code & ")", vbExclamation
End Sub
Navigation Events
NavigationStarting Event
- Description: Fires before navigation begins
- Parameters:
Uri
(String) - Target URLIsUserInitiated
(Boolean) - Whether initiated by user actionIsRedirected
(Boolean) - Whether this is a redirectRequestHeaders
(WebView2RequestHeaders) - Request headersCancel
(Boolean) - Set to True to cancel navigation
- Example:
vb
Private Sub WebView21_NavigationStarting(ByVal Uri As String, _
ByVal IsUserInitiated As Boolean, ByVal IsRedirected As Boolean, _
ByVal RequestHeaders As WebView2RequestHeaders, ByRef Cancel As Boolean)
' Example: Block navigation to specific domains
If InStr(1, Uri, "blocked-site.com") > 0 Then
Cancel = True
MsgBox "Access blocked", vbInformation
End If
End Sub
NavigationComplete Event
- Description: Fires after navigation completes
- Parameters:
IsSuccess
(Boolean) - Whether navigation was successfulWebErrorStatus
(Long) - Error status code
- Example:
vb
Private Sub WebView21_NavigationComplete(ByVal IsSuccess As Boolean, _
ByVal WebErrorStatus As Long)
If IsSuccess Then
Debug.Print "Page loaded successfully: " & WebView21.DocumentURL
Else
Debug.Print "Page load failed, error code: " & WebErrorStatus
End If
End Sub
SourceChanged Event
- Description: Fires when the DocumentURL property is updated, typically after navigation
- Parameters:
IsNewDocument
(Boolean) - Whether this is a new document
- Purpose: Used to keep URL bars in sync
- Example:
vb
Private Sub WebView21_SourceChanged(ByVal IsNewDocument As Boolean)
' Assuming we have a textbox for displaying the URL
txtUrl.Text = WebView21.DocumentURL
If IsNewDocument Then
' Special handling for new documents
UpdateNavigationButtons
End If
End Sub
DocumentTitleChanged Event
- Description: Fires when the page title changes
- Purpose: Update application window title
- Example:
vb
Private Sub WebView21_DocumentTitleChanged()
' Update window title
Me.Caption = WebView21.DocumentTitle & " - My Browser"
End Sub
Interaction Events
PermissionRequested Event
- Description: Fires when webpage requires authorization for tasks like clipboard access or camera usage
- Parameters:
IsUserInitiated
(Boolean) - Whether triggered by user actionState
(wv2PermissionState) - Permission stateUri
(String) - Requesting page URLPermissionKind
(wv2PermissionKind) - Type of permission
- Example:
vb
Private Sub WebView21_PermissionRequested(ByVal IsUserInitiated As Boolean, _
ByRef State As wv2PermissionState, ByVal Uri As String, _
ByVal PermissionKind As wv2PermissionKind)
Select Case PermissionKind
Case wv2PermissionKind.wv2PermissionKindCamera
' Camera permission request
If MsgBox("Website " & Uri & " requests camera access. Allow?", _
vbQuestion + vbYesNo) = vbYes Then
State = wv2PermissionState.wv2PermissionStateAllow
Else
State = wv2PermissionState.wv2PermissionStateDeny
End If
Case wv2PermissionKind.wv2PermissionKindClipboard
' Auto-allow clipboard access
State = wv2PermissionState.wv2PermissionStateAllow
End Select
End Sub
AcceleratorKeyPressed Event
- Description: Fires when accelerator keys are pressed
- Parameters:
KeyState
(wv2KeyEventKind) - Key stateIsExtendedKey
(Boolean) - Whether it's an extended keyWasKeyDown
(Boolean) - Previous key stateIsKeyReleased
(Boolean) - Whether key was releasedIsMenuKeyDown
(Boolean) - Menu key stateRepeatCount
(Long) - Key repeat countScanCode
(Long) - Key scan codeIsHandled
(Boolean) - Whether event was handled
- Example:
vb
Private Sub WebView21_AcceleratorKeyPressed(ByRef KeyState As wv2KeyEventKind, _
ByVal IsExtendedKey As Boolean, ByVal WasKeyDown As Boolean, _
ByVal IsKeyReleased As Boolean, ByVal IsMenuKeyDown As Boolean, _
ByVal RepeatCount As Long, ByVal ScanCode As Long, _
ByRef IsHandled As Boolean)
' Example: Capture Ctrl+B shortcut
If KeyState = wv2KeyEventKind.wv2KeyEventKeyDown And _
ScanCode = vbKeyB And IsMenuKeyDown Then
' Execute custom bookmark function
AddBookmark
IsHandled = True
End If
End Sub
WebResourceRequested Event
- Description: Fires when HTTP requests match URIs set up through AddWebResourceRequestedFilter
- Parameters:
Request
(WebView2Request) - Request objectResponse
(WebView2Response) - Response object
- Example:
vb
Private Sub WebView21_WebResourceRequested(ByVal Request As WebView2Request, _
ByVal Response As WebView2Response)
' Example: Intercept image requests
If InStr(1, Request.URI, ".jpg") > 0 Or InStr(1, Request.URI, ".png") > 0 Then
Response.Content = LoadFileToByteArray("C:\images\placeholder.png")
Response.Headers = "Content-Type: image/png"
End If
End Sub
NewWindowRequested Event
- Description: Fires when a new webpage window should be opened
- Parameters:
IsUserInitiated
(Boolean) - Whether initiated by userIsHandled
(Boolean) - Set to True if handledUri
(String) - Target URL- Various position and size parameters
- Example:
vb
Private Sub WebView21_NewWindowRequested(ByVal IsUserInitiated As Boolean, _
ByRef IsHandled As Boolean, ByVal Uri As String, _
ByVal HasPosition As Long, ByVal HasSize As Long, _
ByVal Left As Long, ByVal Top As Long, _
ByVal Width As Long, ByVal Height As Long, _
ByVal ShouldDisplayMenuBar As Long, _
ByVal ShouldDisplayStatus As Long, _
ByVal ShouldDisplayToolbar As Long, _
ByVal ShouldDisplayScrollBars As Long)
' Example: Open in new tab
If IsUserInitiated Then
CreateNewTab Uri
IsHandled = True
End If
End Sub
ScriptDialogOpening Event
- Description: Fires when AreDefaultScriptDialogsEnabled is False, allowing custom dialog implementation
- Parameters:
- Dialog type and content parameters
- Example:
vb
Private Sub WebView21_ScriptDialogOpening(ByVal ScriptDialogKind As wv2ScriptDialogKind, _
ByRef Accept As Boolean, ByVal ResultText As String, ByVal URI As String, _
ByVal Message As String, ByVal DefaultText As String)
Select Case ScriptDialogKind
Case wv2ScriptDialogKind.wv2ScriptDialogKindAlert
' Custom alert() implementation
MsgBox Message, vbInformation, "Web Page Alert"
Accept = True
Case wv2ScriptDialogKind.wv2ScriptDialogKindPrompt
' Custom prompt() implementation
Dim result As String
result = InputBox(Message, "Please Enter", DefaultText)
If result <> "" Then
ResultText = result
Accept = True
End If
End Select
End Sub
JsMessage Event
- Description: Fires when JavaScript calls window.chrome.webview.postMessage()
- Parameters:
Message
(Variant) - Message from JavaScript
- Example:
vb
Private Sub WebView21_JsMessage(ByVal Message As Variant)
' Handle messages from JavaScript
Select Case VarType(Message)
Case vbString
Debug.Print "Received text message: " & Message
Case vbObject
' Handle JSON object
Dim json As String
json = Message.stringify()
Debug.Print "Received JSON message: " & json
End Select
' Send reply back to JavaScript
WebView21.PostWebMessage "Message received: " & Message
End Sub