# Building a browser shell

A short worked tutorial: turn a [**CefBrowser**](/tB/Packages/CEF/CefBrowser/index.md) control into a working browser with an address bar, back / forward / reload buttons, zoom, and a few helpers (DevTools, PDF export).

The complete project ships as *Sample 1b --- Chromium Embedded Framework Examples*, the card **Chromium Embedded Framework Examples** in the **Samples** section of the New-Project dialog (form *Example 1*). This tutorial describes its key pieces.

## The form

Drop a [**CefBrowser**](/tB/Packages/CEF/CefBrowser/index.md) control onto a Form and rename it `WebView`. Around it, add a `TextBox` named `AddressBar` plus six `CommandButton`s --- `btnBack`, `btnForward`, `btnRefresh`, `btnZoomIn`, `btnZoomOut`, `btnPDF`, `btnDevTools`.

## Navigating

The bare-bones navigation methods --- [**Navigate**](/tB/Packages/CEF/CefBrowser/index.md#navigate), [**GoBack**](/tB/Packages/CEF/CefBrowser/index.md#goback), [**GoForward**](/tB/Packages/CEF/CefBrowser/index.md#goforward), [**Reload**](/tB/Packages/CEF/CefBrowser/index.md#reload) --- are one-liners:

```tb check_build
Private Sub btnBack_Click() Handles btnBack.Click
    WebView.GoBack()
End Sub

Private Sub btnForward_Click() Handles btnForward.Click
    WebView.GoForward()
End Sub

Private Sub btnRefresh_Click() Handles btnRefresh.Click
    WebView.Reload()
End Sub
```

To make the back / forward buttons follow the actual history state, sync them against [**CanGoBack**](/tB/Packages/CEF/CefBrowser/index.md#cangoback) and [**CanGoForward**](/tB/Packages/CEF/CefBrowser/index.md#cangoforward) after every navigation:

```tb check_build
Private Sub WebView_NavigationComplete( _
        ByVal IsSuccess As Boolean, ByVal WebErrorStatus As Long) _
        Handles WebView.NavigationComplete
    btnBack.Enabled = WebView.CanGoBack
    btnForward.Enabled = WebView.CanGoForward
End Sub
```

> [!NOTE]
> *IsSuccess* and *WebErrorStatus* are part of the event signature but currently return placeholder values (`True` and `0`) --- use [**DocumentURL**](/tB/Packages/CEF/CefBrowser/index.md#documenturl) to confirm where the browser actually landed.

## The address bar

Pressing **Enter** in the address bar triggers a navigation. The reverse direction --- keeping the visible URL in sync with the page --- is the [**SourceChanged**](/tB/Packages/CEF/CefBrowser/index.md#sourcechanged) event, which fires whenever [**DocumentURL**](/tB/Packages/CEF/CefBrowser/index.md#documenturl) changes (including same-document `history.pushState` updates):

```tb check_build
Private Sub AddressBar_KeyDown(KeyCode As Integer, Shift As Integer) _
        Handles AddressBar.KeyDown
    If KeyCode = vbKeyReturn Then WebView.Navigate AddressBar.Text
End Sub

Private Sub WebView_SourceChanged(ByVal IsNewDocument As Boolean) _
        Handles WebView.SourceChanged
    AddressBar.Text = WebView.DocumentURL
End Sub
```

[**Navigate**](/tB/Packages/CEF/CefBrowser/index.md#navigate) requires a full URI with scheme --- `http://`, `https://`, `file://`, … Unlike [**WebView2**](/tB/Packages/WebView2/WebView2/index.md#navigate), no automatic `https://` prefix is added when the scheme is missing.

## Zoom

[**ZoomFactor**](/tB/Packages/CEF/CefBrowser/index.md#zoomfactor) is a **Double** --- `1.0` is 100%, `1.5` is 150%. The value reads as `0` until the browser has reached [**Ready**](/tB/Packages/CEF/CefBrowser/index.md#ready), so arithmetic that multiplies the current value silently starts from zero unless you clamp first:

```tb check_build
Private Sub btnZoomIn_Click() Handles btnZoomIn.Click
    If WebView.ZoomFactor = 0 Then WebView.ZoomFactor = 1
    On Error Resume Next
    WebView.ZoomFactor *= 1.1
End Sub

Private Sub btnZoomOut_Click() Handles btnZoomOut.Click
    If WebView.ZoomFactor = 0 Then WebView.ZoomFactor = 1
    On Error Resume Next
    WebView.ZoomFactor /= 1.1
End Sub
```

The `On Error Resume Next` catches the "control not ready" error that fires when the button is clicked before [**Ready**](/tB/Packages/CEF/CefBrowser/index.md#ready) has fired.

## PDF export

[**PrintToPdf**](/tB/Packages/CEF/CefBrowser/index.md#printtopdf) saves the current document to disk asynchronously --- the result arrives as [**PrintToPdfCompleted**](/tB/Packages/CEF/CefBrowser/index.md#printtopdfcompleted) or [**PrintToPdfFailed**](/tB/Packages/CEF/CefBrowser/index.md#printtopdffailed):

```tb check_build
Private Sub btnPDF_Click() Handles btnPDF.Click
    Dim outputPath As String = _
        Environ$("USERPROFILE") & "\Documents\page.pdf"
    WebView.PrintToPdf(outputPath)
End Sub

Private Sub WebView_PrintToPdfCompleted() Handles WebView.PrintToPdfCompleted
    MsgBox "PDF saved.", vbInformation
End Sub
```

The optional parameters that follow *outputPath* --- [**cefPrintOrientation**](/tB/Packages/CEF/Enumerations/cefPrintOrientation.md), page size in microns, margins, header/footer toggles --- let the host override Chromium's defaults. See the [**PrintToPdf** reference](/tB/Packages/CEF/CefBrowser/index.md#printtopdf) for the full signature.

## DevTools

The Chromium DevTools window opens in its own top-level window:

```tb check_build
Private Sub btnDevTools_Click() Handles btnDevTools.Click
    WebView.OpenDevToolsWindow()
End Sub
```

The CEF package does not currently expose **WebView2**'s **OpenTaskManagerWindow** equivalent --- see the [WebView2 parity](/tB/Packages/CEF/index.md#webview2-parity) section of the reference for the current gap list.

## Form-title sync

To make the host window's caption track the page's `<title>`, listen for [**DocumentTitleChanged**](/tB/Packages/CEF/CefBrowser/index.md#documenttitlechanged) and read [**DocumentTitle**](/tB/Packages/CEF/CefBrowser/index.md#documenttitle):

```tb inert=designer
Private Sub WebView_DocumentTitleChanged() Handles WebView.DocumentTitleChanged
    Me.Caption = WebView.DocumentTitle
End Sub
```

## Where next

- [Hosting local web assets](/Tutorials/CEF/Hosting-Local-Web-Assets.md) -- serve HTML / JS / CSS from a folder without an HTTP server.
- [JavaScript interop](/Tutorials/CEF/JavaScript-Interop.md) -- pass values and method calls between BASIC and the page.
- [Re-entrancy](/Tutorials/CEF/Re-entrancy.md) -- the one thing to know about [**JsRun**](/tB/Packages/CEF/CefBrowser/index.md#jsrun) before you use it.
- [CefBrowser reference](/tB/Packages/CEF/CefBrowser/index.md) -- every property, method, and event.
