如何让Edit控件只能接受数字和小数点,并且小数点只能有一个?if not (key in['0'..'9','.',#8]) then
  key:=#0
else if key in['.'] then
begin
    {这段代码如何写呢?}
end;

解决方案 »

  1.   

    procedure JudgeNumber(Edit: TEdit; Kind: String; Key: Char);
    begin
        if Kind = '整数' then
        begin
            if Key in ['0'..'9',#13,#8] = False then
            begin
                Application.MessageBox('只能输入整数! ','系统提示',MB_OK+MB_ICONWARNING);
                Edit.SetFocus;
                Abort;
            end;
        end
        else if Kind = '小数' then
        begin
            if (Trim(Edit.Text) = '')and(Key = '.') then
            begin
                Application.MessageBox('小数格式输入有误! ','系统提示',MB_OK+MB_ICONWARNING);
                Edit.SetFocus;
                Abort;
            end;
            if (Pos('.',Trim(Edit.Text)) > 0)and(Key = '.') then
            begin
                Application.MessageBox('小数格式输入有误! ','系统提示',MB_OK+MB_ICONWARNING);
                Edit.SetFocus;
                Abort;
            end;
            if Key in ['0'..'9','.',#13,#8] = False then
            begin
                Application.MessageBox('只能输入小数! ','系统提示',MB_OK+MB_ICONWARNING);
                Edit.SetFocus;
                Abort;
            end;
        end;
    end;
      

  2.   

    Try
      Edit1.Text:=FormatFloat('0.0',StrToFloat(Edit1.Text));
    Except
      Application.MessageBox('请正确输入数据','提示',Mb_OK);
    End;
      

  3.   

    用 MaskEdit
    控件就可以了.在 Additional 组件页内
      

  4.   

    先设一个标志,表示是否出现过小数点
    var PointFlag:Boolean;
    begin
    PointFlag:=false;
    ...
    else if key in ['.'] then
     if PointFlag then key:=#0
     else PointFlag:=true;其中PointFlag也可以设置为Integer,用0或1表示是否出现了小数点
      

  5.   

    //加在procedure TForm1.edit1Exit(Sender: TObject)事件中
    for i:=1 to length(trim(edt_sjje.Text)) do
    begin
       str:=midstr(trim(edt_sjje.Text),i,1);
       if(str='.')or((str>='0')and(str<='9'))then
       begin
           ffbz:=true;
       end
       else begin
       showmessage('你输入了非法字符,请重新输入。');
        ffbz:=false;
        break;
        end;
    end;
    if ffbz then
    begin
    //在这里无须其他只需要转换一下strtofloat(edit1.text)
    end;