Working with Text

After selecting an edit control, a user can select text in the control by using a pointing device or keyboard keys. You can retrieve the starting and ending character positions of the current selection in an edit control by sending the control an EM_GETSEL message.

You can also select text in an edit control by sending the control an EM_SETSEL message with the starting and ending character indexes for the selection. For example, you can use EM_SETSEL with EM_REPLACESEL to delete text from an edit control. These three messages apply to both single-line and multiline edit controls.

You can replace selected text in an edit control by sending the control an EM_REPLACESEL message with a pointer to the replacement text. If there is no current selection, EM_REPLACESEL inserts the replacement text at the insertion point. You might receive an EN_ERRSPACE notification message if the replacement text exceeds the available memory. This message applies to both single-line and multiline edit controls. You can use EM_REPLACESEL to replace part of an edit control's text or the SetDlgItemText function to replace all of it.

Manipulating Text

Windows CE provides four messages for moving text between an edit control and the Clipboard. These four messages apply to both single-line and multiline edit controls. The following table describes these messages.

Message
Description
WM_COPY Copies the current selection, if any, from an edit control to the Clipboard without deleting it from the edit control
WM_CUT Deletes the current selection, if any, in the edit control and copies the deleted text to the Clipboard
WM_CLEAR Deletes the current selection, if any, from an edit control, but does not copy it to the Clipboard unless a user presses the SHIFT key
WM_PASTE Copies text from the Clipboard into an edit control at the insertion point

When a user selects, deletes, or moves text in an edit control, Windows CE maintains an internal flag for each edit control indicating whether the content of the control has been modified. Windows CE clears this flag when it creates the control and sets the flag when the text in the control is modified. You can retrieve the modification flag by sending the control an EM_GETMODIFY message and set or clear the modification flag by sending the control an EM_SETMODIFY message. These messages apply to both single-line and multiline edit controls.

The default limit of text that a user can type in an edit control is 30,000 characters. You can change the amount of text a user can type by sending the control an EM_SETLIMITTEXT message. This message sets a hard limit to the number of bytes a user can type into an edit control, but affects neither text in the control when the message is sent, nor text copied to the control by the SetDlgItemText function or the WM_SETTEXT message. For example, suppose that you use the SetDlgItemText function to place 500 characters in an edit control, and a user also types 500 characters in the edit control, creating a total of 1,000 characters. If you send an EM_SETLIMITTEXT message limiting user-entered text to 300 characters, the 1,000 characters already in the edit control remain there, and a user cannot add any more text. On the other hand, if you send an EM_SETLIMITTEXT message limiting user-entered text to 1,300 characters, the 1,000 characters remain, but a user can add 300 more characters.

When a user reaches the character limit of an edit control, Windows CE sends the application a WM_COMMAND message containing an EN_MAXTEXT notification message. This notification message does not mean that memory has been exhausted, but that the limit for user-entered text has been reached; a user cannot type any more text. To change this limit, you must send the control a new EM_SETLIMITTEXT message with a higher limit.

Working with Wordwrap Functions

You can direct a multiline edit control to add or remove a soft line break character—two carriage returns and a linefeed—automatically at the end of wrapped text lines. An application can turn this feature on or off by sending the edit control an EM_FMTLINES message. This message applies only to multiline edit controls and does not affect a line that ends with a hard line break—one carriage return and a linefeed typed by a user.

Retrieving Points and Characters

To determine which character is closest to the specified point in an edit control, send the EM_CHARFROMPOS message. The message returns the character index and line index of the character nearest the point. Similarly, you can determine the client coordinates of the specified character in an edit control by sending the EM_POSFROMCHAR message. You specify the index of a character and the message returns the x-coordinate and y-coordinate of the upper-left corner of the character.

Undoing Text Operations

Every edit control maintains an undo flag that indicates if an application can reverse the most recent operation, such as a text deletion, on the control. The edit control sets the undo flag to indicate that the operation can be undone and resets it to indicate that the operation cannot be undone. You can determine the setting of the undo flag by sending the control an EM_CANUNDO message.

To undo the most recent operation, send the control an EM_UNDO message. An operation can be undone if no other edit control operation occurs first. For example, a user can delete text, replace the text or undo the deletion, and then delete the text again or undo the replacement. The EM_UNDO message applies to both single-line and multiline edit controls and always works for single-line edit controls.