Manipulating Edit Control Text
Windows Predefined Controls – Part 6
Volume - Windows User Interface
Forward: In this part of the series, we see how to manipulate edit control text.
By: Chrysanthus Date Published: 29 Aug 2012
Introduction
Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.
Selecting an Edit Control
When you select an edit control, the edit control gains focus. Any alphanumeric character you type on the keyboard should go into the edit control. You select an edit control by clicking it with the mouse or by pressing the TAB key to move to it. So, when the user selects an edit control, the system gives the control the keyboard focus and highlights its text by using reverse video (see later).
Selecting Text
When the edit control is selected, you can go on to select text that is in the edit control. You select a group (consecutive characters) of text in the edit control by dragging the mouse pointer over the group or by using the keyboard (shift key and arrow keys).
Now. After selecting text in the edit control with the mouse, your application can send the EM_GETSEL message (see later) to the control to know the start and ending positions of the selected characters.
An application can also select text in the edit control. The application will send the EM_SETSEL message (see later) with the starting and ending character indexes (positions) for the selection.
Replacing Text
You can replace text in an edit control by manually selecting it and typing over it. An application can do the same thing by sending the EM_REPLACESEL message (see later) to the edit control.
Zero-based indexing is used in numbering characters and lines in an edit control. If a single-line edit control has n characters, then the numbering of the characters is from 0 to n-1. If a multi-line edit control has m lines, then the numbering of the lines is from 0 to m-1. In a multi-line edit control, the last character in the last line is at n-1 position. The counting of characters in a multi-line edit control ignores line breaks.
An application can know the number of lines in a multi-line edit control by sending the EM_GETLINECOUNT message (see later) to the edit control. To know the line index (between 0 and m-1) that has the index of a character (between zero and n-1) send the EM_LINEFROMCHAR message (see later) to the edit control. To know the index of the first character in a specified line send the EM_LINEINDEX message (see later) to the edit control. An application can copy a line from an edit control to a buffer (see later) by sending the EM_GETLINE message (see later) to the edit control.
An application can know the number of characters in a line in a single-line or multi-line edit control by sending the EM_LINELENGTH Message to the edit control (see later).
Setting Tab Stops and Margins
Tab stops divide an edit control into imaginary columns. If you are typing within one column and you press the Tab key on the keyboard, the caret will go to the beginning of the next column. The default for a tab stop (column width) is 8 characters. To set tab stops in a multi-line edit control send the EM_SETTABSTOPS message (see later) to the edit control.
You can set the width of the left and right margins for an edit control by sending the EM_SETMARGINS message. After sending this message, the system redraws the edit control to reflect the new margin settings. You can retrieve the width of the left or right margin by sending the EM_GETMARGINS message to the edit control. We shall see how to send these messages later.
An edit control with the ES_PASSWORD style allows the user to type in password. The default password character is an asterisk (*). An application can use the EM_SETPASSWORDCHAR message (see later) to remove or define a different password character and the EM_GETPASSWORDCHAR message (see later) to retrieve the current password character.
Using Integers
If your edit control has the style, ES_NUMBER, then only digits can enter the edit control, everything being equal. The programmer would not find the use of the ES_NUMBER style convenient. Under normal circumstances, what goes into the edit control is a string. The C++ language can convert a string into an integer. With that you can send the integer directly into the edit control.
However, if you have an integer (signed or unsigned) you cannot used C++ to convert it into a string directly and send to an edit control that does not have the ES_NUMBER style. The User Interface provides the SetDlgItemInt function, which would create the string representation of a specified integer and send the string to the edit control without the ES_NUMBER style. The User Interface provides another function, which is GetDlgItemInt to create an integer (signed or unsigned) from its string representation in an edit control and return the integer to the caller.
Undoing Text Operations
An application can undo the most recent operation in an edit control. The application sends the EM_UNDO message to the edit control. It is not all the times that an operation can be undone. The application can send the EM_CANUNDO message to verify if the last operation can be undone. We shall see how to use all the edit messages of this tutorial in the next part of the series.
As the user is inputting (typing) text into a multi-line edit control the text will wrap onto the next line as the text reaches the right border of the edit control. By default the text wraps at the end of the space between words. We shall allow things like that in this series.
As the user is typing, if he presses the Enter key on the keyboard a new paragraph has to start at the beginning of the next line. Also the carriage return (r) and new line character (n) have to be inserted into the edit control at the point where the user pressed Enter. Without these characters, it will not be possible to redisplay the content of the edit control in another computer maintaining the paragraphs the way the user type it. For these characters to appear when the user presses the Enter key, the multi-line edit control should have the ES_WANTRETURN style. When you press the Enter key twice you develop a blank line.
Retrieving Points and Characters
A point is measured from the left-top corner of its window (control) with the x and y coordinates. However, with windows the y coordinate becomes more positive as you move down, opposite to what happens in mathematics graph. The x and y distances are measured in device units.
For an application to determine the character closest to a specified point in the client area of an edit control, the application should send the EM_CHARFROMPOS message to the control. The message returns the character index and line index of the character nearest the point. Alternatively, the application can retrieve the client area coordinates of a specified character by sending the EM_POSFROMCHAR message. This message returns the x and y coordinates of the left-top corner of the specified character (not its window).
Note: All edit control messages begin with EM_.
I have discussed most of the features of manipulating edit control text in this article. You should consult the manual or some other document for the features not discussed here.
Time to take a break. We stop here and continue in the next part of the series.
Chrys
Related Courses
C++ CourseRelational Database and Sybase
Windows User Interface
Computer Programmer – A Jack of all Trade – Poem
NEXT