JAL Computing

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

 

Home
Up

In this project, TestEvents2.js, we respond to a MouseUp in a panel and extract data from MouseEventArgs. We then display a Message Box with the coordinates of the mouse event.

Things get a bit complicated when we try to handle a MouseUp event in a panel. To do this we can extend System.Windows.Forms.Panel and override the OnMouseUp event handler. In this code example we explicitly tell the complier to "override" OnMouseUp, enabling the compiler to warn us if the signature does not match a signature in base class method. Note the call to super.OnMouseUp(e). This may not be necessary if the base class method OnMouseUp is empty, but it is a good habit to call the base class method whenever you override a method.

Source Code

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

package JAL {

    // extend Panel to trap MouseUp events
    class MyPanel extends System.Windows.Forms.Panel {
        // override OnMouseUp event handler
        override protected function OnMouseUp (e : MouseEventArgs) {
            super.OnMouseUp(e);
            MessageBox.Show("Mouse Up at: "+e.X+", "+ e.Y);
        }
    } // end_MyPanel


    // demonstrates MouseUp event handling and extracting data from EventArgs
    class TestEvents extends System.Windows.Forms.Form {
    // declare variables
       
private var panelBase : MyPanel;

        // constructor
        function TestEvents() {
            InitializeComponent();
        } // end_constructor

        // 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 MyPanel();
            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;

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

    } // end_class
}  // end_package

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

Learn More About Overriding

One of the advantages of object oriented programming is the ability to reuse software by adding additional properties or methods to an existing class. In this project we want to add a special behavior to the Panel class. We create a new class called MyPanel and use the key word extends that tells the compiler that we want to reuse any non final public or protected methods and properties of the Panel class. Our goal is to modify the behavior of the OnMouseUp method in the Panel class. We do this by explicitly "overriding" the OnMouseUp method. Since the method name and signature in MyPanel matches a method name and signature in the Panel class, the MyPanel method OnMouseUp will be called instead of the Panel method OnMouseUp.

Learning More About Hide

JScript adds the key word hide which tells the compiler that we do _not_ want to override the base class method with the same name and signature. Instead, we will hide the member function in the child class (eg. MyPanel.OnMouseUp()).

Learn More About /versionsafe

The jsc compiler option /versionsafe+ enables warning if a member function would override or hide a base member function and the member function is not explicitly declared with the key word override or hide. If you remove the key word "override" in the program TestEvents2.js and compile as:

jsc /versionsafe+ c:\pathToFolder\TestEvents2.js

You will get a warning that a method matches a method in a base class and is going to be overridden or hidden.

If you change the signature of the overridden method OnMouseUp in the file TestEvents2.js and explicitly declare the method with the key word override you will get a warning that there is no matching method in the base class to override. You might conclude that the best course of action is to explicitly declare overridden or hidden methods and enable the /versionsafe option.


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