Handle Windows Forms Events

Process events fired by Windows Forms controls hosted in Web pages.

Hot Tip

LANGUAGES: C#

TECHNOLOGIES: DateValidator class

 

Handle Windows Forms Events

Process events fired by Windows Forms controls hosted in Web pages.

 

By Jeff Prosise

 

It's no secret that one way to overcome the limitations of HTML and give browser-based clients rich user interfaces is to host Windows Forms controls in Web pages. As an example, here's a derived class named WebSlider that encapsulates a Windows Forms trackbar control:

 

namespace Wintellect

{

    public class WebSlider :

     System.Windows.Forms.TrackBar {}

}

 

If this source-code file is compiled into a DLL named WebSlider.dll and deployed on a Web server, this tag declares an instance of it, causing a vertical trackbar control to appear on the page:

 

<object id="Slider"

 classid="http:WebSlider.dll#Wintellect.WebSlider"

 width="64" height="256">

  <param name="Minimum" value="0">

  <param name="Maximum" value="10">

  <param name="Value" value="0">

  <param name="Orientation" value="Vertical">

  <param name="BackColor" value="gainsboro">

</object>

 

The first time the page is accessed, Internet Explorer downloads the DLL and, with the .NET Framework's help, caches it in the .NET download cache. The two chief requirements are that the client must run Internet Explorer 5.01 or higher and must have the .NET Framework installed.

Once a Windows Forms control is declared in this manner, you can access its methods and properties by name from client-side script. This JavaScript moves the trackbar's thumb to position 5 by setting its Value property accordingly:

 

MyForm.Slider.Value = 5;

 

So far, so good. But this is where developers using Windows Forms controls in Web pages often encounter an obstacle. It's easy enough to call a control's methods and read and write its properties from client-side script. But what about processing the control's events? It turns out that events can be processed in client-side script, too. But first, you, the control developer, must do a little work to let it happen.

 

Here's a modified version of WebSlider whose Scroll events (which are inherited from and fired by the TrackBar base class) can be processed on the client side:

 

using System;

using System.Runtime.InteropServices;

namespace Wintellect

{

     [ComSourceInterfaces (typeof (IWebSliderEvents))]

    public class WebSlider :

     System.Windows.Forms.TrackBar {}

     [InterfaceType (ComInterfaceType.InterfaceIsIDispatch)]

    public interface IWebSliderEvents

    {

         [DispId (1)]

         void Scroll (Object sender, EventArgs e);

    }

}

 

The modified control DLL defines an interface named IWebSliderEvents, which in turn declares a method named Scroll. The [InterfaceType] attribute tells the Framework to expose the interface to unmanaged COM clients as an IDispatch interface and assigns Scroll a dispatch ID of 1. To expose other TrackBar events, simply declare them in the interface as well and assign them unique dispatch IDs.

 

The second half of the equation is the [ComSourceInterfaces] attribute that adorns the class definition. This tells the Framework that WebSlider's Scroll events should be exposed to COM clients as COM events using an outgoing IWebSliderEvents interface.

 

Here's a Web page you can use to test the modified WebSlider control and prove you can respond to its events in client-side script:

 

<html>

  <body>

    <form id="MyForm">

      <object id="Slider"

       classid="http:WebSlider2.dll#Wintellect.WebSlider"

       width="64" height="256">

        <param name="Minimum" value="0">

        <param name="Maximum" value="10">

        <param name="Value" value="0">

        <param name="Orientation" value="Vertical">

        <param name="BackColor" value="gainsboro">

      </object>

    </form>

    <span id="Output" runat="server">0

  </body>

</html>

<script for="Slider"

 event="Scroll (source, args)"

 language="javascript">

  Output.innerText = MyForm.Slider.value;

</script>

 

As you move the slider's thumb, the client-side event handler updates the positional value beneath the slider continually to reflect the thumb's latest position. Note that in order for this to work, the assembly containing the control must be granted full trust on the client computer so it has permission to call into unmanaged code. (The event handler is, after all, unmanaged code, and for security reasons managed code can only call unmanaged code if it is granted permission to do so.) You can use the Microsoft .NET Framework Wizards found in Control Panel/Administrative Tools to grant the assembly full trust.

 

Jeff Prosise is author of several books, including Programming Microsoft .NET (Microsoft Press). He also is a co-founder of Wintellect (http://www.wintellect.com), a software consulting and education firm that specializes in .NET. Got a question for this column? Submit queries to [email protected].

 

 

 

 

Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish