一个个地在KeyPress事件中写if not (key in ['0'..'9',   '.']) then Key:=#0;太麻烦了......

解决方案 »

  1.   

    指定所有的TEdit的OnKeyPress()都指向同一个事件啊。
      

  2.   

    to 红辣椒:关键是我在每个TEdit控件里还要写一些针对这一特定控件的代码,如if key=#13 then跳转到下一控件
      

  3.   

    我提议,你写一个函数。我以前写过一个,也不麻烦:function IsNumber(s: string):boolean;
    var
      i,m_Length:Integer;
    begin
      Result := false;
      m_Length := Length(s);
      if m_Length <= 0 then
      begin
        Exit;
      end;
      for i:= 1 to m_Length do
      begin
        if (i = 1) then
        begin
          if (not( s[i] in ['0'..'9'])) and (not (s[i] = '-')) then
          begin
            Exit;
          end;
        end
        else
        begin
          if not( s[i] in ['0'..'9']) then
          begin
            Exit;
          end;
        end;
      end;
      Result := true;
    end;
    然后再你的每个edit 中调用这个函数,添加判断就好了!这样简单些!
    同时也赞成,一楼的看法,就是在一个edit中添加一段代码,然后让所有edit keydown事件只想着一个公共模块。
    希望对楼主有所帮助!如果感觉还有什么困难,就给我发邮件:
    [email protected] 
      

  4.   

    你新建一个VCL组件,继承自TEdit,然后重载它的CreateParam方法和Create方法,然后就可以享受成果了:unit SCEdit;interfaceuses
      SysUtils, Classes, Controls, StdCtrls, Windows;type
      TNumEdit = class(TEdit)
      private
        { Private declarations }
      protected
        procedure CreateParams(var Params: TCreateParams); override;
        { Protected declarations }
      public
        constructor Create(Owner: TComponent); override;
        { Public declarations }
      published
        { Published declarations }
      end;procedure Register;implementationprocedure Register;
    begin
      RegisterComponents('Standard', [TNumEdit]);
    end;procedure TNumEdit.CreateParams(var Params: TCreateParams);
    begin
      inherited CreateParams(Params);
      Params.Style := Params.Style or ES_NUMBER or ES_CENTER;
    end;constructor TNumEdit.Create(Owner: TComponent);
    begin
      inherited;
      Text := '0';
    end;
      

  5.   

    如果不想要数字居中,把Params.Style   :=   Params.Style   or   ES_NUMBER   or   ES_CENTER; 这一句里的ES_CENTER去掉,当然你还可以加个属性来设置是以左对齐、右对齐还是居中方式显示了。