一个简单问题:如何使Edit框只允许写入数字

解决方案 »

  1.   

    if not (key in['0'..'9',#13,#8]) then abort;
      

  2.   

    真是easy啊procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
    begin
        if not (key in(['0'..'9'])) then
          key := #13;
    end;
      

  3.   

    定义一个枚举类型,[0,1,2,3,4,5,6,7,8,9,.]
    在OnKeyDown事件中判断,如果NOT IN [.....],就返回空
      

  4.   

    if not (key in(['0'..'9'])) then
           begin
           //
           end;
      

  5.   

    很简单啊.
    增加一个edit的onkeypress事件代码如下:
    var l:boolean;
    begin 
     l:=(key<#48)or(key>#57);
     if l then key:=#0;
    end;
      

  6.   

    最好把‘.'、'Spaceback'带上:if not (key in['0'..'9','.',#8]) then 
     key:=#0;
      

  7.   

    同意楼上
    procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
    begin
      if not (key in ['0'..'9','.',#8]) then
      key := #0;
    end;
      

  8.   

    //用我做的控件,Style属性
    unit tzDigitEdit;interfaceuses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
      StdCtrls;type  TStyle = (esInteger,esText,esFloat,esCurrency);  TtzDigitEdit = class(TEdit)
      private
        FStyle: TStyle;
        procedure WMPaste(var Message: TMessage); message WM_PASTE;
        procedure WMChar(var Message: TWMChar); message WM_CHAR;
        procedure SetStyle(const value: TStyle);
        { Private declarations }
      protected
        { Protected declarations }
      public
        constructor Create(AOwner: TComponent); override;
        { Public declarations }
      published
        property Style: TStyle read FStyle write SetStyle default esInteger;
        { Published declarations }
      end;procedure Register;implementationprocedure Register;
    begin
      RegisterComponents('TZhuang', [TtzDigitEdit]);
    end;{ TtzDigitEdit }
    constructor TtzDigitEdit.Create(AOwner: TComponent);
    begin
      inherited;
      Style:= esInteger;
      Text:= '0';
    end;procedure TtzDigitEdit.SetStyle(const value: TStyle);
    begin
      if FStyle <> value then begin
        //设计期或运行期
        if (csDesigning in ComponentState) or (ComponentState = []) then
          case value of
            esText:
              Text:= '';
            esInteger:
              Text:= '0';
            esFloat,esCurrency:
              Text:= '0.0';
          end;
        FStyle:= value;
      end;
    end;procedure TtzDigitEdit.WMChar(var Message: TWMChar);
    var s: string;
    begin
      if Style = esText then begin
        inherited;
        exit;
      end;  if Chr(Message.CharCode) in ['a'..'z','A'..'Z'] then
        Message.CharCode:= 0
      else begin
        s:= Text;
        inherited;
        try
          if Style = esInteger then
            StrToInt(Text)
          else if Style = esFloat then
            StrToFloat(Text)
          else if Style = esCurrency then
            StrToCurr(Text);
        except
          Text:= s;
        end;
      end;end;procedure TtzDigitEdit.WMPaste(var Message: TMessage);
    var
      S: string; 
    begin
      if Style = esText then begin
        inherited;
        exit;
      end;  S:= Text;
      inherited;
      try
        if Style = esInteger then
          StrToInt(Text)
        else if Style = esFloat then
          StrToFloat(Text)   //有争议,转换结果是Extended型,到数据库里是否出错?
        else if Style = esCurrency then
          StrToCurr(Text);
      except
        Text:= S;
      end;end;end.
      

  9.   

    但是有一个问题,如果在onkeypress中写的话,如果要用到退格健什么的,就很不自然了,包括其他的一些键,
      

  10.   

    楼上说的情况我遇到过,不过看楼住的意思,键盘上的键应该只需要用到数字键和退格键,别的用不到。所以加个判断就可以了。还有希望这样的问题,楼主在问的时候搜索一下,我已经遇到过n次这种同样的问题了,(n>=3)