Edit controls for VCL Components

Source: Internet
Author: User

In the future, we will refer to components in two ways, either by component name or by defining the name of The VCL class of the component. It can be said that the "label component is used ......" Or "tlabel is used ......", The two methods talk about the same component.

Edit Control)

Delphi has five editing control components: Edit, memo, maskedit, RichEdit (the control is not a standard Windows Control), and spinedit (digital editing box ), it is discussed here because it has many identical features with other editing controls.

1. edit component

The Edit component encapsulates the basic single-row editing control. This component does not have the align and alignment attributes. It does not have the alignment attribute because the text in the single-row editing control can only be left aligned; the Edit component does not have the align attribute because it cannot or should not be expanded to a user area full of windows.

The tedit object is inherited from the tcustomedit object. The inheritance relationship is as follows:

Main attributes of the customedit object
  • Autoselect -- when the input focus moves closer to the editing box, all text in the box is automatically selected
  • Autosize -- if it is true, the height of the edit box automatically adapts to the height of the input text font (valid only when the borderstyle attribute is bssingle)
  • Canundo -- can the Undo method be used to undo the text modification in the edit box?
  • Charcase -- set the case sensitivity of the letters in the editing box. The value is ecnormal and case insensitive. The value of ecuppercase is uppercase, and the value of eclowercase is lowercase.
  • Hideselection -- when this attribute is set to false, even if the input focus leaves the edit box, the text in the box is still in a status that looks like it is selected (the default value is true)
  • Maxlength -- specifies the maximum number of characters that the edit box can accommodate. If it is set to 0, it indicates no limit.
  • Modified -- indicates whether the content of the edit box has been modified since the last modified attribute was changed to false.
  • Oemconvert -- specifies whether to convert the input ANSI character to the OEM character. Generally, this attribute is set to true only when the input file name is used.
  • Passwordchar -- the default value is #0 (null character), indicating that the character entered by the user is displayed. If it is set to another character, the character entered by the user is automatically displayed as this character, which is usually used in the password box, set this attribute to "*"
  • Readonly -- specify whether the displayed input content can be changed
  • Sellength -- indicates the length of the selected string
  • Selstart -- indicates the starting character position. It is usually used with the sellength attribute to select several characters starting at any position.
  • Seltext -- indicates the selected string. Setting the attribute value in the program can replace the selected character.
Important methods:
  • Clear process -- clear all content in the edit box
  • Clearselection process -- clear content in the selected region
  • Clearundo process -- clears the Undo information stored in the buffer, so that the Undo method cannot be used to undo the operation.
  • Copytoclipboard process-copy the selected content to the clipboard
  • Cuttoclipboard process-cut the selected content to the clipboard
  • Getseltextbuf function -- paste the selected content in the edit box to the buffer specified by the buffer parameter, and return the number of copied characters.
  • Pastefromclipboard process-paste the content in the clipboard into the editing box

If you need to edit the text in the component to the right or center, use the memo component, but make its height the height of the standard edit component, and then set the alignment attribute when necessary.


As long as it is possible to maintain the standard form, although the edit component can be made as high as you like, if the height exceeds the height of the standard Windows editing control (for users, it looks like a multi-line editing control), it will confuse users.

2. maskedit component

Maskedit is a "smart" edit box. We can use the maskedit component to create a valid formatted data entry field, which limits users to enter the required symbols at the defined position. For example, in a phone number input box, the operator is required to enter numbers of several digits in the defined format.

Tmaskedit is inherited from tcustommaskedit. The inheritance relationship is as follows:

Double-click the editmask attribute in the object Inspector. The "input mask Editor" dialog box is displayed. This dialog box selects one from the predefined filter or creates one by yourself, as shown below:

The "input mask" edit box in the upper-left corner is used to enter the mask rule. From this edit box, we enter the rule "! \ (999 \) 0000-00000000; 1; _ "it can be seen that the rule is divided into three parts, separated by"; ", different parts are distinguished by different colors:

  • Part 1 "! \ (999 \) 0000-00000000 "defines the data input format. We use some special characters to indicate the type and format of characters to be entered.
  • The second part can only be "1" or "0". When it is "1", non-user input data such as separators in the mask is also saved as part of the data.
  • The "_" symbol in the third part indicates that the space in the data is expressed.

For example, use the mask "! \ (999 \) 0000-00000000; 1; _ ", you can control the user's input in the mask editing box is the same as the phone number format" (086) 0311-83034220. How to implement it is as long as a table with the meaning of a format character is used for comparison, I believe it will soon be mastered.

Special characters used to define the mask format

! Clear the space at the end of the data without "!" The space after the data is retained. This character can only be used as the first character in the mask format.

> All characters that appear after the ">" character are capitalized until the "<" format character is displayed.

<The characters that appear after the "<" character are converted to lowercase letters until the ">" format character is displayed.

<> The Case sensitivity check is not performed. The data is displayed in the case of user input.

\ Any format character that appears after this format character will be treated as a normal character

L this location must contain letters

L (lower-case letter l) The position can be filled with a letter, but it is not required. You can move the cursor to skip it.

A. This position must contain letters or numbers.

A. This position can be filled with a letter or number, but it is not required. You can move the cursor to skip it.

C. Any character can be entered at this position.

C. Any character can be entered at this position, which is not required.

0 (number 0) This position must have a number

9 only numbers can be entered, but not required

# You can enter a number or a positive or negative number for this position, which is not required.

: This character is used to separate hours, minutes, And seconds in time. If the time delimiter set in the control panel is different from this format, it is used.

/This character is used to divide the month, day, and year by date. If the date delimiter set in the control panel is different from this format, it is used.

_ This character automatically generates a space in the input string. when you enter a character, the cursor will skip this position.

This character is used to separate the three parts of the mask rule

In the input mask editor dialog box, there are several other options:

  • Character for blanks edit box -- the third part of the mask has the same meaning
  • Save literal characters check box -- same as the second part of the mask rule
  • Test input edit box -- you can enter data in it to test whether your mask rule is correct or not.
  • Sample masks -- provides examples of common mask rules. You can choose to use
  • Masks button-click to open the "" dialog box. You can load a mask rule (*. Dem) file.

The contents of DEM files in Delphi are as follows:

Phone | 4155551212 | !\(999\)000-0000;1;_Extension | 15450 | !99999;1;_Social Security | 555555555 | 000\-00\-0000;1;_Short Zip Code | 90504 | 00000;1;_Long Zip Code | 905040000 | 00000\-9999;1;_Date | 062794 | !99/99/00;1;_Long Time | 090515PM | !90:00:00>LL;1;_Short Time | 1345 | !90:00;1;_
Important Methods

The gettextlen function is used to return the number of characters entered by the user before formatting:

function GetTextLen: Integer;
3. Memo Components

Memo is a multi-line text editor capable of displaying and editing. tmemo is inherited by tcustommemo, and tcustommemo is inherited from tcustomedit, therefore, the attributes, methods, and events of the memo control are roughly the same as those of the Edit Control.

Main attributes of the custommemo object
  • Alignment -- alignment of text. We have discussed three options: taleftjustify center left, tacenter center, and tarightjustify center right.
  • Caretpos -- this read-only attribute returns the "^" position in the editor, that is, coordinates.
  • Lines -- we can still use the text attribute to access the characters in the edit box, but the line attribute is used to access the text of a line.
  • Scrollbars -- specifies the scroll bar position of the editing box, which can be ssnone, sshorizontal, ssvertical, or ssboth.
  • Wantreturns -- Can I press enter to wrap a line in the editing box?
  • Wanttabs -- whether to use the tab key to move the cursor to a tab in the editing box

The lines attribute is a typical tstrings object. We can use various attribute methods of this object to customize each line of memo content as needed, such:

Memo1.lines. Add ('This is an example of adding a row using the add method of the strings object ');

Alternatively, we can read the content of a text file and add it to the memo editor.

procedure TForm1.Button1Click(Sender: TObject);begin  memo1.Lines.Clear;  memo1.Lines.LoadFromFile('C:\Boot.ini');end;

Assume that there is a button on the form and the default attribute of the button is true. When you enter a button in the memo object, when you press the Enter key, is it a line break or a default button clicked? This requires the wantreturns attribute. If this attribute is true, carriage return is equivalent to line feed. If it is false, carriage return is equivalent to clicking the default button. To wrap a line, you must use the combination key "Ctrl + enter ".

Similarly, if the wanttabs attribute is true, you press the tab key to move the cursor to a tab in the editor instead of switching the input focus on the window control of the form. When this attribute is set to false, you must press Ctrl + TAB to move the cursor to a tab ".


4. RichEdit component

The RichEdit object is located on the Win32 component panel. It is very similar to a memo object and can edit multiple lines of text. However, the text in the memo editor can only be in one format, while the text in the RichEdit object can contain multiple fonts and colors.

The common attributes of a RichEdit object are similar to those of a memo object. The following lists some unique attributes inherited from a tcustomrichedit object.

Common attributes of a RichEdit object
  • Defattributes -- default attribute of text (visible during runtime)
  • Defaultconverter -- specify the conversion class called when the file with an unknown extension is read into the text and the format is converted.
  • Hidescrollbars-whether to hide the scroll bar when the text length is short and does not need to be rolled
  • Pagerect -- specifies the area of the paper in pixels when the RichEdit control is printed.
  • Paragraph -- specify the format of the current segment
  • Plaintext -- specifies whether to read, write, or write text to a file in plain or complex text format.
  • Selattributes -- attributes of the selected text (visible during runtime)
About defattributes attributes and selattributes attributes:

The selattributes attribute of the RichEdit object can be used to specify the color, Font, and other formats of the selected text. After selecting a region using the selstart and sellength attributes, you can use the selattributes attributes to set the format of the selected text. Selattributes belongs to the ttextattributes class. The object of this class can specify the font, color, font size, and font type of the text. The selattributes attribute can be used to obtain the font and color of tfont and tcolor objects through the assign method. The tfont and tcolor objects can also use the assign method to obtain the font and color of selattributes.

The defattributes attribute refers to the default attribute of the RichEdit component, which is identical to the selattributes attribute.

Use the paragraph attribute to obtain the tparaattributes object that the RichEdit control uses to specify the paragraph format information. The Section format contains information such as alignment, indentation, numbering, and tabs.

The paragraph attribute is a read-only attribute, because a customrichedit object can contain only one unchanged paraattributes object. However, by setting the attributes of the paraattributes object, we can change the format of the current paragraph. You can set an attribute or read it from an existing paraattributes object through the Assign Method of the paragraph attribute. The current paragraph refers to a paragraph that contains the selected text. If no selected text exists, the current paragraph refers to the paragraph where the cursor is located.

Main events:
  • Onprotectchange -- starts when the user tries to modify the text whose attribute is protected. The startpos and endpos parameters indicate the text range of the modification design. If modification is allowed, you can set the allowchange attribute to true (use the selattributes attribute to set or cancel the protected attribute of the text)
  • Onresizerequest-this event is triggered when RichEdit needs to be adjusted to an hour when the text changes. The rect parameter specifies the border range required for displaying all text at the same time.
  • Onsaveclipboard -- this event is triggered when the RichEdit object is about to be released. The numobjects parameter and numchars specify the number of objects and characters in the clipboard. Setting the saveclipboard parameter to false can also clear information related to the RichEdit object on the clipboard.
  • Onselectionchange -- this event is triggered when the user changes the selection area with the mouse or keyboard or when the selection area changes by setting the seltext attribute.
The main methods are as follows:
  • Clear process -- clear all text content
  • Findtext function-use the findtext method to search for the text specified by the searchstr parameter in the range of length parameter from the position of startpos In the RichEdit control. The options parameter specifies the search method: Is full match? Case Sensitive? Return Value findtext returns the character location found. If no matching text is found, the function returns-1. Using the findtext function, we can avoid the trouble of coding the complex text feature in programming search.
  • Getseltextbuf -- copy the selected text to the buffer location specified by the buffer parameter, and return the actual number of copied bytes.
  • Print -- format and print the text in the RichEdit control.


5. tspinedit component

The spinedit component on the samples component panel, which is used to enter numbers. You can also click the up or down arrow to increase or decrease the number. The common methods, events, and edit box components of the digital editing box are basically the same. Common attributes are as follows:

Common attributes of the spinedit component
  • Increment -- specifies the Increase (decrease) of the number when the button is clicked each time)
  • Maxlength -- specifies the maximum number of digits. If it is 0, it indicates no limit.
  • Maxvalue -- specifies the maximum value of a number.
  • Minvalue -- specifies the minimum value of a number.

For all the methods for editing each component, see Delphi online help.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.