
Webview2 VB.NET for WinForms and .NET 5 is a powerful combination for building desktop applications with a modern web browsing experience.
Webview2 allows you to easily embed web content into your WinForms applications, making it a great choice for developers looking to create rich and interactive user interfaces.
The Webview2 control for WinForms is compatible with .NET 5, enabling you to leverage the latest features and improvements of the .NET framework.
This means you can take advantage of the improved performance, security, and reliability of .NET 5 to build high-quality desktop applications with Webview2.
Getting Started
To get started with WebView2 in VB.Net, you'll need to install the package from NuGet. Drag and drop the WebView2 control onto a Form, and you're off the races.
You'll need to initialize the control by calling the EnsureCoreWebView2Async() method. This will guarantee that the CoreWebView2 property is initialized, so subsequent calls won't throw a null reference exception.
Here are the basic steps to initialize WebView2:
By following these simple steps, you'll be well on your way to getting started with WebView2 in VB.Net.
Initializing WebView
You can find the WebView2 control on NuGet, and installing it in a WinForms app makes it quickly available in the Toolbox.
To add it to a project, simply drag and drop it onto a Form.
Initializing the control is crucial to avoid null reference exceptions. You can do this by calling the EnsureCoreWebView2Async() method, which guarantees the CoreWebView2 property is initialized.
Here's a simple example of how to initialize the control in C#:
```csharp
public Form1(){InitializeComponent();InitializeWebView2();}
async void InitializeWebView2(){await webView2.EnsureCoreWebView2Async();}
```
In VB.Net, you can achieve the same result with the following code:
```vb
Private WithEvents webVW2 As New Microsoft.Web.WebView2.WinForms.WebView2
```
This code sets up the WebView2 control and ensures it's initialized before use.
In both cases, the CoreWebView2 property is accessed through the EnsureCoreWebView2Async() method, which is essential for subsequent calls to the control.
Customization
You can customize the WebView2 control in Visual Studio by dragging it onto the WinForms designer. It's pretty simple to get started.
Customizing the control involves accessing its properties, specifically the CoreWebView2 property, which must be done after the web view has initialized. This can be achieved by setting the Source property, which triggers initialization of the CoreWebView2.
You can also customize the control by setting the ZoomFactor property, as shown in the example: And there are other properties you can configure by accessing the CoreWebView2 property.
To disable the status bar tooltip at the bottom of the page, you can use the CoreWebView2 property in the OnWebViewInitializationComplete event handler.
Here's a summary of some of the properties you can configure:
Remember to set the Source property to initialize the CoreWebView2, and to use the CoreWebView2 property to perform more operations on the WebView2 content.
Navigation
Navigation is as easy as calling the Navigate() method. You can load any website by passing the URL to this method.
The only requirement is that the URL starts with either "https" or "http". This ensures a secure connection to the website.
Scripting
Scripting is a powerful feature of WebView2 in VB.NET. You can execute scripts in the top-level document using the ExecuteScriptAsync method.
This method works the same regardless of the page loaded, allowing for consistent scripting across different pages. The syntax is simple: webView2.ExecuteScriptAsync("your_script_here").
For example, you can use it to display an alert message, as shown in the example: webView2.ExecuteScriptAsync("alert('Hi there, hello.')");. This method is a great way to interact with the web page and perform tasks programmatically.
Execute a Script

Executing a script is a powerful tool in scripting, and it's surprisingly easy to do. You can execute a script in the top-level document using webView2.ExecuteScriptAsync, which works the same regardless of the page loaded.
This method takes a single argument, a string containing the script to be executed. For example, executing the script "alert('Hi there, hello.')" will display an alert box with the specified message.
The beauty of this method is that it's a one-liner, making it incredibly concise and easy to use.
Events
Events are a crucial part of scripting in WebView2, allowing you to interact with the control and respond to various actions.
The AcceleratorKeyPressed event is triggered when an accelerator key is pressed, giving you the opportunity to suppress handling by the WebView2 by setting e.Handled to true.
You can also handle the ContentLoading event, which is equivalent to the ContentLoading event and dispatches after a navigation begins to a new URI and the content of that URI begins to render.

The CoreWebView2InitializationCompleted and CoreWebView2Ready events are similar, both triggered when the control's CoreWebView2 has finished being initialized, but CoreWebView2Ready is more specific to one-time setup operations.
The NavigationCompleted event is triggered after a navigate of the top-level document completes rendering, while the NavigationStarting event is triggered before a new navigate starts for the top-level document.
Here are the events listed in a table for easy reference:
By handling these events, you can create a more interactive and responsive experience for your users.
Communication
Communication is a two-way street with WebView2 in VB.NET. You can send messages from your Form to the loaded website and vice versa.
To communicate between your Form and the loaded website, you need to subscribe to both events in the InitializeWebView2() method. This involves adding a WinForms event and a JavaScript event listener.
One of the key things to note is that you can send a message to the website using the PostWebMessageAsString() method. This method takes a string as an argument, which is then displayed in an alert box on the website.
You can also send messages from the website to your Form using the window.chrome.webview.postMessage() method in JavaScript. This method sends the message to the Form, which can then be displayed in a MessageBox.
Here's a breakdown of the communication process:
By using these methods, you can create a seamless communication channel between your Form and the loaded website. This allows you to send and receive messages, enabling a more interactive and dynamic experience for your users.
Remarks
The WebView2 control in VB.NET is a powerful tool, but it's essential to understand its initialization process. Upon creation, the control's CoreWebView2 property will be null, so you'll need to trigger initialization to get things started.
You have two options to create the CoreWebView2: explicit initialization through the EnsureCoreWebView2Async method or implicit initialization by setting the Source property. Both methods will start initialization in the background, but you'll need to wait for it to finish before using the control.
Initialization occurs in the following order: the CoreWebView2InitializationCompleted event is invoked, the control starts navigating to the specified Uri if set, and the Task returned from EnsureCoreWebView2Async completes. Make sure to check the specific documentation for each method involved in the initialization process.
Accelerator key presses, like Ctrl+P, will fire standard key press events, but you can suppress the control's default implementation by setting the Handled property of its EventArgs to true. Be cautious, though, as the underlying browser process is blocked while these handlers execute.
Here are some best practices to keep in mind when handling accelerator key presses:
- Avoid doing a lot of work in these handlers.
- Some WebView2 and CoreWebView2 APIs may throw errors if invoked within these handlers due to communication issues with the browser process.
Featured Images: pexels.com

