JAL Computing

C++COMProgramming .NET Mac Palm CPP/CLI Hobbies

 

Home
Up

In this sample project, TestEvent1.js, we trap the button click event and pop up a Message Box. To do this we register the event handlers buttonx_Clicked(o : Object, e : EventArgs) with the button object using the method buttonx.add_Click(buttonx_Clicked). In this object oriented model, each event is bound to a single event handler method. It is also possible to register all button events with a single button event handler and switch on o.Name().

Source Code

Note the event handling code in red.

import System;
import System.Windows.Forms;
import System.ComponentModel;
import System.Drawing;

package JAL {
    // demonstrates simple event handling
    class TestEvents extends System.Windows.Forms.Form {
        // declare variables
        private var button1, button2, button3: Button;
        private var panelBase, panelBottom : Panel;

        // constructor
        function TestEvents() {
        // move the form init code to a private method for clarity
            InitializeComponent();

            // register event handlers with buttons
            button1.add_Click(button1_Clicked);
            button2.add_Click(button2_Clicked);
            button3.add_Click(button3_Clicked);

        } // end_constructor

        // event handlers
        private function button1_Clicked(o : Object, e : EventArgs) {
            MessageBox.Show(o.Name());
        }

        private function button2_Clicked(o : Object, e : EventArgs) {
            MessageBox.Show(o.Name());
        }

        private function button3_Clicked(o : Object, e : EventArgs) {
            MessageBox.Show(o.Name());
        }


        // move form init code to a private method
        private function InitializeComponent() {
            // label and size and position form
            Text= "Click Me!";
            ClientSize= new System.Drawing.Size(500,300);
            StartPosition= System.Windows.Forms.FormStartPosition.CenterScreen;

            // create base panel
            panelBase= new Panel;
            panelBase.Location= new Point(0,0);
            panelBase.Size= new System.Drawing.Size(500,300);
            panelBase.Name= "panelBase";
            // resize in all directions
            panelBase.Anchor= AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Bottom;
   
            // create a bottom panel
            panelBottom= new Panel;
            panelBottom.Location= new Point(100,200);
            panelBottom.Size= new System.Drawing.Size(400,100);
            panelBottom.Name= "panelBottom";
            // resize except up direction
            panelBottom.Anchor= AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Bottom;

            // create Buttons and add to bottom panel
            button1= new Button;
            button1.Location= new Point(20,60);
            button1.Size= new System.Drawing.Size(80,20);
            button1.Name= "button1";
            button1.Text= "One";
            button1.Anchor= AnchorStyles.Left;

            button2= new Button;
            button2.Location= new Point(120,60);
            button2.Size= new System.Drawing.Size(80,20);
            button2.Name= "button2";
            button2.Text= "Two";
            button2.Anchor= AnchorStyles.Left;

            button3= new Button;
            button3.Location= new Point(220,60);
            button3.Size= new System.Drawing.Size(80,20);
            button3.Name= "button3";
            button3.Text= "Three";
            button3.Anchor= AnchorStyles.Left;

            // add buttons to bottom panel
            panelBottom.Controls.Add(button1);
            panelBottom.Controls.Add(button2);
            panelBottom.Controls.Add(button3);

            // add bottom panel to base panel
            panelBase.Controls.Add(panelBottom);

            // add base panel to form
            this.Controls.Add(panelBase);
        }
    } // end_class
} // end_package

// enable event loop
Application.Run(new JAL.TestEvents());

Learn More About Delegation

The .NET framework uses delegation to send and receive events. In some frameworks, events bubble up through the component hierarchy and we trap the events as needed. However, in the delegation model, only objects that have registered an interest in an event are notified of the event. We register and unregister events in JScript directly using the add_XXXX() and remove_XXXX() methods. (This differs from the mechanism in C#.) In a sense, the event source "calls back" the event listener or event sink by invoking the event listener method. In this project, we call the add_Click method to register an interest in the "Click" event. We pass the name of the event listener method to the event source. When the event is fired, the event listener method is called.

Previous  Next

Send mail to [email protected] with questions or comments about this web site. Copyright © 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 © 
Last modified: 08/04/09
Hosted by www.Geocities.ws

1