Partager via


Method and Event Code Guidelines

In addition to writing code for the methods and events of an object, you can extend the set of methods in subclasses of Visual FoxPro base classes. Here are the rules for writing event code and methods:

  • The event set for the Visual FoxPro base classes is fixed and cannot be extended.
  • Every class recognizes a set of fixed default events, the minimum set of which includes Init, Destroy, and Error events.
  • When you create a method in a class definition with the same name as an event that the class can recognize, the code in the method is executed when the event occurs.
  • You can add methods to your classes by creating a procedure or function within the class definition.
  • You can create Access and Assign Methods for your classes by creating a procedure or function with the same name as a class property and _ACCESS or _ASSIGN appended to the procedure or function name.

Calling Event Code up the Class Hierarchy

When you create a class, the class automatically inherits all the properties, methods, and events of the parent class. If code is written for an event in the parent class, that code is executed when the event occurs with respect to an object based on the subclass. You can, however, overwrite the parent class code by writing code for the event in the subclass.

To explicitly call the event code in a parent class when the subclass has code written for the same event, use the DODEFAULT( ) function.

For example, you could have a class named cmdBottom based on the command button base class that has the following code in the Click event:

GO BOTTOM
THISFORM.Refresh

When you add an object based on this class to a form, named, for example, cmdBottom1, you might decide that you also want to display a message for the user so that he or she knows that the record pointer is at the bottom of the table. You could add the following code to the Click event of the object to display the message:

WAIT WINDOW "At the Bottom of the Table" TIMEOUT 1

When you run the form, however, the message is displayed, but the record pointer doesn't move because the code in the Click event of the parent class is never executed. To make sure the code in the Click event of the parent class is also executed, include the following lines of code in the Click event procedure of the object:

DODEFAULT( )
WAIT WINDOW "At the Bottom of the Table" TIMEOUT 1

Note   You can use the ACLASS( ) function to determine all the classes in an object's class hierarchy.

Preventing Base Class Code from Executing

Sometimes you'll want to prevent the base class default behavior from taking place in an event or method. You can do this by including the NODEFAULT keyword in the method code you write. For example, the following program uses the NODEFAULT keyword in the KeyPress event of a text box to prevent the typed characters from being displayed in the text box:

frmKeyExample = CREATEOBJECT("test")
frmKeyExample.Show
READ EVENTS
DEFINE CLASS test AS FORM
  ADD OBJECT text1 AS TEXTBOX
  PROCEDURE text1.KeyPress
   PARAMETERS nKeyCode, nShiftAltCtrl
   NODEFAULT
   IF BETWEEN(nKeyCode, 65, 122) && between 'A' and 'z'
    This.Value = ALLTRIM(This.Value) + "*"
    ACTIVATE SCREEN      && send output to main Visual FoxPro window
    ?? CHR(nKeyCode)
   ENDIF
  ENDPROC
  PROCEDURE Destroy
   CLEAR EVENTS
  ENDPROC
ENDDEFINE

See Also

Writing Class Definitions Programmatically | Addition of Objects to a Container Class | Creating a Set of Table Navigation Buttons | Init | Destroy | Error | Access and Assign Methods | Object-Oriented Programming