How to: Add Web Browser Capabilities to a Windows Forms Application

 

With the WebBrowser control, you can add Web browser functionality to your application. The control works like a Web browser by default. After you load an initial URL by setting the Url property, you can navigate by clicking hyperlinks or by using keyboard shortcuts to move backward and forward through navigation history. By default, you can access additional browser functionality through the right-click shortcut menu. You can also open new documents by dropping them onto the control. The WebBrowser control also has several properties, methods, and events that you can use to implement user interface features similar to those found in Internet Explorer.

The following code example implements an address bar, typical browser buttons, a File menu, a status bar, and a title bar that displays the current page title.

Imports System
Imports System.Windows.Forms
Imports System.Security.Permissions

<PermissionSet(SecurityAction.Demand, Name:="FullTrust")> _
Public Class Form1
    Inherits Form

    Public Sub New()

        ' Create the form layout. If you are using Visual Studio, 
        ' you can replace this code with code generated by the designer. 
        InitializeForm()

        ' Load the user's home page.
        webBrowser1.GoHome()

    End Sub


    ' Displays the Save dialog box.
    Private Sub saveAsToolStripMenuItem_Click( _
        ByVal sender As Object, ByVal e As EventArgs) _
        Handles saveAsToolStripMenuItem.Click

        webBrowser1.ShowSaveAsDialog()

    End Sub

    ' Displays the Page Setup dialog box.
    Private Sub pageSetupToolStripMenuItem_Click( _
        ByVal sender As Object, ByVal e As EventArgs) _
        Handles pageSetupToolStripMenuItem.Click

        webBrowser1.ShowPageSetupDialog()

    End Sub

    ' Displays the Print dialog box.
    Private Sub printToolStripMenuItem_Click( _
        ByVal sender As Object, ByVal e As EventArgs) _
        Handles printToolStripMenuItem.Click

        webBrowser1.ShowPrintDialog()

    End Sub

    ' Displays the Print Preview dialog box.
    Private Sub printPreviewToolStripMenuItem_Click( _
        ByVal sender As Object, ByVal e As EventArgs) _
        Handles printPreviewToolStripMenuItem.Click

        webBrowser1.ShowPrintPreviewDialog()

    End Sub

    ' Displays the Properties dialog box.
    Private Sub propertiesToolStripMenuItem_Click( _
        ByVal sender As Object, ByVal e As EventArgs) _
        Handles propertiesToolStripMenuItem.Click

        webBrowser1.ShowPropertiesDialog()

    End Sub

    ' Selects all the text in the text box when the user clicks it. 
    Private Sub toolStripTextBox1_Click( _
        ByVal sender As Object, ByVal e As EventArgs) _
        Handles toolStripTextBox1.Click

        toolStripTextBox1.SelectAll()

    End Sub

    ' Navigates to the URL in the address box when 
    ' the ENTER key is pressed while the ToolStripTextBox has focus.
    Private Sub toolStripTextBox1_KeyDown( _
        ByVal sender As Object, ByVal e As KeyEventArgs) _
        Handles toolStripTextBox1.KeyDown

        If (e.KeyCode = Keys.Enter) Then
            Navigate(toolStripTextBox1.Text)
        End If

    End Sub

    ' Navigates to the URL in the address box when 
    ' the Go button is clicked.
    Private Sub goButton_Click( _
        ByVal sender As Object, ByVal e As EventArgs) _
        Handles goButton.Click

        Navigate(toolStripTextBox1.Text)

    End Sub

    ' Navigates to the given URL if it is valid.
    Private Sub Navigate(ByVal address As String)

        If String.IsNullOrEmpty(address) Then Return
        If address.Equals("about:blank") Then Return
        If Not address.StartsWith("http://") And _
            Not address.StartsWith("https://") Then
            address = "http://" & address
        End If

        Try
            webBrowser1.Navigate(New Uri(address))
        Catch ex As System.UriFormatException
            Return
        End Try

    End Sub

    ' Updates the URL in TextBoxAddress upon navigation.
    Private Sub webBrowser1_Navigated(ByVal sender As Object, _
        ByVal e As WebBrowserNavigatedEventArgs) _
        Handles webBrowser1.Navigated

        toolStripTextBox1.Text = webBrowser1.Url.ToString()

    End Sub

    ' Navigates webBrowser1 to the previous page in the history.
    Private Sub backButton_Click( _
        ByVal sender As Object, ByVal e As EventArgs) _
        Handles backButton.Click

        webBrowser1.GoBack()

    End Sub

    ' Disables the Back button at the beginning of the navigation history.
    Private Sub webBrowser1_CanGoBackChanged( _
        ByVal sender As Object, ByVal e As EventArgs) _
        Handles webBrowser1.CanGoBackChanged

        backButton.Enabled = webBrowser1.CanGoBack

    End Sub

    ' Navigates webBrowser1 to the next page in history.
    Private Sub forwardButton_Click( _
        ByVal sender As Object, ByVal e As EventArgs) _
        Handles forwardButton.Click

        webBrowser1.GoForward()

    End Sub

    ' Disables the Forward button at the end of navigation history.
    Private Sub webBrowser1_CanGoForwardChanged( _
        ByVal sender As Object, ByVal e As EventArgs) _
        Handles webBrowser1.CanGoForwardChanged

        forwardButton.Enabled = webBrowser1.CanGoForward

    End Sub

    ' Halts the current navigation and any sounds or animations on 
    ' the page.
    Private Sub stopButton_Click( _
        ByVal sender As Object, ByVal e As EventArgs) _
        Handles stopButton.Click

        webBrowser1.Stop()

    End Sub

    ' Reloads the current page.
    Private Sub refreshButton_Click( _
        ByVal sender As Object, ByVal e As EventArgs) _
        Handles refreshButton.Click

        ' Skip refresh if about:blank is loaded to avoid removing
        ' content specified by the DocumentText property.
        If Not webBrowser1.Url.Equals("about:blank") Then
            webBrowser1.Refresh()
        End If

    End Sub

    ' Navigates webBrowser1 to the home page of the current user.
    Private Sub homeButton_Click( _
        ByVal sender As Object, ByVal e As EventArgs) _
        Handles homeButton.Click

        webBrowser1.GoHome()

    End Sub

    ' Navigates webBrowser1 to the search page of the current user.
    Private Sub searchButton_Click( _
        ByVal sender As Object, ByVal e As EventArgs) _
        Handles searchButton.Click

        webBrowser1.GoSearch()

    End Sub

    ' Prints the current document Imports the current print settings.
    Private Sub printButton_Click( _
        ByVal sender As Object, ByVal e As EventArgs) _
        Handles printButton.Click

        webBrowser1.Print()

    End Sub

    ' Updates the status bar with the current browser status text.
    Private Sub webBrowser1_StatusTextChanged( _
        ByVal sender As Object, ByVal e As EventArgs) _
        Handles webBrowser1.StatusTextChanged

        toolStripStatusLabel1.Text = webBrowser1.StatusText

    End Sub

    ' Updates the title bar with the current document title.
    Private Sub webBrowser1_DocumentTitleChanged( _
        ByVal sender As Object, ByVal e As EventArgs) _
        Handles webBrowser1.DocumentTitleChanged

        Me.Text = webBrowser1.DocumentTitle

    End Sub

    ' Exits the application.
    Private Sub exitToolStripMenuItem_Click( _
        ByVal sender As Object, ByVal e As EventArgs) _
        Handles exitToolStripMenuItem.Click

        Application.Exit()

    End Sub

    ' The remaining code in this file provides basic form initialization and 
    ' includes a Main method. If you use the Visual Studio designer to create
    ' your form, you can use the designer generated code instead of this code, 
    ' but be sure to use the names shown in the variable declarations here. 

    Private WithEvents webBrowser1 As WebBrowser

    Private menuStrip1 As MenuStrip
    Private WithEvents fileToolStripMenuItem, saveAsToolStripMenuItem, _
        printToolStripMenuItem, printPreviewToolStripMenuItem, _
        exitToolStripMenuItem, pageSetupToolStripMenuItem, _
        propertiesToolStripMenuItem As ToolStripMenuItem
    Private toolStripSeparator1, toolStripSeparator2 As ToolStripSeparator

    Private toolStrip1, toolStrip2 As ToolStrip
    Private WithEvents toolStripTextBox1 As ToolStripTextBox
    Private WithEvents goButton, backButton, forwardButton, _
        stopButton, refreshButton, homeButton, _
        searchButton, printButton As ToolStripButton

    Private statusStrip1 As StatusStrip
    Private toolStripStatusLabel1 As ToolStripStatusLabel

    Private Sub InitializeForm()

        webBrowser1 = New WebBrowser()

        menuStrip1 = New MenuStrip()
        fileToolStripMenuItem = New ToolStripMenuItem()
        saveAsToolStripMenuItem = New ToolStripMenuItem()
        toolStripSeparator1 = New ToolStripSeparator()
        printToolStripMenuItem = New ToolStripMenuItem()
        printPreviewToolStripMenuItem = New ToolStripMenuItem()
        toolStripSeparator2 = New ToolStripSeparator()
        exitToolStripMenuItem = New ToolStripMenuItem()
        pageSetupToolStripMenuItem = New ToolStripMenuItem()
        propertiesToolStripMenuItem = New ToolStripMenuItem()

        toolStrip1 = New ToolStrip()
        goButton = New ToolStripButton()
        backButton = New ToolStripButton()
        forwardButton = New ToolStripButton()
        stopButton = New ToolStripButton()
        refreshButton = New ToolStripButton()
        homeButton = New ToolStripButton()
        searchButton = New ToolStripButton()
        printButton = New ToolStripButton()

        toolStrip2 = New ToolStrip()
        toolStripTextBox1 = New ToolStripTextBox()

        statusStrip1 = New StatusStrip()
        toolStripStatusLabel1 = New ToolStripStatusLabel()

        webBrowser1.Dock = DockStyle.Fill

        menuStrip1.Items.Add(fileToolStripMenuItem)

        fileToolStripMenuItem.DropDownItems.AddRange( _
            New ToolStripItem() { _
                saveAsToolStripMenuItem, toolStripSeparator1, _
                pageSetupToolStripMenuItem, printToolStripMenuItem, _
                printPreviewToolStripMenuItem, toolStripSeparator2, _
                propertiesToolStripMenuItem, exitToolStripMenuItem _
            })

        fileToolStripMenuItem.Text = "&File"
        saveAsToolStripMenuItem.Text = "Save &As..."
        pageSetupToolStripMenuItem.Text = "Page Set&up..."
        printToolStripMenuItem.Text = "&Print..."
        printPreviewToolStripMenuItem.Text = "Print Pre&view..."
        propertiesToolStripMenuItem.Text = "Properties"
        exitToolStripMenuItem.Text = "E&xit"

        printToolStripMenuItem.ShortcutKeys = Keys.Control Or Keys.P

        toolStrip1.Items.AddRange(New ToolStripItem() { _
            goButton, backButton, forwardButton, stopButton, _
            refreshButton, homeButton, searchButton, printButton})

        goButton.Text = "Go"
        backButton.Text = "Back"
        forwardButton.Text = "Forward"
        stopButton.Text = "Stop"
        refreshButton.Text = "Refresh"
        homeButton.Text = "Home"
        searchButton.Text = "Search"
        printButton.Text = "Print"

        backButton.Enabled = False
        forwardButton.Enabled = False

        toolStrip2.Items.Add(toolStripTextBox1)
        toolStripTextBox1.Size = New System.Drawing.Size(250, 25)

        statusStrip1.Items.Add(toolStripStatusLabel1)

        Controls.AddRange(New Control() { _
            webBrowser1, toolStrip2, toolStrip1, _
            menuStrip1, statusStrip1, menuStrip1})
    End Sub

    <STAThread()> _
    Public Shared Sub Main()
        Application.EnableVisualStyles()
        Application.Run(New Form1())
    End Sub

End Class

This example requires:

  • References to the System,``System.Drawing, and System.Windows.Forms assemblies.

For information about building this example from the command line for Visual Basic or Visual C#, see Building from the Command Line or Command-line Building With csc.exe. You can also build this example in Visual Studio by pasting the code into a new project. Also see How to: Compile and Run a Complete Windows Forms Code Example Using Visual Studio.

WebBrowser
WebBrowser Control

Show: