Windows Application interaction with a Web Page

Share

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.

Finally, there’s another very important peculiarity of what does Cialis that brings it so high above its alternatives. It is the only med that is available in two versions – one intended for use on as-needed basis and one intended for daily use. As you might know, Viagra and Levitra only come in the latter of these two forms and should be consumed shortly before expected sexual activity to ensure best effect. Daily Cialis, in its turn, contains low doses of Tadalafil, which allows to build its concentration up in your system gradually over time and maintain it on acceptable levels, which, consequently, makes it possible for you to enjoy sex at any moment without having to time it.

3 thoughts on “Windows Application interaction with a Web Page”
  • this post is very usefull thx!

    July 1, 2010 at 7:09 pm
  • Great, I never knew this, thanks.

    July 2, 2010 at 2:16 am
  • themade says:

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

    August 1, 2010 at 5:14 pm

Comments are closed.

By continuing to use the site, you agree to the use of cookies. More information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close