Wednesday, December 20, 2006

My first .NET Applet

In one of the current projects I'm working on, performance has become an issue. To gain more performance, we decided to implement the solution as a .NET applet. At first it looked a bit strange. I always associated applets with Java. But apparently this is not the case.

A .NET applet is nothing more than a windows form control hosted in Internet Explorer. As a sample, I've created a small application that shows the current time in a .NET applet.

Here is how to do it:
First you need to create a new Windows Control Library project. Add a button and a textbox to the usercontrol.



Add code to the Click event of the button as shown below:

using System;



using System.ComponentModel;



using System.Drawing;



using System.Windows.Forms;







namespace MyApplet



{



public class TimeControl : UserControl



{



public TimeControl()



{



InitializeComponent();



}







private void btnShowTime_Click(object sender, EventArgs e)



{



txtTime.Text
= DateTime.Now.ToString();



}







public void ShowTime(object sender, EventArgs e)



{



txtTime.Text
= DateTime.Now.ToString();



}



}



}
Now that you have your control, you need to create an application that hosts it. In this example we will use Internet Explorer as host. 
So add a new website to your solution. Within the default.aspx page, you will create a reference to your control via an object tag. To be able to do this, you need to copy the DLL to your website. Don't copy it in the bin folder because that folder isn't browsable. The object tag will look like this:
<object id="applet1" classid="http:MyApplet.dll#MyApplet.TimeControl"></object>
The classid needs to be in the format http:NameOfDll.dll#Namespace.ClassName.
NOTE: It is best to use a separate tag to close the object, otherwise the tags after the object tag can give problems.
If you now run the example it should look like the screen below:
 
Note:
  • .NET applets are restricted to Internet Explorer.
  • In some companies ActiveX components are not allowed.

1 comments:

Alberto said...

and wath about the interaction with javascript!?!? building the control using .NET 2.0???