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 take off his tshirt 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

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

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

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

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

Comments

No comments yet.

Leave a reply

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

Featured stories

Greetings from Moldova

Greetings from Moldova

"But Chisinau is an unexpected gem in the wilderness. Even though Moldova is one of the poorest countries in Europe, the capital has its fair share of impressive buildings and post-Soviet architecture."
Good morning Montenegro

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."
The sound of Salzburg

The sound of Salzburg

"It is based on the true story of Maria von Trapp, even though I suspect she didn’t go singing alone in the mountain ranges."
Visions of Singapore

Visions of Singapore

"By extensive use of landfill with sand bought from Indonesia and Cambodia, the bay area has expanded greatly in the last few years and tall buildings pop up everywhere."
Roaming the cobblestones of Istanbul

Roaming the cobblestones of Istanbul

"Inside the church there are still traces of rune inscriptions made during the Viking age by Varangians, an elite guard made up of Scandinavian immigrant warriors."
The fairytale castles of Sintra

The fairytale castles of Sintra

"The Portuguese town of Sintra is famous for many beautiful castles. Palácio Nacional da Pena is arguably the most stunning of the lot. "

Instagram

Seville
Venice
Fever Ray
Rome
Basel
Kuala Lumpur