【摘自网上】Delphi中MaskEdit的用法

 

掩码编辑框(MaskEdit)是一种特殊的编辑框。使用它可以设计出许多复杂的输入格式。在选用MaskEdit之后,需要为它设计一个输入格式。方法如下:

 

首先在窗体上将MaskEdit选中,在对象查看器(Object Inspector)中找到EditMask属性,双击它,弹出一个如下图所示的对话框。

 

对话框左上角的Input Mask编辑框用于输入掩码格式。掩码格式可以分为三部分,每个部分之间用分号“;”分开,如“!90:90:90:;1;_ ”。下面解释各输入格式符的意义:

 

1. 输入格式的第一部分是掩码本身,也就是数据输入格式。在这一部分用一些特殊的格式符来表示应输入的字符类型及格式,如“!l0A9:C>ll<”这样一串输入格式掩码。常用的MaskEdit输入格式符有:

 

格式符意义

 

L和l L表示该位置只能填一个字母。l和L一样,但l允许不填。

 

A和a A表示该位置只能填一个字母或数字。a和A一样,但a允许不填。

 

C和c C表示该位置可以填任意字符。c和C一样,但c允许不填。

 

0和9 0表示该位置只能填一个数字。9和0一样,但9允许不填。

 

<和> <格式符表示随后的字母均以小写的形式显示,直到遇到一个>格式字符或<>格式符。>格式符则和<正好相反。

 

<> 表示不作大小写转换,以输入时的形式为准。

 

/ 表示该格式符之后的那个掩码格式符将作为数据中的普通字符对待。当用户想在数据中放进一个普通字符,而不希望作为掩码格式符被解析时,就可以在这个字符前放一个/符号。如/A,//等等。

 

# 表示该位置可以填一个数字或正负号,或什么都不填。

 

! !格式符只能放在掩码格式符的第一个字符处,表示输入的数据的前导空格将被去除(即读取EditMask控件值的时候将没有这些空格),没有!则数据后的空格将被去除。

 

;和/ 标准的分隔符,可作为数据的一部分。其中/在显示时为‘-’而非‘/’。

 

2. 在输入格式中的第二部分只有0和1两种选择。如为1,则掩码中的非用户输入数据和标准分隔符等其它各种字符会作为数据的一部分保存;为0则不保存。

 

3. 输入格式的第三部分用于表示数据中的空位用哪个字符代替显示。

 

在有了上述知识后,现在来设计一个例子。例如要输入一个汽车牌号如CA-300019 。该数据前两位为字母且为大写,第三位为一个系统分隔符号,后6位为数字。可以这样设计掩码格式:

!>LL/000000;1;_

掩码的第二部分取为1表示系统分隔符‘-’也将作为数据保存。第三部分的“_”表示在掩码编辑框中为空时,用“_”来表示空位。我们也可以用空格来标识空位。

又如一个邮编如PO-100083,其中PO表示邮政,是每个邮政编码中都有的前缀,可以这样设计掩码格式:

!PO/000000;1;_

在掩码设计对话框的右侧是Sample Masks,这里有系统预设的常用的掩码格式,如电话号码(Phone)、日期(Date)等。我们可以选择一个,然后进行必要的调整。对话框左下部是一个Test Input栏,我们可以在这里输入字符,以检测输入格式是否令人满意。 

 

 

EditMask主要是用来设置掩码(Mask)的指定子符,换言之,我们可以利用MaskEdit对象所提供的EditMask属性强迫使用者输入合法的字符   

 

你只要点击EditMask属性右面的按钮就可以打开Input   Mask   Editor对话框,你可以在右面的Sample   Masks中点击来选择一个预设的格式,也可以在Input   Mask中直接输入掩码子符串,左下角的输入框是用来对你在左上脚定义的掩码格式做输入测试的,它也是最终显示在MaskEdit1中的格式。   

 

至于掩码的格式子符有许多种,在这里一一例举不太可能,我只能给你几个常用的   

#   其所对应的位置只能输入数字或正负号,但不一定要输入   

9   其所对应的位置只能输入数字,但不一定要输入   

0   其所对应的位置只能输入数字,一定要输入   

c   其所对应的位置只能输入子符,但不一定要输入   

C   其所对应的位置只能输入子符,一定要输入   

!   假如!子符出现在TMaskEdit掩码子符中,则"!"子符前的空格不会被存为数据   

/   通常用来分隔日期中的年月日等数据   

_   _处自动插入一个空格   

;   用来分隔各个部分   

 

比如你所说的   

2002-12-5   就可以写为   !0000/00/0;1;   或   0000/00/0;1;     

其中最后一个1表示掩码子符会保存在数据中,比如说‘/',如果为0就表示不保存   

;用来分隔各部分,0表示必须输入一个数字,/表示分隔符'-',

 

Character Meaning in a Mask

 

!      If a ! character appears in the mask, optional characters are represented in the text as leading blanks. If a ! character is not present, optional characters are represented in the text as trailing blanks.

 >      If a > character appears in the mask, all characters that follow are in uppercase until the end of the mask or until a < character is encountered.

 <      If a < character appears in the mask, all characters that follow are in lowercase until the end of the mask or until a > character is encountered.

 <>     If these two characters appear together in a mask, no case checking is done and the data is formatted with the case the user uses to enter the data.

/       The character that follows a / character is a literal character. Use this character to use any of the mask special characters as a literal in the data.

 L      The L character requires an alphabetic character only in this position. For the US, this is A-Z, a-z.

 l      The l character permits only an alphabetic character in this position, but doesn't require it.

A       The A character requires an alphanumeric character only in this position. For the US, this is A-Z, a-z, 0-9.

 a      The a character permits an alphanumeric character in this position, but doesn't require it.

C       The C character requires an arbitrary character in this position.

 c      The c character permits an arbitrary character in this position, but doesn't require it.

 0      The 0 character requires a numeric character only in this position.

 9      The 9 character permits a numeric character in this position, but doesn't require it.

#       The # character permits a numeric character or a plus or minus sign in this position, but doesn't require it.

:       The : character is used to separate hours, minutes, and seconds in times. If the character that separates hours, minutes, and seconds is different in the regional settings of the Control Panel utility on your computer system, that character is used instead.

 /      The / character is used to separate months, days, and years in dates. If the character that separates months, days, and years is different in the regional settings of the Control Panel utility on your computer system, that character is used instead.

 ;      The ; character is used to separate the three fields of the mask.

 _      The _ (underscore) character automatically inserts spaces into the text. When the user enters characters in the field, the cursor skips the _ character.

Any character that does not appear in the preceding table can appear in the first part of the mask as a literal character. Literal characters must be matched exactly in the edit control. They are inserted automatically, and the cursor skips over them during editing. The special mask characters can also appear as literal characters if preceded by a backslash character (/).

The second field of the mask is a single character that indicates whether literal characters from the mask should be included as part of the text for the edit control. For example, the mask for a telephone number with area code could be the following string:

(000)_000-0000;0;*

The 0 in the second field indicates that the Text property for the edit control would consist of the 10 digits that were entered, rather than the 14 characters that make up the telephone number as it appears in the edit control.

A 0 in the second field indicates that literals should not be included, any other character indicates that they should be included. The character that indicates whether literals should be included can be changed in the Edit Mask property editor, or programmatically by changing the MaskNoSave typed constant.

The third field of the mask is the character that appears in the edit control for blanks (characters that have not been entered). By default, this is the same as the character that stands for literal spaces. The two characters appear the same in an edit window. However, when a user edits the text in a masked edit control, the cursor selects each blank character in turn, and skips over the space character.

 

Note:  

When working with multibyte character sets, such as Japanese Shift-JIS, each special mask character represents a single byte. To specify multi-byte characters using the L, l, A, a, C, or c specifiers, the mask characters must be duplicated as well. For example, LL would represent two single-byte alphabetic characters or a one double-byte character. Only single-byte literal characters are supported.

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值