转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 软件开发 >> Delphi程序 >> 正文
限制并方便用户输入         ★★★★

限制并方便用户输入

作者:闵涛 文章来源:闵涛的学习笔记 点击数:1648 更新时间:2009/4/23 18:39:06
限制并方便用户输入(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
              (not (Key in [''''0''''..''''3''''])) then CharValid := False;
    end;
    2: if (SelStartSave = 3) and not (Key in [''''0''''..''''5'''']) then CharValid := False;
    3: if (SelStartSave = 6) and not (Key in [''''0''''..''''5'''']) then CharValid := False;
  end;
  if not CharValid then begin
    Key:=#0;exit;
  end;
  if dts<>nil then dts.DataSet.Edit;
  if not (SelStartSave in [2,5]) then TextSave[SelStartSave+1]:=Key;
  if SelLengthSave>1 then begin
    for i:=SelStartSave+2 to SelStartSave+SelLengthSave do
      if i in [1,2,4,5,7,8] then TextSave[i]:=''''0'''';
    SelLengthSave:=1;
  end;
  for i:=1 to length(TextSave) do
    if (i in [1,2,4,5,7,8]) and (not (TextSave[i] in NumChars ) ) then
      TextSave[i]:=''''0'''';
  ////////////////////////////////////
  if SelStartSave in [1,4] then
    SelStartSave :=SelStartSave+2
  else if SelStartSave=length(TextSave)-1 then
    SelStartSave :=SelStartSave
  else SelStartSave :=SelStartSave+1;
  /////////////////////////////////////
  ctl.Text :=TextSave;
  ctl.SelStart :=SelStartSave;
  ctl.SelLength :=SelLengthSave;
  Key:=#0;
end;

//此函数分割时间,因为有时候会遇到非法的时间字符串,所以不采用DecodeTime。
function MxSplitStr(SourceStr,SplitStr:string;var ResultArray:array of string):integer;
var
  i:integer;
  strTmp:string;
begin
  strTmp:=SourceStr;
  i:=0;
  while pos(SplitStr,strTmp)>0 do begin
    ResultArray[i]:=copy(strTmp,1,pos(SplitStr,strTmp)-1);
    strTmp:=copy(strTmp,pos(SplitStr,strTmp)+length(SplitStr),length(strTmp)-
        pos(SplitStr,strTmp));
    i:=i+1;
  end;
  ResultArray[i]:=strTmp;
  result:=i+1;
end;
//此函数检查字符串是否为合法的时间
function TimeValid(TimeStr:string;TimeFormat:integer):boolean;
var
h,m,s:string;
ary:array[0..2] of string;
SplitRet:integer;
i:integer;
begin
  result:=true;
  SplitRet:=MxSplitStr(TimeStr,'''':'''',ary);
  if SplitRet<2 then begin
    result:=false; exit;
  end;
  for i:=0 to 2 do begin
    if length(ary[i])>2 then begin
      result:=false; exit;
    end;
    ary[i]:=trim(ary[i]);
  end;
  h:=ary[0];m:=ary[1];
  if TimeFormat=3 then s:=ary[2];
  ///////////////////////////////
  if (h='''''''') or (strtoint(h)>23 ) then begin
    result:=false; exit;
  end;
  if (m='''''''' ) or (strtoint(m)>59) then begin
    result:=false; exit;
  end;
  if (TimeFormat=3) then
    if (s='''''''') or (strtoint(s)>59) then begin
      result:=false; exit;
    end;
end;
//此函数对时分秒进行加减运算
function IncTime(ATime: TDateTime; Hours, Minutes, Seconds,
  MSecs: Integer): TDateTime;
begin
  Result := ATime + (Hours div 24) + (((Hours mod 24) * 3600000 +
    Minutes * 60000 + Seconds * 1000 + MSecs) / MSecsPerDay);
  if Result < 0 then Result := Result + 1;
end;
//时分秒加减运算的二级函数
function TimeAdd(TimeStr:string;TimeFormat:integer;
        HStep,MStep,SStep:integer):string;
var
  dt:Tdatetime;
begin
  if not TimeValid(TimeStr,TimeFormat) then
    if TimeFormat=2 then begin result:=''''00:00''''; exit; end
    else begin result:=''''00:00:00'''';exit; end;
  dt:=strtotime(TimeStr);
  if TimeFormat=2 then
    result:=FormatDateTime(''''hh:mm'''',IncTime(dt,HStep,MStep,SStep,0))
  else
    result:=FormatDateTime(''''hh:mm:ss'''',IncTime(dt,HStep,MStep,SStep,0))
end;
//限制组件的OnKeyDown
procedure MxFormatTimeKeyDown(ctl:TCustomMaskEdit;TimeFormat:integer;
        var Key:word;Shift: TShiftState;dts:TDataSource);
var
  TextSave:string;
  SelStartSave,SelLengthSave:integer;
  EditingPos:integer;//1-h 2-m 3-s
  i:integer;
begin
  if (ssShift in Shift) and (Key<>vk_delete ) then exit;
  if not (Key

[1] [2]  下一页


没有相关教程
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · C语言系列  · VB.NET程序
    · JAVA开发  · Delphi程序
    · 脚本语言
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台