Click here to Skip to main content
12,549,996 members (47,688 online)
Click here to Skip to main content
Add your own
alternative version


88 bookmarked

Multi Tab WebBrowser

, 23 May 2005
Rate this:
Please Sign up or sign in to vote.
A webbrowser example of how to open new web windows in new tabs.

Sample Image - MultiTabWebBrowser.jpg


One web browser feature that is growing in popularity is the ability to open different tabs, or sub-pages, within the same browser window. Using tabs to separate multiple pages keeps the desktop tidy, while browsing multiple sites.

The web browser is also efficient for viewing multiple local files as it supports many files formats.

This code shows how to implement this feature, and also implements the following features: Print, Print Preview, Page properties, Options screen, Find Dialog, View page source, and more.

Implementing the Multi Tab Feature

This feature is mainly implemented by the NewWindow2 event.

The NewWindow2 event occurs when a new window is to be created for displaying a resource. This event occurs before a new window is created from the WebBrowser control. For example, this event occurs in response to a navigation that is targeted to a new window or to a scripted method.

To specify that your browser program is to be used when a new window is opened, set the ppDisp parameter equal to the Application object. In this scenario, if a user chooses to open a web page in a new window, the new window in your program is used to display the new web page.

Additionally, set the RegisterAsBrowser property to true. This setting causes the new WebBrowser control to participate in window-name resolution. For example, if the window name is used elsewhere in the script, this control is used instead of a newly created one because the control examines all the existing window names before opening a new window.

On this event, we dynamically create a new instance of a tab page and a web browser as child by calling the CreateNewWebBrowser() method, where each web browser object has a tag object containing additional data on the corresponding web browser:

private void axWebBrowser1_NewWindow2(object sender, 
                AxSHDocVw.DWebBrowserEvents2_NewWindow2Event e)
  AxSHDocVw.AxWebBrowser _axWebBrowser = CreateNewWebBrowser();
  e.ppDisp = _axWebBrowser.Application;
  _axWebBrowser.RegisterAsBrowser = true;

private AxSHDocVw.AxWebBrowser CreateNewWebBrowser()
  AxSHDocVw.AxWebBrowser _axWebBrowser = new AxSHDocVw.AxWebBrowser();
  _axWebBrowser.Tag = new HE_WebBrowserTag();
  TabPage _TabPage = new TabPage();
  _axWebBrowser.Dock = DockStyle.Fill;
  _axWebBrowser.BeforeNavigate2 += new 
  _axWebBrowser.DocumentComplete += new 
  _axWebBrowser.NavigateComplete2 += new 
  _axWebBrowser.NavigateError += new 
  _axWebBrowser.NewWindow2 += new 
  _axWebBrowser.ProgressChange += new 
  _axWebBrowser.StatusTextChange += new 
  _axWebBrowser.TitleChange += new 
  _axWebBrowser.CommandStateChange += new 

  tabControl1.SelectedTab = _TabPage;
  return _axWebBrowser;

Each instance of a web browser will have in its tag an HE_WebBrowserTag object that holds some details on that web browser object:

public class HE_WebBrowserTag
  public int _TabIndex = 0;
  public bool _CanBack = false;
  public bool _CanForward = false;

Invoke the Find, View Source, and Options Dialog Boxes

Warning: this sample uses an undocumented command-group GUID that is subject to change in the future. Although this sample was tested to work correctly with Internet Explorer 6 and earlier, there is no guarantee that these techniques will continue to work successfully in future versions.

Define IOleCommandTarget in Visual C# .NET

