|
|
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 Codeimport System; Learn More About OverridingOne 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 HideJScript 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 /versionsafeThe 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. |
Send mail to [email protected]
with questions or comments about this web site. Copyright © 2001, 2002, 2003,
2004, 2005, 2006, 2007, 2008, 2009 ©
|