Microsoft KB Archive/136479

From BetaArchive Wiki

How To Simulate an Incremental Search from a Text Box

Article ID: Q136479
Creation Date: 10-SEP-1995
Revision Date: 18-OCT-1996 The information in this article applies to:

  • Microsoft Visual FoxPro for Windows, versions 3.0, 5.0
  • Microsoft Visual FoxPro for Macintosh, version 3.0b

SUMMARY


The combo box control has an Incremental Search property that, when set to true (.T.), searches the Control Source of the combo box with each keystroke. Thus, if a user types the string ABC, the table is searched after each keystroke, first for the string A, then for the string AB, and then for the string ABC. Although the text box control does not have this property, you can simulate this action. Two examples are given in this article, one for a form with a grid, the other for a simple form.

MORE INFORMATION


NOTE: These examples are case sensitive.

Example One - Form with a Grid


  1. Create a new form, and add the Customer table from the Testdata database (located in the Samples subdirectory) to the data environment. Drag the table onto the form to create a grid.
  2. Add a text box to the form. Do not specify a control source for the text box. In the Load event of the form, enter the command "set order to cust_id" (or whatever the name of the index tag for the cust_id field is).
  3. Add a custom property to the form called HoldValue. Change the default value for this property to blank. Add the following code to the text box Keypress event procedure:

    * Start Code Example * Turn off all default processing of the Keypress Event; * this is necessary for code to work NODEFAULT * nKeycode >= 65, <= 122 limits input to alpha characters, nKeycode = 32 * allows for spaces. To allow for input of numerals and/or other * characters, refer to the INKEY() topic in the Help file for codes * Builds the string as user enters keystrokes IF (nKeycode >= 65 and nKeycode <= 122) or nKeyCode = 32

            THIS.VALUE = THIS.VALUE + CHR(nKeyCode)
            GO TOP
            SEEK THIS.VALUE
            IF FOUND()

    * The custom property holdvalue is used to store the string * the user has entered

                 THISFORM.HOLDVALUE = THIS.VALUE

    * Changing the focus will lose the string; you must save it * in order to continue building

                 THISFORM.GRID1.SETFOCUS

    * This sets focus to the grid so that the located record is * displayed in the grid and the RecordMark is turned on for * the located record

                 THISFORM.TEXT1.SETFOCUS

    * Reset focus immediately to the text box where the user is * entering the string

                 THIS.VALUE = THISFORM.HOLDVALUE

    * Reset value of text box to string user had entered before seek

                 THIS.SelStart = LEN(THIS.VALUE)

    * This moves the cursor to the end of the string in the text box

            ENDIF

    ELSE * Tests/traps for the ENTER key

            IF nKeycode = 13

    * This resets the string to a null or empty string so the user can * start a new search without having to exit and re-enter the text * box.

              THIS.VALUE = ""
            ENDIF

    * Tests/traps for the Backspace key

            IF nKeycode = 127

    * Emulates the backspace key by removing the last character * from the string

                 THIS.VALUE = LEFT(THIS.VALUE, LEN(ALLTRIM(THIS.VALUE)) - 1)

    * If the new string is one or more characters, repeat the seek * on the new, shortened string

                 IF LEN(ALLTRIM(THIS.VALUE)) > 0
                      GO TOP
                      SEEK THIS.VALUE
                      IF FOUND()

    * Refer to previous section for comments

                           THISFORM.HOLDVALUE = THIS.VALUE
                           THISFORM.GRID1.SETFOCUS
                           THISFORM.TEXT1.SETFOCUS
                           THIS.VALUE = THISFORM.HOLDVALUE
                      ENDIF
                 ENDIF

    * Moves the cursor to the end of the new string

                 THIS.SelStart = LEN(THIS.VALUE)
            ENDIF

    ENDIF * End Code Example

  4. Save and run the Form. Note that as you enter text into the text box, the record pointer (seen in the Grid) moves to the closest matching record.

Example Two - Simple Form, no Grid

  1. Create a new form, and add the Customer table to the data environment as in "Example One."
  2. Add a text box (Text1) that is not bound to any field in the table.
  3. Drag individual fields rather than the whole table onto the form; do not create a grid object for this form.
  4. In the Load event of the form, enter the command "set order to cust_id" (or whatever the name of the index tag for the cust_id field is).
  5. Add a custom property to the Form called HoldValue. Change the default value for this property to blank.
  6. Add the following code to the Keypress event of the text box:

    * Start Code Example * Turns off all default processing of the Keypress event; * this is necessary for code to work NODEFAULT * nKeycode>=65,<=122 limit input to alpha characters, * nKeycode = 32 allows for spaces * to allow for input of numerals and/or other characters, * refer to the INKEY() topic in the Help file for codes IF (nKeycode >=65 and nKeycode <=122) or nKeyCode = 32 * Builds the string as user enters it

            THIS.VALUE=THIS.VALUE+CHR(nKEYCODE)
            GO TOP
            SEEK THIS.VALUE
            IF FOUND()

    * The custom property holdvalue is used to store the string * the user has entered

                 THISFORM.HOLDVALUE = THIS.VALUE

    * Refresh form to show located record

                 THISFORM.REFRESH

    * Return focus to text box to continue search

                 THISFORM.TEXT1.SETFOCUS

    * Reset value of text box to string user had entered * before seek

                 THIS.VALUE = THISFORM.HOLDVALUE

    * This moves the cursor to the end of the string in the * text box

                 THIS.SELSTART=LEN(THIS.VALUE)
            ENDIF

    ELSE * Tests/traps for the ENTER key

            IF nKeycode = 13

    * This resets the string to a null or empty string * so the user can start a new search * without having to exit and re-enter the text box.

                 THIS.VALUE = ""
            ENDIF

    * Tests/traps for the backspace key

            IF nKeycode = 127

    * Emulate the backspace key by removing the * last character from the string

                  THIS.VALUE= LEFT(THIS.VALUE, LEN(ALLTRIM(THIS.VALUE))-1)

    * If the new string is one or more characters, repeat the seek * on the new, shortened string

                  IF LEN(ALLTRIM(THIS.VALUE)) > 0
                       GO TOP
                       SEEK THIS.VALUE
                       IF FOUND()

    * refer to previous section for comments

                            THISFORM.HOLDVALUE = THIS.VALUE
                            THISFORM.REFRESH
                            THISFORM.TEXT1.SETFOCUS
                            THIS.VALUE = THISFORM.HOLDVALUE
                       ENDIF
                  ENDIF

    * Move the cursor to the end of the new string

                  THIS.SELSTART=LEN(THIS.VALUE)
            ENDIF

    ENDIF * End Code Example

  7. Save and run the form. Note that as you enter text into the Text1 box, the fields on the form display data from the closest matching record in the database.

General Notes

In both examples, it is assumed that the text box is named Text1. If the name of that text box is different, modify the code accordingly.

The NODEFAULT command turns off all of the default processing of the Keypress event and allows the code to completely control the processing of the keypresses. This command is necessary.

The SelStart statement ensures that when the focus is returned to the text box, the cursor is at the end of the string.

The HoldValue custom property stores and resets the value of the text box control. A global variable could be used instead.



THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

©1997 Microsoft Corporation. All rights reserved. Legal Notices.


Additional reference words: 5.00 VFoxMac 3.00b 3.00 VFoxWin
KBCategory: kbtool kbcode
KBSubcategory: FxtoolFormdes