怎样在EDIT的ONPRESSKEY中编程实现只允许输入数字型数据!能把详细代码列出吗?谢谢!

解决方案 »

  1.   

    ONKEYPRESS
    begin
      if not (key in ['0'..'9','.',#8,#13]) then
        key := #0;
    end;
      

  2.   

    procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
    begin
      if (Key<'0') or (Key>'9') then
        Key:=chr(0);
    end;
      

  3.   

    if not (key in ['0'..'9',#8,#13,#46]) then key:=#0 ;
      

  4.   

    在KeyPress事件中
    begin
      if not (key in ['0'..'9','.',#8,#13]) then
        abort;
    end;
      

  5.   

    1.只能输入数据
    if (Key<'0') and  (Key>'9')  and (Key<>'-') and (Key<>'.') then  Key:= #0;
    if( (Key='-') and ( (Length(TEdit(Sender).Text)>0) ))then Key:=#0;
    if((Key='.') and ( (Length(TEdit(Sender).Text)>0)  and  ( Pos(TEdit(Sender).Text,'.')>0) )) then Key:=#0;
    2.禁止输入全角字符
     If (Key=#163) then
       Begin
         Key:=#0;
         showmessage('不能输入全角字符,将输入法改为半角');
       end;
     If (Key=#161) then
       Begin
         Key:=#0;
       end;
     If (Key=#170) then
       Begin
         Key:=#0;
       end;
    3.函数实现
    Function PLstrtoInt(ss:string):Int64;
    var
      plst,pls,plss,plsss:string;
      NOdian,NOjianhao,NOjiahao:boolean;
      plf:Int64;
      i:integer;
    begin
      plss:='';
      NOdian:=true;
      NOjianhao:=true;
      NOjiahao:=true;  pls:=trim(ss);
      for i:=1 to length(pls) do
        begin
          plst:=copy(pls,i,1);
             if (plst='0') or (plst='1') or (plst='2') or
                (plst='3') or (plst='4') or (plst='5') or
                (plst='6') or (plst='7') or (plst='8') or
                (plst='9') or
                ((plst='.') and Nodian) or
                ((plst='+')  and  Nojiahao) or
                ((plst='-')   and  Nojianhao)
                 then plss:=plss+plst;
                 if (plst='.') then Nodian    :=false;
                 if (plst='+') then Nojiahao  :=false;
                 if (plst='-') then Nojianhao :=false;
        end;
      Plsss:=copy(plss,1,1);
      plst:='';
      if (length(plss)>=2) then
        begin
          for i:=2 to length(plss) do
            Begin
              plst:=copy(plss,i,1);
              if (plst<>'+') and (plst<>'-') then plsss:=plsss+plst;
            End;
          plf:=strtoInt(plsss);
        end
      else
         begin
          if (plss='-') Or (plss='+') Or (plss='') then plf:=0
          else plf:=strtoInt(plss);
         end;
    result:=plf;
    end;
    4.生成可以为SQL用的语句
    Function PLstrtoNotFuHao(ss:string):String;
    var
    pls:string;
    begin
      pls:=trim(ss);
      while Pos(' ', plS) > 0 do
        plS[Pos(' ', plS)] := '0';
      while Pos('''', plS) > 0 do
        plS[Pos('''', plS)] := '0';
      while Pos(':', plS) > 0 do
        plS[Pos(':', plS)] := '0';
      while Pos(';', plS) > 0 do
        plS[Pos(';', plS)] := '0';
      while Pos('"', plS) > 0 do
        plS[Pos('"', plS)] := '0';
      result:=pls;
    end;
      

  6.   

    以上都对吧!但是在FORM的 KEYPRESS ;=TRUE