共用方式為


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.

Example

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
using System;
using System.Windows.Forms;
using System.Security.Permissions;

[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
public class Form1 : Form
{
    public Form1()
    {
        // Create the form layout. If you are using Visual Studio, 
        // you can replace this code with code generated by the designer. 
        InitializeForm();

        // The following events are not visible in the designer, so 
        // you must associate them with their event-handlers in code.
        webBrowser1.CanGoBackChanged +=
            new EventHandler(webBrowser1_CanGoBackChanged);
        webBrowser1.CanGoForwardChanged +=
            new EventHandler(webBrowser1_CanGoForwardChanged);
        webBrowser1.DocumentTitleChanged +=
            new EventHandler(webBrowser1_DocumentTitleChanged);
        webBrowser1.StatusTextChanged +=
            new EventHandler(webBrowser1_StatusTextChanged);

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

    // Displays the Save dialog box.
    private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
    {
        webBrowser1.ShowSaveAsDialog();
    }

    // Displays the Page Setup dialog box.
    private void pageSetupToolStripMenuItem_Click(object sender, EventArgs e)
    {
        webBrowser1.ShowPageSetupDialog();
    }

    // Displays the Print dialog box.
    private void printToolStripMenuItem_Click(object sender, EventArgs e)
    {
        webBrowser1.ShowPrintDialog();
    }

    // Displays the Print Preview dialog box.
    private void printPreviewToolStripMenuItem_Click(
        object sender, EventArgs e)
    {
        webBrowser1.ShowPrintPreviewDialog();
    }

    // Displays the Properties dialog box.
    private void propertiesToolStripMenuItem_Click(
        object sender, EventArgs e)
    {
        webBrowser1.ShowPropertiesDialog();
    }

    // Selects all the text in the text box when the user clicks it. 
    private void toolStripTextBox1_Click(object sender, EventArgs e)
    {
        toolStripTextBox1.SelectAll();
    }

    // Navigates to the URL in the address box when 
    // the ENTER key is pressed while the ToolStripTextBox has focus.
    private void toolStripTextBox1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Enter)
        {
            Navigate(toolStripTextBox1.Text);
        }
    }

    // Navigates to the URL in the address box when 
    // the Go button is clicked.
    private void goButton_Click(object sender, EventArgs e)
    {
        Navigate(toolStripTextBox1.Text);
    }

    // Navigates to the given URL if it is valid.
    private void Navigate(String address)
    {
        if (String.IsNullOrEmpty(address)) return;
        if (address.Equals("about:blank")) return;
        if (!address.StartsWith("http://") &&
            !address.StartsWith("https://"))
        {
            address = "http://" + address;
        }
        try
        {
            webBrowser1.Navigate(new Uri(address));
        }
        catch (System.UriFormatException)
        {
            return;
        }
    }

    // Updates the URL in TextBoxAddress upon navigation.
    private void webBrowser1_Navigated(object sender,
        WebBrowserNavigatedEventArgs e)
    {
        toolStripTextBox1.Text = webBrowser1.Url.ToString();
    }

    // Navigates webBrowser1 to the previous page in the history.
    private void backButton_Click(object sender, EventArgs e)
    {
        webBrowser1.GoBack();
    }

    // Disables the Back button at the beginning of the navigation history.
    private void webBrowser1_CanGoBackChanged(object sender, EventArgs e)
    {
        backButton.Enabled = webBrowser1.CanGoBack;
    }

    // Navigates webBrowser1 to the next page in history.
    private void forwardButton_Click(object sender, EventArgs e)
    {
        webBrowser1.GoForward();
    }

    // Disables the Forward button at the end of navigation history.
    private void webBrowser1_CanGoForwardChanged(object sender, EventArgs e)
    {
        forwardButton.Enabled = webBrowser1.CanGoForward;
    }

    // Halts the current navigation and any sounds or animations on 
    // the page.
    private void stopButton_Click(object sender, EventArgs e)
    {
        webBrowser1.Stop();
    }

    // Reloads the current page.
    private void refreshButton_Click(object sender, EventArgs e)
    {
        // Skip refresh if about:blank is loaded to avoid removing
        // content specified by the DocumentText property.
        if (!webBrowser1.Url.Equals("about:blank"))
        {
            webBrowser1.Refresh();
        }
    }

    // Navigates webBrowser1 to the home page of the current user.
    private void homeButton_Click(object sender, EventArgs e)
    {
        webBrowser1.GoHome();
    }

    // Navigates webBrowser1 to the search page of the current user.
    private void searchButton_Click(object sender, EventArgs e)
    {
        webBrowser1.GoSearch();
    }

    // Prints the current document using the current print settings.
    private void printButton_Click(object sender, EventArgs e)
    {
        webBrowser1.Print();
    }

    // Updates the status bar with the current browser status text.
    private void webBrowser1_StatusTextChanged(object sender, EventArgs e)
    {
        toolStripStatusLabel1.Text = webBrowser1.StatusText;
    }

    // Updates the title bar with the current document title.
    private void webBrowser1_DocumentTitleChanged(object sender, EventArgs e)
    {
        this.Text = webBrowser1.DocumentTitle;
    }

    // Exits the application.
    private void exitToolStripMenuItem_Click(object sender, EventArgs e)
    {
        Application.Exit();
    }

    // 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,
    // and be sure to attach the event handlers to the associated events. 

    private WebBrowser webBrowser1;

    private MenuStrip menuStrip1;
    private ToolStripMenuItem fileToolStripMenuItem,
        saveAsToolStripMenuItem, printToolStripMenuItem,
        printPreviewToolStripMenuItem, exitToolStripMenuItem,
        pageSetupToolStripMenuItem, propertiesToolStripMenuItem;
    private ToolStripSeparator toolStripSeparator1, toolStripSeparator2;

    private ToolStrip toolStrip1, toolStrip2;
    private ToolStripTextBox toolStripTextBox1;
    private ToolStripButton goButton, backButton,
        forwardButton, stopButton, refreshButton,
        homeButton, searchButton, printButton;

    private StatusStrip statusStrip1;
    private ToolStripStatusLabel toolStripStatusLabel1;

    private void 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();

        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 | Keys.P;

        saveAsToolStripMenuItem.Click +=
            new System.EventHandler(saveAsToolStripMenuItem_Click);
        pageSetupToolStripMenuItem.Click +=
            new System.EventHandler(pageSetupToolStripMenuItem_Click);
        printToolStripMenuItem.Click +=
            new System.EventHandler(printToolStripMenuItem_Click);
        printPreviewToolStripMenuItem.Click +=
            new System.EventHandler(printPreviewToolStripMenuItem_Click);
        propertiesToolStripMenuItem.Click +=
            new System.EventHandler(propertiesToolStripMenuItem_Click);
        exitToolStripMenuItem.Click +=
            new System.EventHandler(exitToolStripMenuItem_Click);

        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;

        goButton.Click += new System.EventHandler(goButton_Click);
        backButton.Click += new System.EventHandler(backButton_Click);
        forwardButton.Click += new System.EventHandler(forwardButton_Click);
        stopButton.Click += new System.EventHandler(stopButton_Click);
        refreshButton.Click += new System.EventHandler(refreshButton_Click);
        homeButton.Click += new System.EventHandler(homeButton_Click);
        searchButton.Click += new System.EventHandler(searchButton_Click);
        printButton.Click += new System.EventHandler(printButton_Click);

        toolStrip2.Items.Add(toolStripTextBox1);
        toolStripTextBox1.Size = new System.Drawing.Size(250, 25);
        toolStripTextBox1.KeyDown +=
            new KeyEventHandler(toolStripTextBox1_KeyDown);
        toolStripTextBox1.Click +=
            new System.EventHandler(toolStripTextBox1_Click);

        statusStrip1.Items.Add(toolStripStatusLabel1);

        webBrowser1.Dock = DockStyle.Fill;
        webBrowser1.Navigated +=
            new WebBrowserNavigatedEventHandler(webBrowser1_Navigated);

        Controls.AddRange(new Control[] {
            webBrowser1, toolStrip2, toolStrip1, 
            menuStrip1, statusStrip1, menuStrip1 });
    }

    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.Run(new Form1());
    }

}
#using <System.Drawing.dll>
#using <System.dll>
#using <System.Windows.Forms.dll>

