手机站
网通分站
电信主站
密 码:
用户名:
当前位置 : 主页>程序设计>delphi>列表

限制并方便用户输入

来源:互联网 作者:西部数码 时间:2008-04-09
西部数码-全国虚拟主机10强!40余项虚拟主机管理功能,全国领先!双线多线虚拟主机南北访问畅通无阻!免费赠送企业邮局,.CN域名,自助建站480元起,免费试用7天,满意再付款! P4主机租用799元/月.月付免压金!
限制并方便用户输入(2002/12/03 三金 版权所有)


防止用户误输入是软件开发的一项必不可少的工作,除才之外,还要为用户
的使用提供最大方便。当然,我们可以利用或开发新的组件,以完成这些功能。
但是,在团队开发中,每个成员都用自己认为不错的组件开发自己所承担的模
块,会给软件的后期维护带来麻烦。交工的时候,项目负责人可不买你的帐。如
果你用函数调用来完成这些功能,老盖也管不着。下面就是针对常用delphi组件
的限制用户输入函数,但愿网友们能用的上。
(一)TEdit、TDBEdit、TComboBox、TDBComboBox的输入
分三种类型限制:
(1)任意输入
(2)整数输入
(3)浮点数输入
限制的项目如下:
(1)整数输入只能输入数字0-9、 、-
(2)浮点输入只能输入数字0-9、 、-、.
(3) 和-只能有其一,并且只能出现在最前面
(4).只能有一个
(5)限制小数位数
函数如下:
procedure MxFormatKeyPress(Text:string;SelStart,SelLength:integer;
var Key:Char;EditType:integer;Digits:integer);
begin
if (Key=#27) or (Key=#8) or (EditType=1) then exit;
if EditType=2 then
if not (Key in [''''0''''..''''9'''','''' '''',''''-''''] ) then Key:=#0;
if EditType=3 then
if not (Key in [''''0''''..''''9'''','''' '''',''''-'''',''''.''''] ) then Key:=#0;
//控制 -
if (Key =''''-'''') or (Key='''' '''' ) then begin
if ((Pos(''''-'''',Text) > 0) or (Pos('''' '''',Text) > 0 )) and
(SelLength=0 ) then Key:=#0;
if SelStart > 0 then Key:=#0;
end;
//控制.
if (Key = ''''.'''') and (EditType=3 ) then begin
if (Pos(''''.'''',Text) > 0) and (not((SelStart=Pos(''''.'''',Text) ))) then Key:=#0;
if SelStart=0 then Key:=#0;
if (Digits>0) and (SelStart SelLength0) and (EditType=3) then
if (pos(''''.'''',Text )>0 ) and (SelStart>=pos(''''.'''',Text)) then
if length(Text)-pos(''''.'''',Text )>=Digits then Key:=#0;
end;

此函数在所限制组件的OnKeyPress事件中调用。Key即为OnKeyPress携带的
Key:Char参数;EditType为限制的类型:1-任意输入;2-整数输入;3-浮点输入;
Digits为浮点数输入时小数的位数,如果是零,则可输入任意位数。另外,此
函数只适用于有Text、SelStart、SelLength等属性的TWinControl类的派生类。
具体限制各组件的二级函数如下:

限制TEdit、TDBEdit:
procedure MxFormatEditKeyPress(Edit:TCustomEdit;var Key:Char;EditType:integer;
Digits:integer);
begin
MxFormatKeyPress(Edit.Text,Edit.SelStart,Edit.SelLength,Key,EditType,Digits);
end;

限制TComboBox:
procedure MxFormatComboKeyPress(Combo:TComboBox;var Key:Char;EditType:integer;
Digits:integer);
begin
MxFormatKeyPress(Combo.Text,Combo.SelStart,Combo.SelLength,Key,EditType,Digits);
end;

限制TDBComboBox:
procedure MxFormatDBComboKeyPress(Combo:TDBComboBox;var Key:Char;
EditType:integer;Digits:integer);
begin
MxFormatKeyPress(Combo.Text,Combo.SelStart,Combo.SelLength,Key,EditType,Digits);
end;

调用示例:
假如Form1上有一ComboBox1,让用户只输入浮点数,并且小数位数为两位。则
可以在ComboBox1的OnKeyPress事件中调用上面的函数,代码如下:
procedure TForm1.ComboBox1KeyPress(Sender: TObject; var Key: Char);
begin
MxFormatComboKeyPress(Combobox1,Key,3,0);
end;

如果你的窗体上有多各TComboBox,并且限制类型一致,则不必每个TComboBox都
书写代码,只需为其中一个编写事件处理代码,其它作连接即可。
procedure TForm1.ComboBox1KeyPress(Sender: TObject; var Key: Char);
begin
MxFormatComboKeyPress(Sender as TComboBox,Key,3,0);
end;
其它组件调用方法同上。

(二)时间的输入
限制类型:
(1)时分
(2)时分秒
组件采用TMaskEdit,数据敏感采用TDBEdit。
限制项目如下:
(1)小时只能输入0-23
(2)分钟不超过59
(3)秒不超过59
(4)用户只能全删,而不能只删某一位数据
(5)箭头键可以更改时间
需要在组件的OnKeyPress和OnKeyDown事件中分别书写代码。

procedure MxFormatTimeKeyPress(ctl:TCustomMaskEdit;TimeFormat:integer;
var Key:Char;dts:TDataSource);
var
TextSave:string;
EditingPos:integer;//1-h 2-m 3-s
i:integer;
NumChars:set of Char;
SelStartSave,SelLengthSave:integer;
CharValid:boolean;
begin
NumChars:=[''''0''''..''''9''''];
if Key=^V then Key:=#0;
if not (Key in NumChars ) then exit;
TextSave:=ctl.Text;
SelStartSave:=ctl.SelStart;
SelLengthSave:=ctl.SelLength;
case ctl.SelStart of
0,1: EditingPos:=1;
3,4: EditingPos:=2;
6,7: EditingPos:=3;
else EditingPos:=0;
end;
///////////////////////////////////////
CharValid:=true;
case EditingPos of
1: begin
if SelStartSave=0 then begin
if not (Key in [''''0''''..''''2'''']) then CharValid := False;
if (Key =''''2'''' ) and (TextSave[2] in [''''4''''..''''9'''']) then
CharValid:=false;
end;
if (SelStartSave = 1) and (TextSave[1] = ''''2'''') and

文章整理:西部数码--专业提供域名注册虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!