Quick start for WinForms

This guide shows how to start working with DotNetBrowser and embed it in a simple WinForms application.

Before you begin make sure that your system meets software and hardware requirements.

1. Create a Windows Forms application

Create a new Embedding.WinForms WinForms Application C# Project or WinForms Application Visual Basic Project:

Winforms Project

2. Add DotNetBrowser to project.

In the Solution Explorer, right-click References and select the Manage NuGet Packages option:

Manage NuGet Packages

Choose “nuget.org” as the Package source, select the Browse tab, search for “DotNetBrowser”, select the DotNetBrowser.WinForms package and hit Install:

WinForms package

Accept license prompt to continue installation.

3. 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.teamdev.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.teamdev.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.

4. Get trial license

To get a free 30-day trial license, fill the web form and click the Get a free trial button. You will receive an email with the license key.

5. Add license

To embed the license key into your project, copy the license key string from the email and insert it as shown below:

EngineOptions engineOptions = new EngineOptions.Builder
{
    RenderingMode = RenderingMode.HardwareAccelerated,
    LicenseKey = "your_license_key"
}.Build();
Dim engineOptions As EngineOptions = New EngineOptions.Builder With {
    .RenderingMode = RenderingMode.HardwareAccelerated,
    .LicenseKey = "your_license_key"
}.Build()

For more information on license installation, refer to this article.

6. Run the application

To run the application, press F5 or click the Start button on the toolbar. The Form1 window opens:

Application Launch

Go Top