Working with EDITTEXT Controls


 

EDITTEXT boxes are versatile controls that get input from the user or display text. EDITTEXT boxes should not be used to display text that you don't want the user to change, unless you've set the Locked property to True.

 

The actual text displayed in a text box is controlled by the Text property. It can be set in three different ways: at design time in the Property window, at run time by setting it in code using the SETITEM instruction, or by input from the user at run time. The current contents of a text box can be retrieved at run time by the GETITEM instruction.

 

Multiple-Line EDITTEXT Boxes and Word Wrap

By default, an EDITTEXT box displays a single line of text and does not display scroll bars. If the text is longer than the available space, only part of the text will be visible. The look and behavior of an EDITTEXT box can be changed by setting two properties, multiline and scrollbars, which are available only at design time.

 

Note: The scrollbars property should not be confused with scroll bar controls, which are not attached to EDITTEXT boxes and have their own set of properties.

 

Setting multiline to true enables an EDITTEXT box to accept or display multiple lines of text at run time. A multiple-line EDITTEXT box automatically manages word wrap as long as there is no horizontal scroll bar. The scrollbars property is set to 0-None by default. Automatic word wrap saves the user the trouble of inserting line breaks at the end of lines. When a line of text is longer than what can be displayed on a line, the EDITTEXT box wraps the text to the next line.

 

Line breaks cannot be entered in the Properties window at design time. Within the code, you create a line break by inserting a carriage return followed by a linefeed (ANSI characters 13 and 10). For example, the following code puts two lines of text into a multiple-line EDITTEXT box (EDIT1):

 

EDIT1

EDITTEXT

 

CRLF

INIT

015,012

 

PACK

STRING WITH "Here are two lines",CRLF,"in a text box"

 

SETITEM

EDIT1,0,STRING

 

Working with Text in an EditText

You can control the insertion point and selection behavior in an EDITTEXT box with the SETITEM instruction. When the second operand {item} is one, the starting position of the selected text is changed to the value of {data}. When {item} is two, the ending position of the selected text is changed to the value of {data}. The selected text will always be brought into view for the object. If the starting and ending position is set to the same value, the cursor will be moved to the specified position but no text will be selected.

 

When an EDITTEXT box first receives the focus, the default insertion point or cursor position within the text box is to the left of any existing text. It can be moved by the user from the keyboard or with the mouse. If the EDITTEXT box loses and then regains the focus, the insertion point will be wherever the user last placed it.

 

In some cases, this behavior can be disconcerting to the user. In a word processing application, the user might expect new characters to appear after any existing text. In a data entry application, the user might expect their typing to replace any existing entry. The selection starting position and selection length modes of SETITEM allow you to modify the behavior to suit your purpose.

 

The selection starting position is a number that indicates the insertion point within the string of text, with one being the left-most position. If the selection starting position is set to a value equal to or greater than the number of characters in the text box, the insertion point will be placed after the last character.

 

The selection length is a numeric value that sets the width of the insertion point. Setting the selection length to a number greater than one causes that number of characters to be selected and highlighted, starting from the current insertion point.

 

If the user starts typing while a block of text is selected, the selected text will be replaced. In some cases, you might want to replace a text selection with new text by using a paste command. The selected text is a string of text that you can assign at run time to replace the current selection. If no text is selected, SETITEM will insert its text at the current insertion point.



PL/B IDE Studio Help Using STATTEXT to Display Data