September 7, 2002 - Overriding an Event Handler | WebReference

September 7, 2002 - Overriding an Event Handler

Yehuda Shiran September 7, 2002
Overriding an Event Handler
Tips: September 2002

Yehuda Shiran, Ph.D.
Doc JavaScript

Sometimes you want to override an event handler of a class you inherit. Suppose you want to redefine the function OnMouseUp() of the class System.Windows.Forms.Panel, by adding a message that displays the coordinates of the event. You need to define a new class, say ClickPanel, which extends the class System.Windows.Forms.Panel. You need to:

OnMouseUp() from the super class, System.Windows.Forms.Panel
  • Add a message box that displays the coordinates of the event.
  • You may want to 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);
      } 
    Integrating into the class ClickPanel:

      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);
        } 
      } 
    To learn more about JScript .NET and ASP.NET, go to Column 117, JScript .NET, Part XI: Creating Windows Forms.