Q131095: How to Control the Occurrence of a Default Event

Article: Q131095
Product(s): Microsoft FoxPro
Version(s): 3.00
Operating System(s): 
Keyword(s): kbcode
Last Modified: 26-AUG-1999

-------------------------------------------------------------------------------
The information in this article applies to:

- Microsoft Visual FoxPro for Windows, version 3.0 
-------------------------------------------------------------------------------

SUMMARY
=======

An event is an action generated by a user, a program, or the system. When an
event is triggered, Visual FoxPro processes some default system tasks. For
example, pressing a key triggers the keypress event that causes the letter to be
displayed in a text box. With Visual FoxPro, you can control the occurrence of
system events. This article describes how to prevent system processing of
events, and how to cause default events to occur before user-generated or
code-generated events occur.

You can add code to a method to customize operations that occur when an event is
triggered. When an event handler is customized, system processing occurs in
addition to the tasks defined in user code. By default, the user code is
processed first followed by system processing.

MORE INFORMATION
================

Preventing System Processing from Occurring
-------------------------------------------

Use the NODEFAULT keyword in method code to prevent system processing of events.
For example, in an input field, you can prevent some characters from being
displayed by using the NODEFAULT clause in a Keypress event handler. The default
behavior when a key is pressed is to display the character being typed. The
NODEFAULT keyword is used in the MAIN form of the CONTROLS project located in
the SAMPLES\CONTROLS directory.

For more information about NODEFAULT, search for DEFINE CLASS in the Visual
FoxPro Help menu.

Forcing System Processing to Occur Before User-Defined Code Is Processed
------------------------------------------------------------------------

You can cause the system code for the event to be processed first - before the
user-defined code is prcessed. This is useful, for example, if you want to
control the starting position of a text selection when the control gets the
focus. Default processing of the GotFocus event causes the SelStart property to
be set to zero (0), and the insertion point (cursor) is positioned before the
first character displayed in the control.

You can't just set the value of the SelStart property in the GotFocus event
because the GotFocus default processing occurs after user code is processed, so
the SelStart property is reset to zero (0). The default processing of the
GotFocus event must occur before the value of the SelStart property is
customized.

To make the default processing occur first, you need to modify the GotFocus event
code in the GotFocus event handler to follow this sequence:

1. Call the base class GotFocus event.

2. Set the SelStart property in the GotFocus event code of the text box.

3. Place the NODEFAULT clause, which stops the default processing of GotFocus
  from occurring again after your code.

The following example code demonstrates this method.

Sample Code to Place in the GotFocus Method of a Text Box Control
-----------------------------------------------------------------

  * First, force default processing:
  Textbox::GotFocus()

  * Set the SelStart property:
  This.SelStart = 4

  * Default processing already occurred, so prevent it from occurring again:
  NODEFAULT

Additional query words: VFoxWin form selection

======================================================================
Keywords          : kbcode 
Technology        : kbVFPsearch kbAudDeveloper kbVFP300
Version           : 3.00

=============================================================================