Click here to Skip to main content
Click here to Skip to main content

Working with the Web Browser Control in Visual Studio 2005 - IE7Clone.

, 14 Jan 2007
Rate this:
Please Sign up or sign in to vote.
Example application working with the VS2005 Web browser control

Ie7 Clone

Introduction

As many people know Microsoft has included a browser control with Visual Studio 2005.

The browser control is a managed wrapper around the internet explorer control found in earlier versions of visual studio (SHDocVW.DLL) and it exposes some functionality that was not always easy to get at, while at the same time leaving out some functionality that would have made the control truly great to work with.

The goal of this article and accompanying sample application is to show working with the new WebBrowser Control in Visual Studio 2005 by cloneing many of the features in Internet Explorer 7. It is not meant to replace your existing browser or be a 100% complete implementation of a browser, The features implemented are to show examples of working with various properties and events of the control and some idea's on what to do to write your own complete browser application. Every feature is not fully error checked as this is meant to be a "starting point" for working with the control and every user interaction possibility has not been addressed. That said with minimal effort you could make this example a complete browser based application. You will find some tips in the comments for certain events and routines on how to improve or use that routine in your own applications as well as some "stubs" such as download manager, phishing filter, rss reader, Document Explorer, Image Grabber etc.

This example was put together with out referencing the Active X (SHDocVW.DLL) directly. That said, It again needs to be pointed out that the browser control is a managed wrapper around the internet explorer control found in earlier versions of visual studio (SHDocVW.DLL) and you may find a mix of the 2 will best suit your purposes.

Getting Started...

To make our example application one of the 1st things we are going to need is closable tabs, not wanting to re-invent the wheel a suitable tab control is found. The tabs used in this example were written by Eduardo Oliveira and can be found here. (Updated Tab Control Code provided by the author of the control. Thanks Eduardo).

Extending the browser control...

The browser control falls short on some basic functionality we will want to use so we will need to extend the control, an example of this is the NewWindow event..

New Window event standard with the control.

Now take a look at what we have with the extended browser.

Please look at the extended browser control for how this was done, several MSDN articles were used in putting it together and you could certainly add a lot more.

Context Menus

One of the nicest features of the new control is the ability to override the built in context menu and create your own,not easily done in the past. In this example we will use our own context menu, which will show you a bit about working with the loaded document directly.

To disable the built in menu set the web browser property IsWebBrowserContextMenuEnabled to false, then create your own context menu and set the browsers context menu to your new custom menu.

This is a pretty powerful tool especially when used with the controls document property as demonstrated in the example application.

* In the example application I do not check parent and children of the selected element, this would not be hard to implement if you choose to and would allow more accurate or robust options in the context menu.

JavaScript Document.External...

One of the most powerful features you can use, especially when creating intranet browser based applications is the javascript window.external call in conjuction with your browser based application. You can build extremely powerful browser based applications using this functionality.

Lets take a look at the search provider code:

Our document might contain a link or onclick such as: window.external.AddSearchProvider

We can react to this call to window.external in whatever way we see fit, in this case we launch a form.

Public Sub AddSearchProvider(ByVal value As String)
   ' See: http://www.opensearch.org/ for more information.
   Dim ofrm As New frmAddSearchProvider
   ofrm.strXML = value
   ofrm.Show()
End Sub

As you can see we are reacting to a call from the document.

window.external.AddSearchProvider / Public Sub AddSearchProvider(ByVal value As String)

*Note - Make sure your routine signatures match as shown above in red.

Using this feature you can extend and add whatever type of feature you would want to provide...

Example:

<a href="#" onClick="window.external.ShowCalc">ShowCalculator</a>

Public Sub ShowCalc()
    Process.Start("Calc.exe")
End sub

This is also demostrated on the blocked site page to launch the settings form.

As you can see using this feature you can do just about anything you want to with the full power of a windows application.

Phishing Filters, Blocked Sites, Iterating the document etc...

The browser control exposes the loaded document making it easy to implement features such as a site blockers, phishing filters and simular types of features with a simple iteration through the documents elements allow you to check the document and perform actions based on it's contents.

Dim oEl As HtmlElement

For Each oEl In wb.Document.All
   'Do something based on the element.
Next

It should also be noted that the HtmlElement object and the document itself has a rich set of functions and features to work with, you will see some of these in the example application such as: GetAttribute, GetElementFromPoint etc...

Features in this Example

