zoukankan      html  css  js  c++  java
  • 只能录入数字

    procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
    begin
      case Key of 
         '0'..'9':   ;      //对数字键响应 
         #8,#9,#37,#39,#46,#35,#36:;  //退格、Tab、左箭头、右箭头、Delete、End、Home

         #1,#3,#22,#24:;//对Ctrl C、V、A、X响应
    ...

    对自己程序的修改:

      if not(key in ['0'..'9', #8, #13,#45, #27, '.',#3,#22,#24]) then
        key:=#0;
      if (key='.') and (Pos('.',TEdit(Sender).Text)>0) then
      key:=#0;

    -------------------------------------

    #45 Ins键  #27 Esc  #13 回车
    =============================

    转载:http://www.cnblogs.com/Bubalus/archive/2011/04/01/2013691.html

    原来的代码无法限制只输入一个小数点

    begin
         case Key of
        '0'..'9', #8, #13,#45, #27, '.' : ; //#8退格键,#13回车键
                    //可输入0-9,退格,回车,点以及负号
      else
        begin
          MessageBox(0, '请输入数值!', '输入错误', MB_OK+MB_ICONERROR);
          Key := #0;
        end;
      end;
      end;

    限制只输入一个小数点:

    在keypress事件加入如下代码即可

    if not (key in ['0'..'9','.',#8]) then
        key:=#0;
    if (key='.') and (Pos('.',Edit1.Text)>0)   then
          key:=#0;

    pos('.',edit1.text)得到的是 . 第一次在edit.text中出现的位置  

    因为只要edit1.text中有一个 .     返回值肯定大于0

    上面这段代码只能在edit1中应用,如果想将该事件应用到多个edit上,可做如下修改:(其他事件同理)

    if not (key in ['0'..'9','.',#8]) then
        key:=#0;
    if (key='.') and (Pos('.',TEdit(Sender).Text>0)   then
          key:=#0;

  • 相关阅读:
    无刷新跨域上传图片
    php框架-yii
    nginx-url重写
    linux下挂载移动硬盘ntfs格式
    页面有什么隐藏bug:字体,图片
    Oracle、MySql、SQLServer数据分页查询
    转载:Qt之界面实现技巧
    QT常用资料
    MySQL判断字段值来确定是否插入新记录
    WindowsAPI开发常用资料
  • 原文地址:https://www.cnblogs.com/jshchg/p/2171726.html
Copyright © 2011-2022 走看看