问题同题目

解决方案 »

  1.   

    //只能输入数字   
      procedure   TForm1.Edit1KeyDown(Sender:   TObject;   var   Key:   Word;   
          Shift:   TShiftState);   
      begin   
          if   Key<'0'   or   Key>'9'   then   Key=#0;   
      end;
      

  2.   

    procedure   TForm1.Edit1KeyDown(Sender:   TObject;   var   Key:   Word;   
          Shift:   TShiftState);   
      begin   
          if   Key<'0'   or   Key>'9'   then   Key=#0;   
      end;===================================
    这样只能输入整数,且输入错误后不能删除
      

  3.   

    if not (Key in ['0'..'9','.' ,#8, #13]) then
          Key := #0;
    ==================================================
    这是我用的方法,但不能限制输入这样的情况 例如:15...
    这样就会出错
      

  4.   

    procedure Edit1KeyPress(Sender: TObject; var Key: Char);
    begin
      while True do
        begin
          if Key=#13 then
            Exit;
          if Key=#8 then
            Exit;
          if (Pos('-',TEdit(Sender).Text)>0) and (Pos('-',TEdit(Sender).SelText)=0) and (TEdit(Sender).SelStart=0) then
            break;
          if Key  in [('-'),('.'),'0'..'9']  then
            begin
              if (Key=('-')) and (((Pos('-',TEdit(Sender).Text)>0) and (Pos('-',TEdit(Sender).SelText)=0)) or (TEdit(Sender).SelStart<>0))then
                break;
              if (Key=('.')) and (Pos('.',TEdit(Sender).Text)>0) then
                if (Pos('.',TEdit(Sender).SelText)=0) then
                  break;
              Exit;
            end;
          break;
        end;
      Key:=#0;
    end;
      

  5.   

    unsigned(僵哥(发站内消息,请附上链接或问题说明,否则不予回) 
    谢谢你的回复,你的方法我测试通过了。
      

  6.   

    靠,搞那么复杂,不如在代码里面判断。
    因为如果用户粘贴你还不是死定?并且粘贴有三种(至少我知道有三种),你不会也一一搞定吧。
    在做其它之前
    if StrToIntDef(UserInput, YourDefaultValue) = YourDefaultValue then
    begin
      // AskUserToInputAgain
    end;
      

  7.   

    to swayi21:
    一般用个弹出菜单把edit的粘贴屏蔽,加上代码屏蔽了Ctrl+V 就ok了,所以基本上没什么问题,你说的3种我就想不起第三种是什么。
      

  8.   

    好了,求同存异。
    第三种:Shift+Insert
      

  9.   

    僵个有理,不过在使用前判断是否可转化为数值是必要的。
    procedure   TForm1.Edit1KeyDown(Sender:   TObject;   var   Key:   Word;   
          Shift:   TShiftState);   
      begin   
        if (key=#$8) or (key=#$9) or (key=#$D) or (key=#$A) then exit;
        //如果需要小数点 或符号,分两步只为了方便阅读
        if (key='.') or (key='+') or (key='-') then exit;
        if (Key<'0'   or   Key>'9') and (key<>'.')   then   Key=#0;  
      end;在使用时
    var intval:integer;
    begin
      if not trystrtoint(edit1.text,intval) then
      begin
        //格式不对提示
        exit;
      end;
      //......
    end;另外,delphi自带maskedit控件可以完成一半输入格式限制,但不灵活,并且不能很好处理中文输入。
      

  10.   

    在同一窗体,所有edit控件可以共享EditKeyDown处理过程
      

  11.   

    最好是直接判断textbox文本把 他拆成一个个字符判断
    public static bool IsNumeric(string str)
    {
    if (str == null || str.Length == 0)
    return false;
    System.Text.ASCIIEncoding ascii = new                 System.Text.ASCIIEncoding();

    byte[] bytestr = ascii.GetBytes(str);
    for  (int i=0;i<bytestr.length;i++)
                           {
                                 if ( bytestr[0]= 51)
                                  {  return false;}
                                 if  (bytestr[i] < 46 || (bytest[i]> 46  &&             bytestr[i] <48 )||c>57)
    {
    return false;
    }
                         if (bytestr[bytestr.length ] =51)
                           {return false;
    }
                          return true;

    另外 对 字符 .进行判断,头字符.的 return false
    尾字符.的返回false
      

  12.   

    VB里可以这样写:懂Delphi的给改一下:Private Sub Text1_KeyPress(KeyAscii As Integer)
        '只能输入一个小数点
        If KeyAscii = 46 And InStr(1, Text1.Text, ".", vbTextCompare) Then KeyAscii = 0
        '只能输入数字和小数点,但可以使用退格键
        If (KeyAscii < 48 Or KeyAscii > 57) And KeyAscii <> 46 And KeyAscii <> 8 Then KeyAscii = 0
        '控制"0"的个数
        If Left(Text1, 1) = "0" And Len(Text1) < 2 And KeyAscii = 48 Then KeyAscii = 0End Sub