OnEdit1Exit 事件中加以下代码:Try
     StrToFloat(Edit1.Text);
Except
     On EConvertError do 
     begin
          Edit1.Text:='0';
          Edit1.SetFocus;
     end;
end;如果你用的数据表,还是用 DBEdit 吧,不用这么麻烦!

解决方案 »

  1.   

    在输入是控制
    onkeypress
     if not (key in ['0'..'9']) then
       begin
          edit1.text:='0';
          edit1.setfocus;
       end;
      

  2.   

    如果是输入一个错误的字母就将EDIT全部清0,未免太你得允许别人犯一次错误嘛。如果一次错误就全部否定,那么这个程序还是不灵活的。解决的办法是如果输入的不是数字,那么就将这个键废除不就行了:
    procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
    begin
    If Not (Key in ['0'..'9']) Then Key:=Chr($0);
    end;
      

  3.   

    楼上两位都对,我自己常用的是二号楼长 “woshiwo(我是我)”的方法。
      

  4.   

    在edit的onkeypress中写入
    procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
    begin
    If Not (Key in ['0'..'9','.']) Then 
    begin
      Key:=#0;
      beep;
      showmessage('只能输入数字')。
    end;
    end;
      

  5.   

    还得加入Key in [#9,#13] 等几个键。
      

  6.   

    procedure Isnumber(sender:tobject);
    begin
     try
     strtofloat((sender as tedit).text);
     except
     (sender as tedit).text:='0';
     (sender as tedit).setfocus;
     end;end;procedure TForm1.Edit1Exit(Sender: TObject);
    begin
    Isnumber(sender as tedit);
    end;
      

  7.   

    It is a study book
      

  8.   

    procedure TForm1.Edit1PressUp(Sender:TObject);
    begin
      Edit1.Text:=Inttostr(StrtointDef(Edit1.text,0)); 
    end;
      

  9.   

    这样的问题的确应该在onexit中解决,或者在onchange里,如果用户是paste到edit里其他办法就有问题了。
      

  10.   

    Try
         StrToFloat(Edit1.Text);
    Except
         Edit1.Text:='0';
         Edit1.SetFocus;
    end;不让他输入其他就行,呵呵,也不用报错提示什么的。
      

  11.   

    onkeypress
    if not (key in ['0'..'9',#8]) then
    begin
    edit1.text:=inttostr(0);
    edit1.setfocus;
    end;
    收工。
      

  12.   

    我觉得楼上johnsonrao(johnson)仁兄的异常处理方法相对更为合理,不过放在OnExit事件里好象不是很好,这样用户就不能跳过该控件,最好是放在提交数据的事件里判断Try
         StrToFloat(Edit1.Text);
    Except
         On EConvertError do 
         begin
              Application.MessageBox('请输入数字!','系统提示',mb_ok+mb_information); //最好给个提示,让用户知道自己为什么输入非法数据
              Edit1.Text:='0';
              Edit1.SetFocus;
         end;
    end;
      

  13.   

    一句话:
    StrToIntDef(Edit1.text,0);
      

  14.   

    1+2:
    1、StrToIntDef(Edit1.text,0);
    2、在KeyPress事件中加入以下代码
    If Not (Key in ['0'..'9']) Then Key:=Chr($0);
      

  15.   

    xzhongjin(xzhongjin) 说得对!我哪来1000分,50分最多的了,结帐!