Click here to Skip to main content
Click here to Skip to main content
 
Add your own
alternative version

Extended .NET 2.0 WebBrowser Control

, 29 Mar 2006
Extending the .NET 2.0 WebBrowser control.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace ExtendedWebBrowser2
{
  [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1301:AvoidDuplicateAccelerators")]
  partial class MainForm : Form
  {
    public MainForm()
    {
      InitializeComponent();
      _windowManager = new WindowManager(this.tabControl);
      _windowManager.CommandStateChanged += new EventHandler<CommandStateEventArgs>(_windowManager_CommandStateChanged);
      _windowManager.StatusTextChanged += new EventHandler<TextChangedEventArgs>(_windowManager_StatusTextChanged);
    }

    // Update the status text
    void _windowManager_StatusTextChanged(object sender, TextChangedEventArgs e)
    {
      this.toolStripStatusLabel.Text = e.Text;
    }

    // Enable / disable buttons
    void _windowManager_CommandStateChanged(object sender, CommandStateEventArgs e)
    {
      this.forwardToolStripButton.Enabled = ((e.BrowserCommands & BrowserCommands.Forward) == BrowserCommands.Forward);
      this.backToolStripButton.Enabled = ((e.BrowserCommands & BrowserCommands.Back) == BrowserCommands.Back);
      this.printPreviewToolStripButton.Enabled = ((e.BrowserCommands & BrowserCommands.PrintPreview) == BrowserCommands.PrintPreview);
      this.printPreviewToolStripMenuItem.Enabled = ((e.BrowserCommands & BrowserCommands.PrintPreview) == BrowserCommands.PrintPreview);
      this.printToolStripButton.Enabled = ((e.BrowserCommands & BrowserCommands.Print) == BrowserCommands.Print);
      this.printToolStripMenuItem.Enabled = ((e.BrowserCommands & BrowserCommands.Print) == BrowserCommands.Print);
      this.homeToolStripButton.Enabled = ((e.BrowserCommands & BrowserCommands.Home) == BrowserCommands.Home);
      this.searchToolStripButton.Enabled = ((e.BrowserCommands & BrowserCommands.Search) == BrowserCommands.Search);
      this.refreshToolStripButton.Enabled = ((e.BrowserCommands & BrowserCommands.Reload) == BrowserCommands.Reload);
      this.stopToolStripButton.Enabled = ((e.BrowserCommands & BrowserCommands.Stop) == BrowserCommands.Stop);
    }

    #region Tools menu
    // Executed when the user clicks on Tools -> Options
    private void optionsToolStripMenuItem_Click(object sender, EventArgs e)
    {
      using (OptionsForm of = new OptionsForm())
      {
        of.ShowDialog(this);
      }
    }
    // Tools -> Show script errors
    private void scriptErrorToolStripMenuItem_Click(object sender, EventArgs e)
    {
      ScriptErrorManager.Instance.ShowWindow();
    }

    #endregion

    #region File Menu

    // File -> Print
    private void printToolStripMenuItem_Click(object sender, EventArgs e)
    {
      Print();
    }

    // File -> Print Preview
    private void printPreviewToolStripMenuItem_Click(object sender, EventArgs e)
    {
      PrintPreview();
    }

    // File -> Exit
    private void exitToolStripMenuItem_Click(object sender, EventArgs e)
    {
      this.Close();
    }

    // File -> Open URL
    private void openUrlToolStripMenuItem_Click(object sender, EventArgs e)
    {
      using (OpenUrlForm ouf = new OpenUrlForm())
      {
        if (ouf.ShowDialog() == DialogResult.OK)
        {
          ExtendedWebBrowser brw = _windowManager.New(false);
          brw.Navigate(ouf.Url);
        }
      }
    }

    // File -> Open File
    private void openFileToolStripMenuItem_Click(object sender, EventArgs e)
    {
      using (OpenFileDialog ofd = new OpenFileDialog())
      {
        ofd.Filter = Properties.Resources.OpenFileDialogFilter;
        if (ofd.ShowDialog() == DialogResult.OK)
        {
          Uri url = new Uri(ofd.FileName);
          WindowManager.Open(url);
        }
      }
    }
    #endregion

    #region Help Menu
    
    // Executed when the user clicks on Help -> About
    private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
    {
      About();
    }

    /// <summary>
    /// Shows the AboutForm
    /// </summary>
    private void About()
    {
      using (AboutForm af = new AboutForm())
      {
        af.ShowDialog(this);
      }
    }

    #endregion


    /// <summary>
    /// The WindowManager class
    /// </summary>
    private WindowManager _windowManager;

    // This is handy when all the tabs are closed.
    private void tabControl_VisibleChanged(object sender, EventArgs e)
    {
      if (tabControl.Visible)
      {
        this.panel1.BackColor = SystemColors.Control;
      }
      else
        this.panel1.BackColor = SystemColors.AppWorkspace;
    }

    // Starting the app here...
    private void MainForm_Load(object sender, EventArgs e)
    {
      // Open a new browser window
      _windowManager.New();
    }


    #region Printing & Print Preview
    private void Print()
    {
      ExtendedWebBrowser brw = _windowManager.ActiveBrowser;
      if (brw != null)
        brw.ShowPrintDialog();
    }

    private void PrintPreview()
    {
      ExtendedWebBrowser brw = _windowManager.ActiveBrowser;
      if (brw != null)
        brw.ShowPrintPreviewDialog();
    }
    #endregion

    #region Toolstrip buttons
    private void closeWindowToolStripButton_Click(object sender, EventArgs e)
    {
      this._windowManager.New();
    }

    private void closeToolStripButton_Click(object sender, EventArgs e)
    {
      this._windowManager.Close();
    }

    private void printToolStripButton_Click(object sender, EventArgs e)
    {
      Print();
    }

    private void printPreviewToolStripButton_Click(object sender, EventArgs e)
    {
      PrintPreview();
    }

    private void backToolStripButton_Click(object sender, EventArgs e)
    {
      if (_windowManager.ActiveBrowser != null && _windowManager.ActiveBrowser.CanGoBack)
        _windowManager.ActiveBrowser.GoBack();
    }

    private void forwardToolStripButton_Click(object sender, EventArgs e)
    {
      if (_windowManager.ActiveBrowser != null && _windowManager.ActiveBrowser.CanGoForward)
        _windowManager.ActiveBrowser.GoForward();
    }

    private void stopToolStripButton_Click(object sender, EventArgs e)
    {
      if (_windowManager.ActiveBrowser != null)
      {
        _windowManager.ActiveBrowser.Stop();
      }
      stopToolStripButton.Enabled = false;
    }

    private void refreshToolStripButton_Click(object sender, EventArgs e)
    {
      if (_windowManager.ActiveBrowser != null)
      {
        _windowManager.ActiveBrowser.Refresh(WebBrowserRefreshOption.Normal);
      }
    }

    private void homeToolStripButton_Click(object sender, EventArgs e)
    {
      if (_windowManager.ActiveBrowser != null)
        _windowManager.ActiveBrowser.GoHome();
    }

    private void searchToolStripButton_Click(object sender, EventArgs e)
    {
      if (_windowManager.ActiveBrowser != null)
        _windowManager.ActiveBrowser.GoSearch();
    }

    #endregion

   

    public WindowManager WindowManager
    {
      get { return _windowManager; }
    }

  }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Jeroen Landheer
Web Developer
Chile Chile
I am
- born in The Netherlands
- living in Chile together with my wife.
- a Microsoft Certified Professional Developer on all 3 areas (Windows, Web and Enterprise)
- an MCITP on Microsoft SQL Server 2005 (Database Administrator)
- an active programmer for about 14 years.
- a business owner, of a Dutch company called "The Wheel Automatisering" (http://www.thewheel.nl)
- a coder in C#, VB.Net and Managed C++.
- someone who likes to share knowledge
 
For fun I like to go out with my dogs, enjoy the sun or write some articles that I share with the community.

| Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 29 Mar 2006
Article Copyright 2006 by Jeroen Landheer
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid