• 文本框只能输入数字


    procedure TMainForm.edtListViewIndexKeyPress(Sender: TObject;
    var Key: Char);
    begin
    if not (Key in ['0'..'9', #8]) then
    Key := #0;
    end;


    在ONKEYPRESS里写下
    procedure Tform1.Edit1KeyPress(Sender: TObject; var Key: Char);
    begin
      if key>'9' or key<'0' then abort
    end;


    procedure TSellForm.NumEditKeyPress(Sender: TObject; var Key: Char);
    begin
      if key in ['0'..'9'] then
      else
        Key:=#0;
    end;


    procedure TQueryBilForm.ComPTextKeyPress(Sender: TObject; var Key: Char);
    begin
      if Assigned(CurField)  then
       begin
        if (CurField.DataType in [ftFloat,ftCurrency,ftBCD]) then
          begin
          if not((key in ['0'..'9','.','-',#8])) then
           begin
            key := #0
           end
          else
            begin
              if (Key='.') and (Pos('.',TEdit(Sender).Text)<>0) then
                 Key:=#0
              else if
                 (Key='-') and (Pos('-',TEdit(Sender).Text)<>0) then
                 Key:=#0;
            end;
          end
        else if (CurField.DataType in [ftAutoInc,ftSmallint, ftInteger, ftWord,
                     ftBytes,ftLargeint]) then
           begin
           if not ((Key in ['0'..'9','-',#8])) then
            begin
             Key:=#0
            end
            else
            begin
              if(Key='-') and (Pos('-',TEdit(Sender).Text)<>0) then
                 Key:=#0;
            end;
           end
        else
           Exit;
       end;
    end;

    try
      StrtoFloat(Edit1.Text);
    except
      messagebox(handle,'请输入数字!',mb_ok);
    end;

    Edit 中只输入数字
        SetWindowLong(Edit1.Handle, GWL_STYLE,
                      GetWindowLong(Edit1.Handle, GWL_STYLE) or
                      ES_NUMBER); 

    在edit1 keypress事件中加入以下代码即可:

    procedure TForm1.edit1KeyPress(Sender: TObject; var Key: Char);
    begin
    if not (key in ['0'..'9','.',#13,#8]) then  key:=#0;
    if (key = '.') and (Pos('.', edit1.text) > 0) then   key:= #0;
    end;

    if Not(Key in ['0'..'9'])
    再加两个,
    if Not(Key in ['0'..'9',#8,#13])
    不然按删除或者都会出提示

    procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
    begin
    if Not(Key in ['0'..'9',#8,#13]) then
    begin
    key:=#0; //把输入的字符清空
    ShowMessage('输入不是数字');
    end;
    end;

    Var
    I : Integer;
    Begin
    Try
    I := StrToInt(Edit1.Text);
    Except
    ShowMessage('XXXX');
    end;

    [分享]限制文本框只能输入数字(delphi)
    if not (key in ['.','0'..'9',#8,#13])then key:=#0;               //只能输入数字、小数点和回车、退格
    if (key in ['.'])and (pos('.',Edit1.Text)>0)then key:=#0;     //只能输入一个小数点
    if (key in ['.'])and(length(Edit1.Text)<1)then key:=#0;       //第一位不能为小数点
    if (key in ['0'])and (pos('.',Edit1.Text)<1)and(copy(Edit1.Text,1,1)='0')   then
    key:=#0;

    //------------------------------------------------------------------------------------------
    //只可输入数值
    if key=#8 then
      else if (key<#48) or (key>#57) then
        begin
          application.MessageBox('您输入的不是数值!','SUER个人工作室',mb_ok+mb_iconquestion);
          key:=#0;
        end
    else
    //------------------------------------------------------------------------------------------

  • 相关阅读:
    让x86的android模拟器能模拟arm架构系统
    婴儿补充微量元素
    asterisk 能打电话的配置
    SIP协议错误代码大全
    asterisk错误排查
    asterisk帮助与国内论坛
    win10 只要打开文件对话框就卡死解决方法
    分享到朋友圈实现
    跳转前暂停几秒js如何实现
    Github css加载失败,样式混乱解决办法
  • 原文地址:https://www.cnblogs.com/bingege/p/1621385.html
Copyright © 2020-2023  润新知