using namespace System;
using namespace System::Security::Permissions;

public ref class Form1: public System::Windows::Forms::Form
{
public:
   Form1()
   {
      //This call is required by the Windows Form Designer.
      InitializeComponent();
      
      //Add any initialization after the InitializeComponent() call
   }


private:

   //NOTE: The following procedure is required by the Windows Form Designer
   //It can be modified using the Windows Form Designer.  
   //Do not modify it using the code editor.
   System::Windows::Forms::MainMenu^ MainMenu1;
   System::Windows::Forms::MenuItem^ MenuItemFile;
   System::Windows::Forms::MenuItem^ MenuItemFileSaveAs;
   System::Windows::Forms::MenuItem^ MenuItemFilePageSetup;
   System::Windows::Forms::MenuItem^ MenuItemFilePrint;
   System::Windows::Forms::MenuItem^ MenuItemFilePrintPreview;
   System::Windows::Forms::MenuItem^ MenuItemFileProperties;
   System::Windows::Forms::TextBox^ TextBoxAddress;
   System::Windows::Forms::Button^ ButtonGo;
   System::Windows::Forms::Button^ ButtonBack;
   System::Windows::Forms::Button^ ButtonForward;
   System::Windows::Forms::Button^ ButtonStop;
   System::Windows::Forms::Button^ ButtonRefresh;
   System::Windows::Forms::Button^ ButtonHome;
   System::Windows::Forms::Button^ ButtonSearch;
   System::Windows::Forms::Panel^ Panel1;
   System::Windows::Forms::WebBrowser ^ WebBrowser1;
   System::Windows::Forms::StatusBar^ StatusBar1;
   System::Windows::Forms::MenuItem^ MenuItem1;
   System::Windows::Forms::MenuItem^ MenuItem2;
   System::Windows::Forms::Button^ ButtonPrint;

   /// <summary>
   /// Required method for Designer support - do not modify
   /// the contents of this method with the code editor.
   /// </summary>
   [SecurityPermission(SecurityAction::Demand, Flags=SecurityPermissionFlag::UnmanagedCode)]
   void InitializeComponent()
   {
      this->MainMenu1 = gcnew System::Windows::Forms::MainMenu;
      this->MenuItemFile = gcnew System::Windows::Forms::MenuItem;
      this->MenuItemFileSaveAs = gcnew System::Windows::Forms::MenuItem;
      this->MenuItem1 = gcnew System::Windows::Forms::MenuItem;
      this->MenuItemFilePageSetup = gcnew System::Windows::Forms::MenuItem;
      this->MenuItemFilePrint = gcnew System::Windows::Forms::MenuItem;
      this->MenuItemFilePrintPreview = gcnew System::Windows::Forms::MenuItem;
      this->MenuItem2 = gcnew System::Windows::Forms::MenuItem;
      this->MenuItemFileProperties = gcnew System::Windows::Forms::MenuItem;
      this->TextBoxAddress = gcnew System::Windows::Forms::TextBox;
      this->ButtonGo = gcnew System::Windows::Forms::Button;
      this->ButtonBack = gcnew System::Windows::Forms::Button;
      this->ButtonForward = gcnew System::Windows::Forms::Button;
      this->ButtonStop = gcnew System::Windows::Forms::Button;
      this->ButtonRefresh = gcnew System::Windows::Forms::Button;
      this->ButtonHome = gcnew System::Windows::Forms::Button;
      this->ButtonSearch = gcnew System::Windows::Forms::Button;
      this->ButtonPrint = gcnew System::Windows::Forms::Button;
      this->Panel1 = gcnew System::Windows::Forms::Panel;
      this->WebBrowser1 = gcnew System::Windows::Forms::WebBrowser;
      this->StatusBar1 = gcnew System::Windows::Forms::StatusBar;
      this->Panel1->SuspendLayout();
      this->SuspendLayout();

      // 
      // MainMenu1
      // 
      array<System::Windows::Forms::MenuItem^>^temp0 = {this->MenuItemFile};
      this->MainMenu1->MenuItems->AddRange( temp0 );
      this->MainMenu1->Name = "MainMenu1";

      // 
      // MenuItemFile
      // 
      this->MenuItemFile->Index = 0;
      array<System::Windows::Forms::MenuItem^>^temp1 = {this->MenuItemFileSaveAs,this->MenuItem1,this->MenuItemFilePageSetup,this->MenuItemFilePrint,this->MenuItemFilePrintPreview,this->MenuItem2,this->MenuItemFileProperties};
      this->MenuItemFile->MenuItems->AddRange( temp1 );
      this->MenuItemFile->Name = "MenuItemFile";
      this->MenuItemFile->Text = "&File";

      // 
      // MenuItemFileSaveAs
      // 
      this->MenuItemFileSaveAs->Index = 0;
      this->MenuItemFileSaveAs->Name = "MenuItemFileSaveAs";
      this->MenuItemFileSaveAs->Text = " Save &As";
      this->MenuItemFileSaveAs->Click += gcnew System::EventHandler( this, &Form1::MenuItemFileSaveAs_Click );

      // 
      // MenuItem1
      // 
      this->MenuItem1->Index = 1;
      this->MenuItem1->Name = "MenuItem1";
      this->MenuItem1->Text = "-";

      // 
      // MenuItemFilePageSetup
      // 
      this->MenuItemFilePageSetup->Index = 2;
      this->MenuItemFilePageSetup->Name = "MenuItemFilePageSetup";
      this->MenuItemFilePageSetup->Text = "Page Set&up...";
      this->MenuItemFilePageSetup->Click += gcnew System::EventHandler( this, &Form1::MenuItemFilePageSetup_Click );

      // 
      // MenuItemFilePrint
      // 
      this->MenuItemFilePrint->Index = 3;
      this->MenuItemFilePrint->Name = "MenuItemFilePrint";
      this->MenuItemFilePrint->Text = "&Print...";
      this->MenuItemFilePrint->Click += gcnew System::EventHandler( this, &Form1::MenuItemFilePrint_Click );

      // 
      // MenuItemFilePrintPreview
      // 
      this->MenuItemFilePrintPreview->Index = 4;
      this->MenuItemFilePrintPreview->Name = "MenuItemFilePrintPreview";
      this->MenuItemFilePrintPreview->Text = "Print Pre&view...";
      this->MenuItemFilePrintPreview->Click += gcnew System::EventHandler( this, &Form1::MenuItemFilePrintPreview_Click );

      // 
      // MenuItem2
      // 
      this->MenuItem2->Index = 5;
      this->MenuItem2->Name = "MenuItem2";
      this->MenuItem2->Text = "-";

      // 
      // MenuItemFileProperties
      // 
      this->MenuItemFileProperties->Index = 6;
      this->MenuItemFileProperties->Name = "MenuItemFileProperties";
      this->MenuItemFileProperties->Text = "P&roperties";
      this->MenuItemFileProperties->Click += gcnew System::EventHandler( this, &Form1::MenuItemFileProperties_Click );

      // 
      // TextBoxAddress
      // 
      this->TextBoxAddress->Location = System::Drawing::Point( 0, 0 );
      this->TextBoxAddress->Name = "TextBoxAddress";
      this->TextBoxAddress->Size = System::Drawing::Size( 240, 20 );
      this->TextBoxAddress->TabIndex = 1;
      this->TextBoxAddress->Text = "";
      this->TextBoxAddress->KeyDown += gcnew System::Windows::Forms::KeyEventHandler( this, &Form1::TextBoxAddress_KeyDown );

      // 
      // ButtonGo
      // 
      this->ButtonGo->Location = System::Drawing::Point( 240, 0 );
      this->ButtonGo->Name = "ButtonGo";
      this->ButtonGo->Size = System::Drawing::Size( 48, 24 );
      this->ButtonGo->TabIndex = 2;
      this->ButtonGo->Text = "Go";
      this->ButtonGo->Click += gcnew System::EventHandler( this, &Form1::ButtonGo_Click );

      // 
      // ButtonBack
      // 
      this->ButtonBack->Location = System::Drawing::Point( 288, 0 );
      this->ButtonBack->Name = "ButtonBack";
      this->ButtonBack->Size = System::Drawing::Size( 48, 24 );
      this->ButtonBack->TabIndex = 3;
      this->ButtonBack->Text = "Back";
      this->ButtonBack->Click += gcnew System::EventHandler( this, &Form1::ButtonBack_Click );

      // 
      // ButtonForward
      // 
      this->ButtonForward->Location = System::Drawing::Point( 336, 0 );
      this->ButtonForward->Name = "ButtonForward";
      this->ButtonForward->Size = System::Drawing::Size( 48, 24 );
      this->ButtonForward->TabIndex = 4;
      this->ButtonForward->Text = "Forward";
      this->ButtonForward->Click += gcnew System::EventHandler( this, &Form1::ButtonForward_Click );

      // 
      // ButtonStop
      // 
      this->ButtonStop->Location = System::Drawing::Point( 384, 0 );
      this->ButtonStop->Name = "ButtonStop";
      this->ButtonStop->Size = System::Drawing::Size( 48, 24 );
      this->ButtonStop->TabIndex = 5;
      this->ButtonStop->Text = "Stop";
      this->ButtonStop->Click += gcnew System::EventHandler( this, &Form1::ButtonStop_Click );

      // 
      // ButtonRefresh
      // 
      this->ButtonRefresh->Location = System::Drawing::Point( 432, 0 );
      this->ButtonRefresh->Name = "ButtonRefresh";
      this->ButtonRefresh->Size = System::Drawing::Size( 48, 24 );
      this->ButtonRefresh->TabIndex = 6;
      this->ButtonRefresh->Text = "Refresh";
      this->ButtonRefresh->Click += gcnew System::EventHandler( this, &Form1::ButtonRefresh_Click );

      // 
      // ButtonHome
      // 
      this->ButtonHome->Location = System::Drawing::Point( 480, 0 );
      this->ButtonHome->Name = "ButtonHome";
      this->ButtonHome->Size = System::Drawing::Size( 48, 24 );
      this->ButtonHome->TabIndex = 7;
      this->ButtonHome->Text = "Home";
      this->ButtonHome->Click += gcnew System::EventHandler( this, &Form1::ButtonHome_Click );

      // 
      // ButtonSearch
      // 
      this->ButtonSearch->Location = System::Drawing::Point( 528, 0 );
      this->ButtonSearch->Name = "ButtonSearch";
      this->ButtonSearch->Size = System::Drawing::Size( 48, 24 );
      this->ButtonSearch->TabIndex = 8;
      this->ButtonSearch->Text = "Search";
      this->ButtonSearch->Click += gcnew System::EventHandler( this, &Form1::ButtonSearch_Click );

      // 
      // ButtonPrint
      // 
      this->ButtonPrint->Location = System::Drawing::Point( 576, 0 );
      this->ButtonPrint->Name = "ButtonPrint";
      this->ButtonPrint->Size = System::Drawing::Size( 48, 24 );
      this->ButtonPrint->TabIndex = 9;
      this->ButtonPrint->Text = "Print";
      this->ButtonPrint->Click += gcnew System::EventHandler( this, &Form1::ButtonPrint_Click );

      // 
      // Panel1
      // 
      this->Panel1->Controls->Add( this->ButtonPrint );
      this->Panel1->Controls->Add( this->TextBoxAddress );
      this->Panel1->Controls->Add( this->ButtonGo );
      this->Panel1->Controls->Add( this->ButtonBack );
      this->Panel1->Controls->Add( this->ButtonForward );
      this->Panel1->Controls->Add( this->ButtonStop );
      this->Panel1->Controls->Add( this->ButtonRefresh );
      this->Panel1->Controls->Add( this->ButtonHome );
      this->Panel1->Controls->Add( this->ButtonSearch );
      this->Panel1->Dock = System::Windows::Forms::DockStyle::Top;
      this->Panel1->Location = System::Drawing::Point( 0, 0 );
      this->Panel1->Name = "Panel1";
      this->Panel1->Size = System::Drawing::Size( 624, 24 );
      this->Panel1->TabIndex = 11;

      // 
      // WebBrowser1
      // 
      this->WebBrowser1->AllowWebBrowserDrop = false;
      this->WebBrowser1->ScriptErrorsSuppressed = true;
      this->WebBrowser1->WebBrowserShortcutsEnabled = false;
      this->WebBrowser1->Dock = System::Windows::Forms::DockStyle::Fill;
      this->WebBrowser1->IsWebBrowserContextMenuEnabled = false;

      this->WebBrowser1->Location = System::Drawing::Point( 0, 24 );
      this->WebBrowser1->Name = "WebBrowser1";
      this->WebBrowser1->Size = System::Drawing::Size( 624, 389 );
      this->WebBrowser1->TabIndex = 10;
      this->WebBrowser1->StatusTextChanged += gcnew System::EventHandler( this, &Form1::WebBrowser1_StatusTextChanged );
      this->WebBrowser1->CanGoBackChanged += gcnew System::EventHandler( this, &Form1::WebBrowser1_CanGoBackChanged );
      this->WebBrowser1->Navigated += gcnew System::Windows::Forms::WebBrowserNavigatedEventHandler( this, &Form1::WebBrowser1_Navigated );
      this->WebBrowser1->CanGoForwardChanged += gcnew System::EventHandler( this, &Form1::WebBrowser1_CanGoForwardChanged );
      this->WebBrowser1->DocumentTitleChanged += gcnew System::EventHandler( this, &Form1::WebBrowser1_DocumentTitleChanged );

      // 
      // StatusBar1
      // 
      this->StatusBar1->Location = System::Drawing::Point( 0, 413 );
      this->StatusBar1->Name = "StatusBar1";
      this->StatusBar1->Size = System::Drawing::Size( 624, 16 );
      this->StatusBar1->TabIndex = 12;

      // 
      // Form1
      // 
      this->ClientSize = System::Drawing::Size( 624, 429 );
      this->Controls->Add( this->WebBrowser1 );
      this->Controls->Add( this->Panel1 );
      this->Controls->Add( this->StatusBar1 );
      this->Menu = this->MainMenu1;
      this->Name = "Form1";
      this->Text = "WebBrowser Example";
      this->Panel1->ResumeLayout( false );
      this->ResumeLayout( false );
   }


internal:

   static property Form1^ GetInstance 
   {
      Form1^ get()
      {
         if ( m_DefaultInstance == nullptr || m_DefaultInstance->IsDisposed )
         {
            System::Threading::Monitor::Enter( Form1::typeid );
            try
            {
               if ( m_DefaultInstance == nullptr || m_DefaultInstance->IsDisposed )
               {
                  m_DefaultInstance = gcnew Form1;
               }
            }
            finally
            {
               System::Threading::Monitor::Exit( Form1::typeid );
            }
         }

         return m_DefaultInstance;
      }
   }

private:
   static Form1^ m_DefaultInstance;

   // Displays the Save dialog box.
   void MenuItemFileSaveAs_Click( System::Object^ /*sender*/, System::EventArgs^ /*e*/ )
   {
      this->WebBrowser1->ShowSaveAsDialog();
   }

   // Displays the Page Setup dialog box.
   void MenuItemFilePageSetup_Click( System::Object^ /*sender*/, System::EventArgs^ /*e*/ )
   {
      this->WebBrowser1->ShowPageSetupDialog();
   }

   // Displays the Print dialog box.
   void MenuItemFilePrint_Click( System::Object^ /*sender*/, System::EventArgs^ /*e*/ )
   {
      this->WebBrowser1->ShowPrintDialog();
   }

   // Displays the Print Preview dialog box.
   void MenuItemFilePrintPreview_Click( System::Object^ /*sender*/, System::EventArgs^ /*e*/ )
   {
      this->WebBrowser1->ShowPrintPreviewDialog();
   }

   // Displays the Properties dialog box.
   void MenuItemFileProperties_Click( System::Object^ /*sender*/, System::EventArgs^ /*e*/ )
   {
      this->WebBrowser1->ShowPropertiesDialog();
   }

   // Navigates to the URL in the address text box when 
   // the ENTER key is pressed while the text box has focus.
   void TextBoxAddress_KeyDown( Object^ /*sender*/, System::Windows::Forms::KeyEventArgs^ e )
   {
      if ( e->KeyCode == System::Windows::Forms::Keys::Enter &&  !this->TextBoxAddress->Text->Equals( "" ) )
      {
         this->WebBrowser1->Navigate( this->TextBoxAddress->Text );
      }
   }

   // Navigates to the URL in the address text box when 
   // the Go button is clicked.
   void ButtonGo_Click( System::Object^ /*sender*/, System::EventArgs^ /*e*/ )
   {
      if (  !this->TextBoxAddress->Text->Equals( "" ) )
      {
         this->WebBrowser1->Navigate( this->TextBoxAddress->Text );
      }
   }

   // Updates the URL in TextBoxAddress upon navigation.
   void WebBrowser1_Navigated( Object^ /*sender*/, System::Windows::Forms::WebBrowserNavigatedEventArgs^ /*e*/ )
   {
      this->TextBoxAddress->Text = this->WebBrowser1->Url->ToString();
   }


   // Navigates WebBrowser1 to the previous page in the history.
   void ButtonBack_Click( System::Object^ /*sender*/, System::EventArgs^ /*e*/ )
   {
      this->WebBrowser1->GoBack();
   }

   // Disables the Back button at the beginning of the navigation history.
   void WebBrowser1_CanGoBackChanged( System::Object^ /*sender*/, System::EventArgs^ /*e*/ )
   {
      this->ButtonBack->Enabled = this->WebBrowser1->CanGoBack;
   }

   // Navigates WebBrowser1 to the next page in history.
   void ButtonForward_Click( System::Object^ /*sender*/, System::EventArgs^ /*e*/ )
   {
      this->WebBrowser1->GoForward();
   }

   // Disables the Forward button at the end of navigation history.
   void WebBrowser1_CanGoForwardChanged( System::Object^ /*sender*/, System::EventArgs^ /*e*/ )
   {
      this->ButtonForward->Enabled = this->WebBrowser1->CanGoForward;
   }

   // Halts the current navigation and any sounds or animations on 
   // the page.
   void ButtonStop_Click( System::Object^ /*sender*/, System::EventArgs^ /*e*/ )
   {
      this->WebBrowser1->Stop();
   }

   // Reloads the current page.
   void ButtonRefresh_Click( System::Object^ /*sender*/, System::EventArgs^ /*e*/ )
   {
      
      // Skip refresh if about:blank is loaded to avoid removing
      // content specified by the DocumentText property.
      if (  !this->WebBrowser1->Url->Equals( "about:blank" ) )
      {
         this->WebBrowser1->Refresh();
      }
   }

   // Navigates WebBrowser1 to the home page of the current user.
   void ButtonHome_Click( System::Object^ /*sender*/, System::EventArgs^ /*e*/ )
   {
      this->WebBrowser1->GoHome();
   }

   // Navigates WebBrowser1 to the search page of the current user.
   void ButtonSearch_Click( System::Object^ /*sender*/, System::EventArgs^ /*e*/ )
   {
      this->WebBrowser1->GoSearch();
   }

   // Prints the current document using the current print settings.
   void ButtonPrint_Click( System::Object^ /*sender*/, System::EventArgs^ /*e*/ )
   {
      this->WebBrowser1->Print();
   }

   // Updates StatusBar1 with the current browser status text.
   void WebBrowser1_StatusTextChanged( Object^ /*sender*/, System::EventArgs^ /*e*/ )
   {
      this->StatusBar1->Text = WebBrowser1->StatusText;
   }

   // Updates the title bar with the current document title.
   void WebBrowser1_DocumentTitleChanged( Object^ /*sender*/, System::EventArgs^ /*e*/ )
   {
      this->Text = WebBrowser1->DocumentTitle;
   }
};

/// <summary>
/// The main entry point for the application.
/// </summary>

[STAThread]
int main()
{
   System::Windows::Forms::Application::Run( gcnew Form1 );
}

Compiling the Code

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 (Visual Basic) or Command-Line Building. You can also build this example in Visual Studio by pasting the code into a new project. How to: Compile and Run a Complete Windows Forms Code Example Using Visual Studio
How to: Compile and Run a Complete Windows Forms Code Example Using Visual Studio
How to: Compile and Run a Complete Windows Forms Code Example Using Visual Studio
How to: Compile and Run a Complete Windows Forms Code Example Using Visual Studio

See Also

Reference

WebBrowser

Other Resources

WebBrowser Control (Windows Forms)