在OnChange事件中或OnKeyDown中判断

解决方案 »

  1.   

    在Edit的OnKeypress事件中加入:
    if not key in [0..9,'.','-'] then 
       key:=char(0);
    就这么简单。给分吧。
      

  2.   

    procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
    begin
      if ((Key>=#32) and (Key<#255)) and (not (((Key >= '0') and (Key <= '9')) or (Key = '.') or (Key = '-')))then
        Key := #0;
    end;
      

  3.   

    这样的问题会常遇到,数据库字段是floatedit的就麻烦了,自己作一个floatedit控件,会一劳永逸的。
      

  4.   

    procedure TForm1.Edit1KeyPress(var Key: Char);
    const CanKey :Set of Char=['0'..'9',
                   '.',
                   '-','+',
                   Char(VK_BACK),
                   Char(VK_DELETE),
                   Char(VK_RETURN),
                   CHar(VK_TAB)];begin
    with Edit1 do
    begin
    if SelStart=0 then CanKey := CanKey+['-','+']
             else CanKey := CanKey -['-','+'];
    if (not (Key in CanKey))  then
     Key:=#0;
    end;
    end;
      

  5.   

    procedure TForm1.Edit1KeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
        if (not (key in ['0'..'9', #45,#46,#8])) then
        Key := #0;
    end;