WebView2 Control Settings Reference
Basic Settings
IsScriptEnabled Property
- Description: Controls whether JavaScript is enabled
- Type: Boolean
- Example:
vb
' Disable JavaScript
WebView21.IsScriptEnabled = False
' Enable JavaScript and execute script
WebView21.IsScriptEnabled = True
WebView21.ExecuteScript "alert('JavaScript is enabled')"
IsWebMessageEnabled Property
- Description: Controls whether PostWebMessage functionality is enabled
- Type: Boolean
- Example:
vb
' Enable messaging functionality
WebView21.IsWebMessageEnabled = True
' Example message sending
Private Sub SendMessageToWebPage()
If WebView21.IsWebMessageEnabled Then
WebView21.PostWebMessage "Hello World"
End If
End Sub
ZoomFactor Property
- Description: Gets or sets the page zoom factor
- Type: Double
- Example:
vb
' Zoom in 50%
WebView21.ZoomFactor = 1.5
' Zoom out to 75%
WebView21.ZoomFactor = 0.75
' Reset to normal size
WebView21.ZoomFactor = 1.0
' Create zoom controls
Private Sub ZoomIn()
WebView21.ZoomFactor = WebView21.ZoomFactor * 1.2
End Sub
Private Sub ZoomOut()
WebView21.ZoomFactor = WebView21.ZoomFactor / 1.2
End Sub
IsZoomControlEnabled Property
- Description: Controls whether users can zoom using Ctrl++/Ctrl+-/Ctrl+mousewheel
- Type: Boolean
- Example:
vb
' Disable user zoom controls
WebView21.IsZoomControlEnabled = False
' Enable user zoom controls with initial zoom level
Private Sub EnableUserZoom()
WebView21.IsZoomControlEnabled = True
WebView21.ZoomFactor = 1.0
End Sub
Interface Settings
IsStatusBarEnabled Property
- Description: Controls whether the status bar is displayed
- Type: Boolean
- Example:
vb
' Hide status bar
WebView21.IsStatusBarEnabled = False
' Toggle status bar based on user preference
Private Sub chkStatusBar_Click()
WebView21.IsStatusBarEnabled = chkStatusBar.Value
End Sub
AreDefaultContextMenusEnabled Property
- Description: Controls whether default right-click menus are enabled
- Type: Boolean
- Example:
vb
' Disable default context menus
WebView21.AreDefaultContextMenusEnabled = False
' Handle custom context menu
Private Sub WebView21_UserContextMenu(X As Single, Y As Single)
PopupMenu mnuCustomContext, , X, Y
End Sub
AreDevToolsEnabled Property
- Description: Controls whether users can access developer tools via menu or shortcuts
- Type: Boolean
- Example:
vb
' Disable DevTools access
WebView21.AreDevToolsEnabled = False
' Enable DevTools in debug mode
#If DEBUG Then
WebView21.AreDevToolsEnabled = True
#End If
Security Settings
AreHostObjectsAllowed Property
- Description: Controls whether COM objects can be exposed to JavaScript engine
- Type: Boolean
- Example:
vb
' Enable COM object sharing
WebView21.AreHostObjectsAllowed = True
' Enable secure mode
Private Sub EnableSecureMode()
WebView21.AreHostObjectsAllowed = False
WebView21.AreDefaultScriptDialogsEnabled = False
WebView21.AreDevToolsEnabled = False
End Sub
AreDefaultScriptDialogsEnabled Property
- Description: Controls whether default script dialogs (alert, confirm, prompt) are enabled
- Type: Boolean
- Example:
vb
' Disable default dialogs and use custom implementation
WebView21.AreDefaultScriptDialogsEnabled = False
' Custom dialog handler
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
MsgBox Message, vbInformation, "Web Message"
Accept = True
End Select
End Sub
User Agent Settings
UserAgent Property
- Description: Sets or gets the UserAgent string
- Requirement: Requires SupportsUserAgentFeatures support
- Type: String
- Example:
vb
' Check and set custom UserAgent
If WebView21.SupportsUserAgentFeatures Then
' Set custom UserAgent
WebView21.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) " & _
"AppleWebKit/537.36 (KHTML, like Gecko) " & _
"Chrome/91.0.4472.124 Safari/537.36 MyApp/1.0"
End If
' Set mobile device UserAgent
Private Sub SetMobileUserAgent()
If WebView21.SupportsUserAgentFeatures Then
WebView21.UserAgent = "Mozilla/5.0 (iPhone; CPU iPhone OS 14_6 like Mac OS X) " & _
"AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0 " & _
"Mobile/15E148 Safari/604.1"
End If
End Sub
AutoFill Settings
IsPasswordAutoSaveEnabled Property
- Description: Controls whether password autosave is enabled
- Requirement: Requires SupportsAutoFillFeatures support
- Type: Boolean
- Example:
vb
' Configure AutoFill features
Private Sub ConfigureAutoFill()
If WebView21.SupportsAutoFillFeatures Then
' Disable password saving for security
WebView21.IsPasswordAutoSaveEnabled = False
' Enable general form autofill
WebView21.IsGeneralAutoFillEnabled = True
End If
End Sub
IsGeneralAutoFillEnabled Property
- Description: Controls whether general form autofill is enabled
- Requirement: Requires SupportsAutoFillFeatures support
- Type: Boolean
- Example:
vb
' Set AutoFill preferences based on user settings
Private Sub SetAutoFillPreferences(ByVal EnableFormFill As Boolean, _
ByVal EnablePasswordSave As Boolean)
If WebView21.SupportsAutoFillFeatures Then
WebView21.IsGeneralAutoFillEnabled = EnableFormFill
WebView21.IsPasswordAutoSaveEnabled = EnablePasswordSave
End If
End Sub