Mink Machine

Invoking Windows Forms controls from the web browser

How do Java applets fit into the world of .NET? The short answer is, "they don’t", but it is possible to achieve a similar design using Windows Forms controls. This short guide assumes that you have some previous experience in web development on the Windows platform.

Introduction

ASP.NET may or may not be the best thing since sliced bread, but it is a relatively new kid on the block. I first realized its true power in 2001 during a developer conference on a sunny day in downtown Boston. That is, it was sunny until Don Box decided to undress his shirt in front of the audience, but that is another story.

ASP.NET has now been around for several years and every day it attract new arrivals. I was having lunch at a local Greek restaurant with an old friend of mine, who asked me: Is there an adequate equivalent to Java applets in .NET?

That is a very good question. Here is one answer and a way to achieve a similar effect.

Creating a WinForm control

Start Visual Studio.NET, create a new C# project using the Windows Control Library and name it WinFormControl.

Create form window Creating a form window.

This will give us a blank canvas for our artistic urges. Try to stand the corny default names for now. Just create a button and a few text labels.

Blank canvas An empty canvas.

Name one of the text labels lblMessage. In the click event for the button, write something like the following code:

private void button1_Click(object sender, System.EventArgs e) { lblMessage.Text = "This is the response message."; }

Compile this mighty control and copy the DLL file into a new directory called something nice.

HTML

Next step is to create the HTML page where we embed the control. Create a text file with the following content:

<html> <head> <title>WinForms Control example</title> </head> <body> <h1>WinForms Control example</h1> </body> <object id="UserControl1" classid=" http:UserControl1.dll#WinFormControl.UserControl1" height="400" width="400" VIEWASTEXT> </object> </html>

UserControl1.dll is the name of the assembly created in the previous step, WinFormControl is the namespace and UserControl1 the control itself. Name the file ControlHost.html and place it where you placed the DLL file.

Web server configuration

Time to bring this magnificent creation into the world. Start the IIS management snap-in and create a new virtual directory: Right-click "Default Web Site", select New -> Virtual Directory, specify alias WinFormsControl and locate the directory where you placed the DLL and HTML files. The default access permissions, "Read" and "Run scripts" will do fine.

Properties in IIS Properties in IIS.

View result

Start your Internet Explorer browser (sorry, this won’t work in Mozilla and other nicer browsers) and navigate to
http://localhost/winformscontrol/controlhost.html

The control will load quickly into the HTML page. Click the button and verify that the message is displayed.

View result Viewing the result.

As you can see, this may be done quite quickly. Happy coding!

Comments

No comments yet.

Post a comment

Your email address will not be published. Required fields are marked *

Featured stories

Getting lost in Yazd

"Navigating on random while surrounded by staring old men, pointing their crooked fingers at the Godzilla Viking in surprise. It feels like I’m walking around in Mos Eisley."

Road trip across the American Southwest

"We drove along Route 6, Route 66 and Route 666. If there was a Route 6666, we must have missed that turn."

A journey through Iran

"I woke up freezing on a Persian rug with aching back. Behind a corner I saw the damned rooster that kept me awake during many hours."

Vineyards of southern France

"Not far from Sauternes is the village of Saint-Émilion, also a famous wine region but what got me hooked was their impressive limestone caves."

Good morning Montenegro

"One would almost think that all this secrecy was made on purpose to keep the hordes of tourists away. Let them all perish in the crowds of Dubrovnik while the people of Montenegro can enjoy their beautiful country on their own. Or something like that."

Memories of Skye

"The countryside is charming and some places even have coffee."