JScript .NET, Part XI: Creating Windows Forms: Redefining the OnMouseUp Event Handler - Doc JavaScript | WebReference

JScript .NET, Part XI: Creating Windows Forms: Redefining the OnMouseUp Event Handler - Doc JavaScript


JScript .NET, Part XI: Creating Windows Forms

Redefining the OnMouseUp Event Handler

In this page, we demonstrate how to override an event handler. We define a new class, ClickPanel, which extends the class System.Windows.Forms.Panel. We redefine the function OnMouseUp() by:

  • Including the definition of OnMouseUp() from the super class, System.Windows.Forms.Panel
  • Adding a message box that displays the coordinates of the event.

We specify that the new function is overriding the super class and is protected against further overriding by other classes that may inherit it:

override protected function OnMouseUp (e : MouseEventArgs) {
  super.OnMouseUp(e);
  MessageBox.Show("Mouse Up at: "+e.X+", "+ e.Y);
} 

The class ClickPanel is defined as follows:

class ClickPanel extends System.Windows.Forms.Panel {
  override protected function OnMouseUp (e : MouseEventArgs) {
    super.OnMouseUp(e);
    MessageBox.Show("Mouse Up at: "+e.X+", "+ e.Y);
  } 
} 

We want to change the top panel to behave as the ClickPanel class, instead of the generic Panel class. We first change its definition:

private var topPanel: ClickPanel;

And then we need to change its construction statement:

topPanel= new ClickPanel;

The rest of the code is as shown in Page 5. You pop up this window by calling the Application.Run() method. The package name is MouseUpPkg and the class name is MouseUpCls. The running statement is:

Application.Run(new MouseUpPkg.MouseUpCls());

Whenever you click inside the top panel, a message box will pop up with the event coordinates, like this:


Next: How to create form menus


Produced by Yehuda Shiran and Tomer Shiran
All Rights Reserved. Legal Notices.
Created: August 26, 2002
Revised: August 26, 2002

URL: http://www.webreference.com/js/column117/6.html

/HTML>