Procedure to Insert Text in Read-Only Edit Region

Last reviewed: December 18, 1996
Article ID: Q110771
The information in this article applies to:
  • Microsoft FoxPro for Windows, versions 2.5, 2.5a, and 2.5b
  • Microsoft FoxPro for MS-DOS, versions 2.0, 2.5, 2.5a, and 2.5b
  • Microsoft FoxPro for Macintosh, version 2.5b

SUMMARY

At times you may want to insert text into a read-only edit region at the location of the cursor. Because the edit region is read-only, text cannot be inserted into the region directly. The code below demonstrates how this can be done.

MORE INFORMATION

To obtain the location of the cursor, you must copy the text from the cursor position to the end of the file. This copied text is stored in the variable _CLIPTEXT, where the length of this variable can be compared with the length of the memo field. This comparison will result in the cursor's position.

After the cursor's position has been determined, you can use the STUFF() function to insert text at the specified position.

In the following sample program, the KEYBOARD command is used to stuff commands into the keyboard buffer. These keystrokes are not executed, however, until control is returned from the clauses. In this example, the ON KEY LABEL CTRL+Q command is used to trigger another function after the initial KEYBOARD commands have been executed. The second ON KEY LABEL command is disabled after it has been executed.

   ** Initialize variables
   string = "This is a text string. "
   newstr = ""
   ** Declare F2 as shortcut key to add text to edit region
   ON KEY LABEL F2 DO editmemo
   ** Define edit region and additional GET field
   @ 2,2 EDIT string SIZE 10,25 SCROLL NOMODIFY
   @ 12,2 GET test DEFAULT SPACE(10)
   ** Activate edit region and GET
   READ CYCLE

   ** Procedure to add text to the edit region
   PROCEDURE editmemo
   ** Reset the contents of 'newstr' to empty spaces
   newstr = SPACE(500)
   ** Define and activate window to input text
   DEFINE WINDOW editmemo FROM 10,2 TO 24,50 TITLE "Add to Memo Field"
   ACTIVATE WINDOW editmemo NOSHOW
   ** Define edit region and push button
   @ 1,1 EDIT newstr SIZE 8,45 SCROLL
   @ 10,2 GET choice DEFAULT 1 PICTURE "@*T Done"
   READ
   ** Remove window from screen and memory
   RELEASE WINDOW editmemo
   ** Stuff keyboard with keystrokes to copy from cursor
   ** to end of file.  This will not be executed until
   ** control is returned back to the keyboard.
   KEYBOARD "{SHIFT+PGDN}"
   KEYBOARD "{CTRL+C}"
   ** Set up keystrokes to insert text
   ON KEY LABEL CTRL+Q DO insertxt
   ** Stuff keyboard with keystrokes to insert text
   KEYBOARD "{CTRL+Q}"
   RETURN 0

   ** Procedure to insert the text
   PROCEDURE insertxt
   ** Determine the difference between the length of the memo
   ** field and the length of the selected text.
   location = LEN(string) - LEN(_cliptext)
   ** Insert at the location of the previously determined
   ** difference.
   string = STUFF(string,location,0,newstr)
   ** Turn off the keystrokes to enable this procedure.
   ON KEY LABEL CTRL+Q
   ** Move to the beginning of the file.
   KEYBOARD"{CTRL+HOME}"
   RETURN 0

To test the program, place the cursor in the middle of the text string "This is a text string". Press the F2 key, and type any additional text in the second window. Choose the Done button when you are finished. The additional text is added to the edit region where the cursor was located. The cursor is moved to the beginning of the edit region.

NOTE: This routine does not work the same way in Visual FoxPro due to differences in the way it reads functions.


KBCategory: kbenv kbprg kbcode
KBSubcategory: FxenvMemory
Additional reference words: VFoxWin 3.00 FoxMac FoxDos FoxWin 2.00 2.50
2.50a 2.50b


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.

Last reviewed: December 18, 1996
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.