Most of the learning value is in the example application itself, there are many articles on MSDN and on the web dealing with working with the ShDocVW.DLL and MSHTML. I tried to provide as many starting points and examples of how to implement many features to show working with different the control in many ways, some of the features implemented either fully or partially are:

  • Basic Browser functionality.
  • Document explorer.
  • Most of Internet Explorers Dialogs (properties, find, print dialogs, favorites, select all etc).
  • Grabing images from a page.
  • Blocking sites.
  • Phishing Filter example.
  • Working with VS2005 My.Settings.
  • RSS Viewer + Detecting RSS.
  • History / Cache Viewer.
  • Cookie Viewer.
  • Search Providers.
  • Working with popups, implementing an info bar.
  • Creating a ruler user control.
  • Example of how you might deal with errors
  • Alot more, just poke around in the code.

Background / Notes

The basic idea behind this example is to show a good overview of working with the new Web Browser control by cloning a lot of the features in Internet Explorer 7 as I have found one of the best ways to learn is to "clone" an existing application as well as to point out some idea's on where to go with your own browser based applications and some of the possibilities.

Many articles (including some on MSDN) recommend using on error resume next type error handling in your code due to the nature of connected applications that use browser controls. I have implemented some error handling in the example, but leave the rest up to you to determine how best to handle the many possibilities. All of the features implemented in the example application work for the sites I browse most often.

VS2005 provides application events such as when network connection state has changed, this could be handy for you depending on how your own browser based applications are intended to work.

Using the code

The code is pretty straight forward and is commented where I thought would be needed.

History

This is the 1st release of this example.

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

Share

About the Author

tmaxwell07
Web Developer
United States United States
I have played various roles in IT since around 1993 when I began writing commercial web pages. I then moved to mainly coding in Visual Basic around 1995, making point of sale and kiosk systems, more recently moving to e-commerce and crm applications. Since the release of .net I have been mainly working in .net focusing in vb.net although I have done several projects in c#. I have worked for various companies and have held the position of lead developer at several, recently (2005) leading the e-commerce team at a national retailer to a successful e-commerce implementation. I currently hold comptia a+, network plus, mcp, mcsa, mcse (server system 2003) and mcsd certifications. I enjoy sharing what I have learned as well as embracing new technologies as they are released.

Comments and Discussions

 
QuestionUse webkit Pinmembermisterff12-Apr-13 0:08 
AnswerRe: Use webkit PinmemberMetalPower29-Aug-13 10:27 
GeneralRe: Use webkit Pinmembermisterff116-Nov-13 3:27 
QuestionLicense PinmemberMember 86341149-Feb-12 5:05 
QuestionText only mode Pinmembermarjansik22-Jan-12 15:39 
QuestionDo I Need a Custom Download Manager? PinmemberBig Dog29-Sep-11 22:23 
AnswerRe: Do I Need a Custom Download Manager? PinmemberBaslifico9-Jul-12 5:45 
GeneralMy vote of 5 PinmemberАslam Iqbal1-Jul-11 7:05 
Generalframes Pinmembereug200721-May-11 8:00 
General2 error found... PingroupLozuMaster26-Apr-11 3:48 
GeneralMy vote of 5 PinmemberHeaven20203-Dec-10 1:19 
Questiongreat job but links does not works Pinmemberandreapv2-Sep-10 4:25 
QuestionUsage and License PinmemberWilltwinny8-Apr-10 6:51 
Questiondefault browser Pinmemberhk_maniya18-Mar-10 1:39 
AnswerRe: default browser PinmemberMegaGigsLtd12-Nov-10 12:44 
GeneralAbout default browser Pinmemberhk_maniya18-Mar-10 1:38 
QuestionCan U tell me ? How u set PrintPreviewWindow maximized ? PinmemberMazkhan778-Feb-10 3:11 
GeneralExcellent PinmemberiSurf Elite Company30-Dec-09 18:49 
QuestionPlease help (flash issue)! Pinmemberracrosso1237-Nov-09 1:08 
GeneralTRACING IFRAME BODY Content USING VB.NET 2005 PinmemberMember 13544252-Oct-09 9:05 
GeneralRe: TRACING IFRAME BODY Content USING VB.NET 2005 Pinmemberkpcrash16-Oct-09 13:44 
QuestionFlash Issue In IE7 Clone Pinmemberrhuckle24-Aug-09 1:27 
GeneralUnexpected call to method or property access. PinmemberBigMac200217-Jul-09 9:05 
GeneralJava Applet Pinmembermimranshafiq17-Jun-09 1:53 
QuestionTabs OPening right to left Pinmembermimranshafiq10-May-09 21:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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.141223.1 | Last Updated 14 Jan 2007
Article Copyright 2007 by tmaxwell07
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid