VCL Controls: The Masked Edit Control |
|
Introduction to the Masked Edit Control |
Description |
The masked edit is a special edit control that provides more control over text entered or displaying in an edit box. The biggest difference between the edit and the masked edit controls is the masking possibilities available on the latter. |
To support the masked edit control, the VCL provides the TMaskEdit class. This class is derived from TCustomMaskEdit, which is based on the TCustomEdit class:
To add a masked edit control to your form, from the Additional tab of the Tool Palette, click the TMaskEdit button and click on the form. To dynamically create a masked edit objec, declarea variable of type TMaskEdit. Using the new operator, specify the owner of the control. You must also specify the parent of the variable. Here is an example: //--------------------------------------------------------------------------- void __fastcall TForm1::Button1Click(TObject *Sender) { TMaskEdit *Mascot = new TMaskEdit(Form1); Mascot->Parent = Form1; } //--------------------------------------------------------------------------- A masked edit control is created like theclassic edit control. If you want to make a true masked edit object, set its properties as needed.
|
Characteristics of the Masked Edit Control |
Editing the Mask |
The most important property of a masked edit control, which sets it apart from the (traditional) edit control, is its ability to control what the user can and cannot enter in the text side. To configure this text, the TMaskEdit class is equipped with a property named EditMask. To visually set it, in the Object Inspector, click the EditMask field to reveal its ellipsis button. You have two options: |
Create a new file following the example. Each mask is typed on its own line and press Enter at the end of each mask. To save the file, locate the C:\Program Files (x86)\Embarcadero\RAD Studio\7.0\bin folder. In the Save dialog box, change the Save As Type to All Files. Type a name in one word followed by an extension .dem extension. To use your list of masks, invoke the Input Mask Editor, click Masks… Locate the C:\Program Files (x86)\Embarcadero\RAD Studio\7.0\bin folder. Change the Files of Types to All files. Click the file you created and click Open. You can also set a mask programmatically using the symbols appropriate for the masks. Here is an example: //--------------------------------------------------------------------------- void __fastcall TForm1::FormCreate(TObject *Sender) { // Mask for an 8 character file name + 3-character extension // The first character is automatically converted to uppercase // After the first character, the user can enter an alphabetical // character or a digit to complete the 8 characters. // The other 7 characters and the extensions are converted to lowercase edtFileName->EditMask = ">L<aaaaaaa.<LLL;1;_"; } //---------------------------------------------------------------------------
As a text-based control, the content of the MaskEdit control is represented by the Text property, which is a UnicodeString string. Following the EditMask you had set in the Input Mask Editor editor, you can use a default text that would display when the control opens. To set this text, on the Object inspector, click Text and type a value that abides by the rules of the EditText field. At design time, C++Builder will assist you and make sure that the value you type is appropriate. At runtime also, the user will have to follow the rules of the mask. When a mask is configured for a masked edit control, the compiler reinforces the rule to make sure the user would follow number and types of characters allowed in the edit box. If you add a masked edit control but do not apply a mask to it, you can limit the number of characters that the user can enter in the box. The maximum number of characters allowed is set using the MaxLength property. This property has any effect only if no mask is applied to the control. At design time, type an integer value for the property. At runtime, assign an appropriate value to the control.
The IsMasked Boolean property can be used to check whether a masked edit control has been configured with a mask already.
The main event of the MaskEdit control occurs when the content of the control has been altered. The compiler takes care of validating the characters and symbols while the user is editing the edit box. When the user has finished and presses Enter or Tab to possibly shift the focus to another control, a notification is sent to the operating system. If the value entered in the control does not conform to the EditMask property, an error is thrown and the application may stop responding. For this reason, you should use the MaskEdit control only when appropriately needed; or you should write an event handler or function that would deal with errors of this control. |
|
|||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||
Home | Copyright © 2010-2016, FunctionX | |
|