Using Windows Forms Controls in Internet Explorer

This topic describes how to successfully execute Windows Forms controls within Internet Explorer (IE). Windows Forms controls within IE are activated without a user prompt, require no registration, and utilize the common language runtime (CLR) code access security.

There are five steps in getting a Windows Forms control activated within Internet Explorer, and each is listed here and detailed below.

  • Create the Windows Forms control.
  • Create an HTML document with an object tag.
  • Create the virtual directory and set permissions.
  • Run the control.
Create the Windows Forms Control

Almost any Windows Forms control can be hosted in Internet Explorer, but for this example we will host the SimpleControl that is included in the Creating Controls section of this QuickStart tutorial. The control must be installed to the global assembly cache or be present in the same virtual directory as the web page that contains it.

 
VB SimpleControl

[Run Sample] | [View Source]
Create an HTML document with an object Tag

The next step is to create an HTML document with an object tag that refers to the Windows Forms control. For this sample, some simple script and input tags will also be added to demonstrate programmatic access to the control.

<object id="simpleControl1"

classid="http:SimpleControl.dll#Microsoft.Samples.WinForms.Cs.SimpleControl.SimpleControl"
height="300" width="300" VIEWASTEXT>
	<param name="Text" value="Simple Control">
</object>

The classid has two interesting parts: the path to the control library, and the fully qualified name of the control, separated by the pound sign. If you are familiar with an ActiveX object tag, you will notice the lack of a guid. In the case of Windows Forms, the combination of the path and fully qualified class name serve as the unique identifier.

Param tags may be used to set properties on controls. In this case, the name attribute is the name of the property and the value attribute is the value of the property.

<script>

function ChangeText() {
	simpleControl1.Text = text1.value;
}

</script>
�

<input type="text" id="text1">
<input type="button" value="Change Text" onclick="ChangeText()">

To gain programmatic access to your control, you can write script against it. A button and text box on the page is used in conjunction with the simple JScript function ChangeText to set the text property of the control. The following is the complete HTML and script code for this example.

<html>

<script language="JScript">

function ChangeText() {
	simpleControl1.Text = text1.value;
}

</script>

<body>

<p>Simple Control
<br>
<br>
</body>

<object id="simpleControl1"

classid="http:SimpleControl.dll#Microsoft.Samples.WinForms.Cs.SimpleControl.SimpleControl"
height="300" width="300" VIEWASTEXT>
	<param name="Text" value="Simple Control">
</object>

<br>
<br>

<input type="text" id="text1">
<input type="button" value="Change Text" onclick="ChangeText()">

</html>
Create the Virtual Directory and Set Permissions

The HTML page must reside in an IIS virtual directory on your web server, and have appropriate permissions. In this example, the Windows Forms control resides in the same directory, but it can also be installed in the global assembly cache. Execution permissions on the virtual directory must be set to scripts -- the control will not be properly activated if the execution permissions are set to scripts & executables. For this sample, these steps have been performed for you.

Run the Control

To run the control, just point Internet Explorer to the HTML page in your virtual directory. If the control is not activating correctly, it may be necessary to restart Internet Explorer. To view and run this sample, click the icon below.

 
VB IE Sourcing

[Run Sample] | [View Source]


Copyright 2001 Microsoft Corporation. All rights reserved.