To define a .NET interface to obtain a reference to a Component Object Model (COM) interface, follow these steps:

  1. Assign the interface the GUID of the appropriate COM interface.
  2. Include type declarations for all the methods of the interface.
  3. Include references to the Mshtml.dll file and the Shdocvw.dll file.

    To do this in your Visual C# .NET project, follow these steps:

    1. Click Add Reference on the Project menu.
    2. Click the COM tab.
    3. Double-click Microsoft HTML Object Library and Microsoft Internet Controls.
  4. Include the following interface declaration just before your application's namespace declaration to add a reference to the Microsoft HTML (MSHTML) IOleCommandTarget interface:
    using System;
    using System.Runtime.InteropServices;
    public struct OLECMDTEXT
        public uint cmdtextf;
        public uint cwActual;
        public uint cwBuf;
        [MarshalAs(UnmanagedType.ByValTStr,SizeConst=100)]public char rgwz;
    public struct OLECMD
        public uint cmdID;
        public uint cmdf;
    // Interop definition for IOleCommandTarget.
    public interface IOleCommandTarget
        //IMPORTANT: The order of the methods is critical here. You
        //perform early binding in most cases, so the order of the methods
        //here MUST match the order of their vtable layout (which is determined
        //by their layout in IDL). The interop calls key off the vtable ordering,
        //not the symbolic names. Therefore, if you 
        //switched these method declarations
        //and tried to call the Exec method 
        //on an IOleCommandTarget interface from your
        //application, it would translate 
        //into a call to the QueryStatus method instead.
        void QueryStatus(ref Guid pguidCmdGroup, UInt32 cCmds,
            [MarshalAs(UnmanagedType.LPArray, SizeParamIndex=1)] 
            OLECMD[] prgCmds, ref OLECMDTEXT CmdText);
        void Exec(ref Guid pguidCmdGroup, uint nCmdId, uint nCmdExecOpt, 
                  ref object pvaIn, ref object pvaOut);

Define the Command GUID for CGID_IWebBrowser

You must also define the GUID for CGI_IWebBrowser to inform MSHTML how to process your command IDs. Use the Microsoft .NET Framework class GUID to do this:

private Guid cmdGuid = new Guid("ED016940-BD5B-11CF-BA4E-00C04FD70816");

private enum MiscCommandTarget { Find = 1, ViewSource, Options }

Call Exec()

Finally, encapsulate the calls to the Exec method in three separate method calls. Each call assumes the existence of a hosted instance of the WebBrowser control that is named webBrowser:

private mshtml.HTMLDocument GetDocument()
        mshtml.HTMLDocument htm = (mshtml.HTMLDocument)axWebBrowser2.Document;
        return htm;
        throw (new Exception("Cannot retrieve" + 
               " the document from the WebBrowser control"));

public void ViewSource()
    IOleCommandTarget cmdt;
    Object o = new object();
        cmdt = (IOleCommandTarget)GetDocument();
        cmdt.Exec(ref cmdGuid, (uint)MiscCommandTarget.ViewSource,
    catch(Exception e)

public void Find()
    IOleCommandTarget cmdt;
    Object o = new object();
        cmdt = (IOleCommandTarget)GetDocument();
        cmdt.Exec(ref cmdGuid, (uint)MiscCommandTarget.Find,
    catch(Exception e)

public void InternetOptions()
    IOleCommandTarget cmdt;
    Object o = new object();
        cmdt = (IOleCommandTarget)GetDocument();
        cmdt.Exec(ref cmdGuid, (uint)MiscCommandTarget.Options,
        // NOTE: Because of the way that this CMDID is handled in Internet Explorer,
        // this catch block will always fire, even though the dialog box
        // and its operations completed successfully. You can suppress this
        // error without causing any damage to your host.


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

Ronit H
Israel Israel
No Biography provided

You may also be interested in...


Comments and Discussions

BugGUI freezing on navigating Pin
Emiliarge23-Aug-14 23:01
memberEmiliarge23-Aug-14 23:01 
QuestionDocument.All.Item and InvokeMember help Pin
zouhair samir13-Sep-13 15:57
memberzouhair samir13-Sep-13 15:57 
GeneralMy vote of 5 Pin
LVMONTIEL5-May-11 1:05
memberLVMONTIEL5-May-11 1:05 
GeneralDrag Out tabs Pin
Fullmetal9901224-Apr-11 3:54
memberFullmetal9901224-Apr-11 3:54 
QuestionGreat Job, Help Please Pin
young10120-Nov-09 19:59
memberyoung10120-Nov-09 19:59 
GeneralHelp with tabs!!! Pin
ALEX CHIHAI7-Oct-09 2:33
memberALEX CHIHAI7-Oct-09 2:33 
GeneralYou Rock! Pin
Shannon McCoy19-Aug-09 3:04
memberShannon McCoy19-Aug-09 3:04 
GeneralVB.Net implementation Pin
bn2vs23-Feb-09 9:12
memberbn2vs23-Feb-09 9:12 
GeneralHere is your 5! Pin
final_zero30-Nov-08 0:16
memberfinal_zero30-Nov-08 0:16 
QuestionHide the Find Dialog when swichting to differnet tab? Pin
TexG29-Feb-08 3:57
memberTexG29-Feb-08 3:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.161021.1 | Last Updated 23 May 2005
Article Copyright 2005 by Ronit H
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid