zoukankan      html  css  js  c++  java
  • DELPHI 输入框框限制并方便用户输入《转》

     防止用户误输入是软件开发的一项必不可少的工作,除才之外,还要为用户的使用提供最大方便。当然,我们可以利用或开发新的组件,以完成这些功能。但是,在团队开发中,每个成员都用自己认为不错的组件开发自己所承担的模块,会给软件的后期维护带来麻烦。交工的时候,项目负责人可不买你的帐。如果你用函数调用来完成这些功能,老盖也管不着。下面就是针对常用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+SelLength > 0) and (EditType=3) then
          if (pos('.',Text )>0 ) and (SelStart>=pos('.',Text)) then
            if length(Text)-pos('.',Text )>=Digits then Key:=#0;
          end;
      end;
    //输入限制(0到9,小数点,小数位)
    procedure MxFormatKeyPress(Text:string; var Key:Char; Digits:integer);
    begin
        if not (Key in ['0'..'9','.',#8] ) then Key:=#0;
      //控制.
      if Key = '.' then
        begin
           if Pos('.',Text) > 0 then
             begin
               Key:=#0;
               exit;
             end;
        end;
       if Digits > 0 then
         if pos('.',Text ) > 0 then
           if length(Text)-pos('.',Text ) >= Digits then
             if key <> #8 then
               Key:=#0;
    end;
    
    //MxFormatKeyPress(sEdit1.text,key,4);

    此函数在所限制组件的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;
    其它组件调用方法同上。

  • 相关阅读:
    数据库入门
    小米笔试题
    苏宁笔试:UML类图中的关系
    动态规划
    快手笔试题
    二叉树与双向链表的转换
    招银网络科技笔试题
    map的运用
    string类型的常用方法
    string类型和int类型之间的转换
  • 原文地址:https://www.cnblogs.com/LceMeaning/p/2852969.html
Copyright © 2011-2022 走看看