Click here to Skip to main content
13,255,340 members (48,807 online)
Click here to Skip to main content
Add your own
alternative version


32 bookmarked
Posted 20 Jun 2006

WebBrowser Control Based UI for Windows Applications

, 20 Jun 2006
Rate this:
Please Sign up or sign in to vote.
Using a WebBrowser control with interactivity to provide a rich interface in Windows Forms applications
Sample Image - BrowserBasedUI.jpg


Using a form with many controls on it tends to make the application quite heavy on the system. During my working experience, I have found that a lot of text boxes or a grid view with many rows slows down the application considerably and affects the responsiveness of the application.

I decided to experiment by using the WebBroswer control available in Visual Studio 2005 to provide a rich GUI. In the demo application, I display an XML file, using an XSL Transform. This XML file could be received by a client from a server, or generated. In order to update the GUI, the developer would just have to call a Refresh method.

Setting Up the WebBrowser Control

Put a WebBrowser control on your form named browser. We need to make this browser transfer control to event handlers when the user clicks something on the page displayed. We need to set a few properties in order to accomplish this. First of all, in the form's constructor we need to set the scripting object of browser as the form.

browser.ObjectForScripting = this;

The form's visibility to COM has to be set to true. This can be done adding the ComVisible attribute to the class. Add [ComVisible(true)] just before the class declaration:

    public partial class frmMain : Form

Creating the XSLT

The XML and XSLT files used in the demo are taken from W3Schools XSLT Tutorial.

I have slightly changed the XML file to include id's for each cd element. The modified file looks like this:

<?xml version="1.0" encoding="ISO-8859-1"?>
        <id />1</id />
        <title>Empire Burlesque</title>
        <artist>Bob Dylan</artist>
        <title>Hide your heart</title>

In the XSLT file, I have replaced...

<td><xsl:value-of select="title"/></td>

... with:

  <xsl:for-each select="title">
      <xsl:attribute name="onclick">
        <xsl:value-of select="concat('window.external.ShowDetails(', ../id,')')"/>

The line of interest here is:

<xsl:value-of select="concat('window.external.ShowDetails(', ../id,')')"/>

This line hooks up the onclick event on the form to the window.external.ShowDetails(int id) function in the form. The XPath ../id passes the id of the clicked element to the ShowDetails function. After getting the id, you can process it as you want. In the demo, the element is displayed in a MessageBox.

Displaying the XML

I have tried two ways to display the Web page in the browser. Both of them involve using the EXtensible Stylesheet Language Transform(XSLT) to display the XML file in the browser.

In the first method, an XSL stylesheet reference is added to the XML document by adding the line...

<?xml-stylesheet type="text/xsl" href="cdcatalog.xsl"?>

... at the top of the XML document below the namespace declaration.

<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="cdcatalog.xsl"?>
        <title>Empire Burlesque</title>

Now to display the XML file in the browser, simply pass its URL to the Navigate method of the browser object. If the XML file and XSL file are in the same directory as the executable, the following code should do the trick:

browser.Navigate(Application.StartupPath + "\\cdcatalog.xml");

The second method involves a little more work, but is far more extendable than the first one. We create an HTML file, in which we load the XML file and transform it with the XSL file by using JavaScript. The code to do it is as follows:


<script type="text/javascript">

// Load XML 
var xml = new ActiveXObject("Microsoft.XMLDOM")
xml.async = false

// Load XSL
var xsl = new ActiveXObject("Microsoft.XMLDOM")
xsl.async = false

// Transform



By using this approach, the XML data in the file can be just a panel in the entire page. Multiple XML files and static content can be displayed on the page with great ease.

The sample demo uses this method to show the XML data.


The above implementation can be used to provide a very light UI in Windows applications. This is particularly of great advantage when the interaction is quite low, i.e. not many functions or events of control are used. Instead of creating or using UserControls to make applications look sleek, a WebBrowser can be used along with a well designed HTML page and XSL stylesheet to achieve the same.


  • 20th June, 2006: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

India India
I am currently working with D. E. Shaw and Co. in Hyderabad. I completed my B.Tech. at Indian Institute of Technology, Kharagpur majoring in Manufacturing Science and Engineering.

I am a really hardcore gamer and play all sorts of computer games. I love to watch and play football.

You may also be interested in...


Comments and Discussions

shanmugam_1323-Sep-09 22:25
membershanmugam_1323-Sep-09 22:25 
vikashparida24-Sep-09 8:38
membervikashparida24-Sep-09 8:38 
GeneralJust what i needed Pin
thylux11-Sep-09 6:38
memberthylux11-Sep-09 6:38 
GeneralDeploy solution Pin
nivler27-Jun-06 11:08
membernivler27-Jun-06 11:08 
GeneralRe: Deploy solution Pin
vikashparida27-Jun-06 12:06
membervikashparida27-Jun-06 12:06 
QuestionHeavy and Light UI's ? Pin
BillWoodruff25-Jun-06 22:02
memberBillWoodruff25-Jun-06 22:02 
GeneralQuestion [modified] Pin
menenedezpoo21-Jun-06 7:13
membermenenedezpoo21-Jun-06 7:13 
GeneralRe: Question Pin
vikashparida21-Jun-06 12:23
membervikashparida21-Jun-06 12:23 
GeneralGreat Tip Pin
menenedezpoo21-Jun-06 4:18
membermenenedezpoo21-Jun-06 4:18 
QuestionLink ??? Pin
fwsouthern20-Jun-06 17:42
memberfwsouthern20-Jun-06 17:42 
AnswerRe: Link ??? Pin
vikashparida21-Jun-06 5:56
membervikashparida21-Jun-06 5:56 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.171114.1 | Last Updated 20 Jun 2006
Article Copyright 2006 by vikashparida
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid