Ropardo Sowftware development company

Experience software development with ROPARDO S.R.L.

RSS Feed
RSS Feed
  • Home
  • About ROPARDO S.R.L
  • Our websites

Windows Application interaction with a Web Page

Displaying a web page inside a widows form application and interact with the content it’s very simple using System.Windows.Forms.WebBrowser.
This way you can easily create a panel containing ads in to your application or even communicate with a 3rd Party web component for displaying data. In our case we needed to integrate an enterprise application with an applet provided by Oracle that was capable of displaying and comparing 3d models.

First step in order to archive this goal is adding a WebBrowser control in one of your forms, or better inside a UserControl to allow a better code separation and reuse.
In order to load a web page in the WebBrowser control you must set the “Url” property of this control. If you run your application you will see that the page is loaded inside the panel of the control.
Interaction with the WebPage is also very simple. If you need to call methods inside your application from the web page you just loaded you just need to set the “ObjectForScripting” property of the WebBrowser control to the object that must handle the requests from the page. By example if want to send text messages from javascript and make use of them inside your web application the code would look like bellow.
C#

1
2
3
4
5
6
webBrowserContainer.ObjectForScripting = this;
…
public void sendMessage(string text)
{
    MessageBox.Show(text);
}

JavaScript

1
2
3
4
function buttonClick()
{
window.external.sendMessage('Button was clicked!!!');
}

Setting the “ObjectForScripting” will set the container of the WebBrowser control as the target for JavaScript calls. Calling of the method is very simple from JavaScript with “window.external”.
Calling JavaScript function is also very simple like presented in the code sample bellow.
C#

1
2
3
4
public void jsCall()
{
  var result = webBrowserContainer.Document.InvokeScript("showText", new object[1] { "Windows Application Call" });
}

JavaScript

1
2
3
4
5
6
7
8
 
function showText(text)
{
	alert(text);
	var results = new Array();
	results[0] = “Value1”;
	results[2] = “Value2”;
}

As you can see simple calls can be made very easily and parameters can be sent through an object array. If the JavaScript function returns a more complex result like an Array or even a custom type constructed by the developer, this can be processed in C# with the help of reflection. The “jsCall” code will change like bellow.
C#

1
2
3
4
5
6
7
8
9
10
public void jsCall()
{
var result = webBrowserContainer.Document.InvokeScript("showText", new object[1] { "Windows Application Call" });
if (result != null)
  {
	Type jsType = result.GetType();
string content = (string) jsType.InvokeMember( “toString”, System.Reflection.BindingFlags.Default | System.Reflection.BindingFlags.InvokeMethod, null, result, null);
	string[] values = content.Split(“,”);
  }
}

Other properties like “length”, in this case, can also be interrogated from the result object.
As you can see the WebBrowser control can be a very useful and easy to use tool in building windows application that need to show web content or even interact with more complex functionalities.

  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
Get Shareaholic
Tags: .NET C# javascript System.Windows.Forms.WebBrowser

 Posted in: Microsoft.NET
May 19, 2010 | Florin Stoian | 3 Comments

3 Responses

  • dental hygienist
    July 1, 2010
    1

    this post is very usefull thx!

  • minority scholarship
    July 2, 2010
    2

    Great, I never knew this, thanks.

  • themade
    August 1, 2010
    3

    I would like to exchange links with your site blog.ropardo.ro
    Is this possible?

Leave a Reply

 


  • « Previous post
  • Next post »
  • Recent Posts

    • Installing PyGraphviz on Windows
    • Convert python object to XML representation
    • Liferay Portlet Development
    • Norway Road Show 2011 private meeting invitation
    • Oracle OpenWorld 2011
  • Ropardo is Hiring

  • Subscribe

    • Add to Google Reader or Homepage Add to netvibes TopOfBlogs
  • Recent Comments

    • Rajkumar Pomaji on Bluetooth PC Remote Control
    • Stelian Morariu on GWT 2.1 – Uploading a file using the RPC mechanism
    • Sergio on GWT 2.1 – Uploading a file using the RPC mechanism
    • Artem on Liferay: Deployment will start in a few seconds… and how to realy start
    • rkd80 on GWT 2.1 – Uploading a file using the RPC mechanism
  • Archives

    • November 2011 (1)
    • September 2011 (4)
    • July 2011 (3)
    • June 2011 (2)
    • May 2011 (4)
    • April 2011 (4)
    • March 2011 (3)
    • February 2011 (2)
    • January 2011 (2)
    • December 2010 (1)
    • November 2010 (4)
    • October 2010 (4)
    • August 2010 (3)
    • July 2010 (3)
    • June 2010 (6)
    • May 2010 (8)
    • April 2010 (7)
    • March 2010 (9)
    • February 2010 (6)
    • January 2010 (5)
    • December 2009 (7)
    • November 2009 (9)
    • October 2009 (10)
    • September 2009 (14)
    • August 2009 (10)
    • July 2009 (1)
    • June 2009 (1)
    • May 2009 (1)
    • April 2009 (1)
    • March 2009 (1)
    • October 2008 (3)
    • October 2007 (3)
    • July 2007 (4)
    • June 2007 (1)
    • May 2007 (3)
  • Meta

    • Log in
    • Entries RSS
    • Comments RSS
    • WordPress.org
  • Categories

    • News (15)
    • Ropardo Team (8)
    • Ropardo Products (6)
      • File Tracking Client (4)
      • iManagement (2)
    • Software Development (83)
      • Microsoft.NET (22)
      • Java (40)
      • Oracle (8)
      • Power Builder (3)
      • Liferay (5)
      • Lotus Notes (9)
      • xWiki (4)
    • System Adminstration (13)
      • Linux (10)
      • Windows (3)
    • Programming (1)
    • Uncategorized (3)
    • Databases (10)
      • MSSQL (5)
      • PostgreeSQL (3)
    • Microsoft.NET (1)
    • Web Development (28)
      • ASP/ASPX (3)
      • Content Management Systems (1)
      • HTML/CSS (5)
      • Javascrip/AJAX (8)
      • PHP (7)
    • Oracle E Business Suite (6)
  • Tags

    .NET ajax blog C# certification client CMS control css database Debugging django Domino Eclipse extension file tracking filter fun gentoo google Hibernate how to html image iManagement import Java javascript jQuery liferay Linux Lotus Notes lotus script Oracle Oracle BI Publisher 11g PHP portal PostgreSQL powerbuilder Python SQL Telerik velocity xml Xwiki

© 2010 ROPARDO s.r.l..

Powered by WordPress. Styled by Ropardo