Quick Start for WinForms
In this article, you will learn how to download the DotNetBrowser library, get an evaluation license, and create, configure and run your first .NET WinForms application which loads and displays the DotNetBrowser Home Page.
Environment:
Windows 10, Microsoft Visual Studio 2017, .NET Framework 4.6.
The sample solution which demonstrates how to embed lightweight or heavyweight controls into WinForms applications can be downloaded here. The projects in this solution have NuGet dependencies which will be resolved automatically during build.
1. Download the library
To download DotNetBrowser library, navigate to http://www.teamdev.com/dotnetbrowser and click the Download button. Extract the downloaded dotnetbrowser-2.x.zip archive into the D:\Projects\DotNetBrowser\
directory.
2. Get license
To get a free 30-day evaluation license, fill the web form and click the GET EVALUATION button. You will receive an email with the license key that you should use to run the example.
3. Create a Windows Forms application
Create a new WinForms.DotNetBrowser
WinForms Application C# Project or WinForms Application Visual Basic Project in the D:\Projects\DotNetBrowser
directory:
4. Add DotNetBrowser DLL assemblies
In the Solution Explorer, right-click References and select the Add References… menu item:
In the Reference Manager which opens, click the Browse button:
Select the DotNetBrowser.dll
, DotNetBrowser.Core.dll
, DotNetBrowser.Logging.dll
, DotNetBrowser-Chromium.Win-x86.dll
, DotNetBrowser-Chromium.Win-x64.dll
, DotNetBrowser.WinForms.dll
files and click Add:
Double-check all selected references are added and appear in the Reference Manager dialog and click OK:
5. Add a license
To embed the license key into your project, copy the license key string from the email and insert it as shown below:
IEngine engine = EngineFactory.Create(new EngineOptions.Builder
{
LicenseKey = "your_license_key"
}.Build());
Dim engine As IEngine = EngineFactory.Create(New EngineOptions.Builder With
{
.LicenseKey = "your_license_key"
}.Build())
For more information on license installation, refer to this article.
6. Change the source code
Insert the following code into the Form1.cs Form1.vb file:
using System.Windows.Forms;
using DotNetBrowser.Browser;
using DotNetBrowser.Engine;
using DotNetBrowser.WinForms;
namespace Embedding.WinForms
{
/// <summary>
/// This example demonstrates how to embed DotNetBrowser
/// into a Windows Forms application.
/// </summary>
public partial class Form1 : Form
{
private const string Url = "https://html5test.com/";
private readonly IBrowser browser;
private readonly IEngine engine;
public Form1()
{
// Create the Windows Forms BrowserView control.
BrowserView browserView = new BrowserView
{
Dock = DockStyle.Fill
};
// Create and initialize the IEngine instance.
EngineOptions engineOptions = new EngineOptions.Builder
{
RenderingMode = RenderingMode.HardwareAccelerated
}.Build();
engine = EngineFactory.Create(engineOptions);
// Create the IBrowser instance.
browser = engine.CreateBrowser();
InitializeComponent();
// Add the BrowserView control to the Form.
Controls.Add(browserView);
FormClosed += Form1_FormClosed;
// Initialize the Windows Forms BrowserView control.
browserView.InitializeFrom(browser);
browser.Navigation.LoadUrl(Url);
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
browser?.Dispose();
engine?.Dispose();
}
}
}
Imports System.Windows.Forms
Imports DotNetBrowser.Browser
Imports DotNetBrowser.Engine
Imports DotNetBrowser.WinForms
Namespace Embedding.WinForms
''' <summary>
''' This example demonstrates how to embed DotNetBrowser
''' into a Windows Forms application.
''' </summary>
Partial Public Class Form1
Inherits Form
Private Const Url As String = "https://html5test.com/"
Private ReadOnly browser As IBrowser
Private ReadOnly engine As IEngine
Public Sub New()
' Create the Windows Forms BrowserView control.
Dim browserView As New BrowserView With {.Dock = DockStyle.Fill}
' Create and initialize the IEngine instance.
Dim engineOptions As EngineOptions = New EngineOptions.Builder With {
.RenderingMode = RenderingMode.HardwareAccelerated
}.Build()
engine = EngineFactory.Create(engineOptions)
' Create the IBrowser instance.
browser = engine.CreateBrowser()
InitializeComponent()
' Add the BrowserView control to the Form.
Controls.Add(browserView)
AddHandler FormClosed, AddressOf Form1_FormClosed
' Initialize the Windows Forms BrowserView control.
browserView.InitializeFrom(browser)
browser.Navigation.LoadUrl(Url)
End Sub
Private Sub Form1_FormClosed(sender As Object, e As FormClosedEventArgs)
browser?.Dispose()
engine?.Dispose()
End Sub
End Class
End Namespace
The complete project is available in our repository: C#, VB.
7. Run the application
To run the application, press F5 or click the Start button on the toolbar. The Form1 window opens: