Windows Forms: Chromium Browser using CefSharp in C#

This post shows you How to make a Chromium Browser using CefSharp (Chromium Embedded Framework) in C# .NET Windows Forms Application.

Creating a new Windows Forms Application project. Next, Right-click on your project select Manage NuGet Packages -> Search cefsharp -> Install it.

How to embed Chrome browser in a WinForms Application

CefSharp is the the easiest way to embed a full-featured standards-complaint web browser into your C# or VB.NET app. CefSharp has browser controls for WinForms and WPF apps, and a headless (offscreen) version for automation projects too. CefSharp is based on Chromium Embedded Framework, the open source version of Google Chrome.

Open your form designer, then drag Button, TextBox and Panel control from the visual studio toolbox to your winform.

You can design a simple ui chromium web browser as show below.

install cefsharp windows forms c#

Adding the Form_Load event handler to your form that allows you to initialize the cefsharp as the following c# code.

private void Form1_Load(object sender, EventArgs e)
{
    CefSettings settings = new CefSettings();
    //Initialize
    Cef.Initialize(settings);
    txtUrl.Text = "https://foxlearn.com";
    chrome = new ChromiumWebBrowser(txtUrl.Text);
    this.pContainer.Controls.Add(chrome);
    chrome.Dock = DockStyle.Fill;
    chrome.AddressChanged += Chrome_AddressChanged;
}

Adding the AddressChanged event handler to the ChromiumWebBrowser allows you to change the address.

private void Chrome_AddressChanged(object sender, AddressChangedEventArgs e)
{
    this.Invoke(new MethodInvoker(() =>
    {
        txtUrl.Text = e.Address;
    }));
}

Adding the click event hanlder to the Go, Refresh, Forward and Back buttons as the following c# code.

private void btnGo_Click(object sender, EventArgs e)
{
    chrome.Load(txtUrl.Text);
}

private void btnRefresh_Click(object sender, EventArgs e)
{
    chrome.Refresh();
}

private void btnForward_Click(object sender, EventArgs e)
{
    if (chrome.CanGoForward)
        chrome.Forward();
}

private void btnBack_Click(object sender, EventArgs e)
{
    if (chrome.CanGoBack)
        chrome.Back();
}

And don't forget to call the Shutdown method in the FormClosing event handler.

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    Cef.Shutdown();
}

Through the c# example above, you've learned how to install cefsharp in windows forms application, use cefsharp to create a simple chromium web browser in c# .net.

VIDEO TUTORIAL