Using the InputMask Property
The InputMask property determines the characteristics of each character typed into the text box. For example, you could set the InputMask property to 999,999.99 to limit user input to numeric values less than 1,000,000 with two decimal places. The comma and the period would be displayed in the text box before the user entered any values. If the user pressed a character key, the character would not be displayed in the text box.
If you have a logical field and want a user to be able to type "Y" or "N" but not "T" or "F", set the InputMask property to "Y".
Accepting User Passwords in a Text Box
Often in an application, you want to obtain secure information from a user, such as a password. You can use a text box to get this information without making the information visible on the screen.
To accept user input without displaying the actual value
- Set the PasswordChar property of the text box to * or some other generic character.
If you set the PasswordChar property to anything other than an empty string, the Value and Text properties of the text box contain the actual value that the user typed in the text box, but the text box displays a generic character for every key the user pressed.
Entering Dates in a Text Box
Text boxes have several properties that you can set to make it easy for your users to enter date values.
Property | Description |
---|---|
Century | Sets whether the first two digits of the year are displayed or not. |
DateFormat | Formats the date in the text box to one of fifteen predetermined formats, such as German or Japanese. |
StrictDateEntry | Sets StrictDateEntry to 0 - Loose. This makes it possible for a user to enter dates in more flexible formats than the default 99/99/99. |
Common Text Box Properties
The following text box properties are commonly set at design time.
Property | Description |
---|---|
Alignment | Sets whether the contents of the text box are left justified, right justified, centered, or automatic. Automatic alignment depends on the data type. Numbers, for example, are right justified and characters are left justified. |
ControlSource | Specifies the table field or variable whose value is displayed in the text box. |
InputMask | Specifies the data entry rule each character entered must follow. For specific information about InputMask, see Help. |
SelectOnEntry | Sets whether the contents of the text box are selected automatically when the text box receives the focus. |
TabStop | Specifies whether the user can tab to the control. If TabStop is set to .F., a user can still select the text box by clicking it. |
Using 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 word-wrapping 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
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 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. |
|
Use the GETFILE( ) function to return the name of the file to open. Store the name in the FileName field of the cursor. |
|
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. |
|
Fill the memo field with the text in the file. |
|
Set the ControlSource of the edit box on the form. |
|
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 |
---|---|
|
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 andTHISFORM.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 doesn't have the focus. |
ReadOnly | Whether the user can change the text in the edit box. |
ScrollBars | Whether there are vertical scrollbars. |
Using Combo Boxes
The combo box control has the functionality of a list box and a text box. There are two styles for a combo box: Drop-down combo and Drop-down list. Specify which one you want by changing the Style property of the control. Drop-down lists are discussed in Application of List Boxes and Drop-Down List Boxes.
Drop-Down Combo Box
A user can click the button on a drop-down combo box to see a list of choices or enter a new item directly in the box beside the button. The default Style property of a combo box is 0 — Dropdown Combo.
Adding User Items to Drop-Down Combo Box Lists
To add the new user value to the drop-down combo box, you can use the following line of code in the method associated with the Valid event of the combo box:
THIS.AddItem(THIS.Text)
Before adding an item, however, it would be a good idea to check to make sure that the value isn't already in the combo box drop-down:
lItemExists = .F. && assume the value isn't in the list.
FOR i = 1 to THIS.ListCount
IF THIS.List(i) = THIS.Text
lItemExists = .T.
EXIT
ENDIF
ENDFOR
IF !lItemExists
THIS.AddItem(THIS.Text)
ENDIF
Common Combo Box Properties
The following combo box properties are commonly set at design time.
Property | Description |
---|---|
ControlSource | Specifies the table field where the value that the user chooses or enters is stored. |
DisplayCount | Specifies the maximum number of items displayed in the list. |
InputMask | For drop-down combo boxes, specifies the type of values that can be typed in. |
IncrementalSearch | Specifies whether the control tries to match an item in the list as the user types each letter. |
RowSource | Specifies the source of the items in the combo box. |
RowSourceType | Specifies the type of the source for the combo box. The RowSourceType values for a combo box are the same as for a List. |
Style | Specifies whether the combo box is a drop-down combo or a drop-down list. |
See Also
Accepting Input That Cannot Be Predetermined | Accepting Numeric Input in a Given Range | Using Controls | Allowing Specific Actions | Controls and Objects | Click Event