Edit Controls

An edit control is a rectangular child window in which the user can enter and edit text. Edit controls have a variety of features, such as multiple-line editing and scrolling. You specify the features you want by specifying a control style.

Edit control styles define how the edit control will appear and operate. For example, the ES_MULTILINE style creates an edit control in which you can enter more than one line of text. The ES_AUTOHSCROLL and ES_AUTOVSCROLL styles direct the edit control to scroll horizontally or vertically if the user enters more text than can fit in the control's client area. If these styles are not specified and the user enters more text than can fit on one line, the text wraps to the next line if it is a multiline edit control. You can also use the WS_HSCROLL and (for a multiline edit control) WS_VSCROLL styles to an edit control to allow the user to scroll the text in the control.

Your application can use an edit control to let a user enter a password or other private text without displaying the password. The ES_PASSWORD style creates an edit control that does not display text as the user types it; instead, the edit control displays an arbitrary character for each character that the user types. By default, this character is an asterisk (*). To change the character displayed by the edit control, send the EM_SETPASSWORDCHAR message to the control.

You can set tab stops in a multiline edit control by sending the EM_SETTABSTOPS message to the control. This message specifies the number of tab stops the edit control should contain and the distances between the tab stops.

An edit control sends notification messages to its parent window. For example, an edit control sends an EN_CHANGE message when the user makes a change to the text. An edit control can also receive messages, such as EM_GETLINE and EM_LINELENGTH. An edit control carries out the specified action when it receives a message.

A particularly powerful feature of edit controls allows you to “undo” a change to the contents of the edit control. To determine whether an edit control can undo an action, send the EM_CANUNDO message to the control; the control will return a nonzero value if it can undo the last change. If it can, your application can send the EM_UNDO message to the control to reverse the last change made to the edit control.

The following list describes the actions and results for the mouse and keyboard interface for edit controls.

Mouse Interface

Single click

Positions the insertion point and drops the selection anchor.

Double click

Selects a word.

SHIFT+Single click

Positions the insertion point and extends the selection from the selection anchor to the insertion point.

Drag

Drops the selection anchor, moves the insertion point, and extends the selection from the selection anchor to the insertion point.

Keyboard Interface

ARROW

Removes the selection from any text and moves the insertion point in the indicated direction.

SHIFT+ARROW

Drops the selection anchor (if it is not already dropped), moves the insertion point, and selects all text between the selection anchor and the insertion point.

CTRL+LEFT ARROW, CTRL+RIGHT ARROW

Moves the insertion point to the beginning of the word in the indicated direction.

SHIFT+CTRL+LEFT ARROW, SHIFT+CTRL+RIGHT ARROW

Drops the selection anchor (if it is not already dropped), moves the insertion point to the beginning of the word in the indicated direction, and selects all text between the selection anchor and the insertion point.

HOME

Removes the selection from any text and moves the insertion point to the beginning of the line.

SHIFT+HOME

Drops the selection anchor (if it is not already dropped), moves the insertion point to the beginning of the line, and selects all text between the selection anchor and the insertion point.

CTRL+HOME

Places the insertion point before the first character in the edit control.

SHIFT+CTRL+HOME

Drops the selection anchor (if it is not already dropped), places the insertion point before the first character in the edit control, and selects all text between the selection anchor and the insertion point.

END

Removes the selection from any text and moves the insertion point to the end of the line.

SHIFT+END

Drops the selection anchor (if it is not already dropped), moves the insertion point to the end of the line, and selects all text between the selection anchor and the insertion point.

CTRL+END

Places the insertion point after the last character in the edit control.

SHIFT+CTRL+END

Drops the selection anchor (if it is not already dropped), places the insertion point after the last character in the edit control, and selects all text between the selection anchor and the insertion point.

DEL

If text is selected, deletes (clears) the text. Otherwise, deletes the character following the insertion point.

SHIFT+DEL

If text is selected, cuts the text to the clipboard. Otherwise, deletes the character before the insertion point.

SHIFT+INS

Pastes (inserts) the contents of the clipboard at the insertion point.

CONTROL+INS

Copies selected text to the clipboard, but does not delete it.

PGUP

In a multiline edit control, scrolls text up one line less than the height of the edit control.

CTRL+PGUP

In a multiline edit control, scrolls text left one character less than the width of the edit control.

PGDN

In a multiline edit control, scrolls text down one line less than the height of the edit control.

CTRL+PGDN

In a multiline edit control, scrolls text right one character less than the width of the edit control.

CTRL+ENTER

In a multiline edit control in a dialog box, ends the line and moves the cursor to the next line.

CTRL+TAB

In a multiline edit control in a dialog box, inserts a tab character.

The EditCntl sample application described at the end of this chapter illustrates how to use a multiline edit control to provide basic text entry and editing.