Partager via


User Input in Edit Boxes

You can make it possible for users to edit text from long character fields or memo fields in edit boxes. Edit boxes allow automatic wordwrapping and the ability to move through the text using the arrow keys, page up and page down keys, and scrollbars.

To see examples of using edit boxes, run Solution.app in the Visual FoxPro ...\Samples\Solution directory. In the tree view, click Controls, and then click Edit boxes.

Allowing Users to Edit a Memo Field in an Edit Box

All you have to do to make it possible for a user to edit a memo field in an edit box is set the ControlSource property of the edit box to the memo field. For example, if you have a memo field named comments in a table named log, you can set the ControlSource property of an edit box to log.comments to make it possible for a user to edit the memo field in the edit box.

Allowing Users to Edit a Text File in an Edit Box

You also can make it possible for a user to edit a text file in an edit box. The following form demonstrates this.

Example form for editing a text file in an edit box

FoxForm1 screenshot

An OK button on the form closes the form with the following command in the Click event code:

RELEASE THISFORM

The other two buttons in this example, cmdOpenFile and cmdSave, make it possible for a user to open a text file and save the file after edits.

Code Associated with the Click Event of cmdOpenFile

Code

Comments

CREATE CURSOR textfile ;

(filename c(35), mem m)

APPEND BLANK

Create a cursor with a character field to hold the name of the text file and a memo field to hold the contents of the text file. Add a blank record to the cursor.

REPLACE textfile.FileName WITH ;

GETFILE("TXT")

Use the GETFILE( ) function to return the name of the file to open. Store the name in the FileName field of the cursor.

IF EMPTY(textfile.FileName)

RETURN

ENDIF

If the user chooses Cancel in the Get File dialog box, the FileName field will be empty and there will be no file to open.

APPEND MEMO mem FROM ;

(textfile.FileName) OVERWRITE

Fill the memo field with the text in the file.

THISFORM.edtText.ControlSource = ;

"textfile.mem"

THISFORM.Refresh

Set the ControlSource of the edit box on the form.

THISFORM.cmdSave.Enabled = .T.

Enable the Save button.

When the file has been opened and edited, the Save button makes it possible for a user to write changes back out to the file.

Code Associated with the Click Event of cmdSave

Code

Comments

COPY MEMO textfile.mem TO ;

(textfile.filename)

Overwrites the old value in the file with the text in the memo field.

Manipulating Selected Text in an Edit Box

Edit boxes and text boxes have three properties that make it possible for you to work with selected text: SelLength, SelStart, and SelText.

You can select text programmatically using the SelStart and SelLength properties. For example, the following lines of code select the first word in an edit box.

Form1.edtText.SelStart = 0 
Form1.edtText.SelLength = AT(" ", Form1.edtText.Text) - 1

Tip

When you change the SelStart property, the edit box scrolls to display the new SelStart. If you change the SelStart in a loop, for example when searching for text, your code will execute faster if you include THISFORM.LockScreen = .T. before processing and THISFORM.LockScreen = .F. after processing.

You can access selected text in an edit box or text box with the SelText property. For example, the following line of code makes the selected text all uppercase:

Form1.edtText.SelText = UPPER(Form1.edtText.SelText)

Common Edit Box Properties

The following edit box properties are commonly set at design time.

Property

Description

AllowTabs

Whether the user can insert tabs in the edit box instead of moving to the next control. If you allow tabs, be sure to indicate that users can move to the next control by pressing CTRL+TAB.

HideSelection

Whether selected text in the edit box is visibly selected when the edit box does not have the focus.

ReadOnly

Whether the user can change the text in the edit box.

ScrollBars

Whether there are vertical scrollbars.

See Also

Reference

Controls for Accepting Input

EditBox Control

Other Resources

Using Controls