求助:限制edit之类的文本框只能输入数字的方法?

解决方案 »

  1.   

    procedure EditContentRestriction(AEditControl: TCustomEdit; Len: Integer;
      IsNumeric: Boolean; var Key: Char);
      procedure DoErr;
      begin
        Key := #0;
        MessageBeep(1);
      end;
    begin
      if Key = #8 then Exit;
      if Len > 0 then
         begin
           if AEditControl.SelLength > 0 then Exit;
           if Length(AEditControl.Text) > Len-1 then
              begin
                DoErr;
                Exit;
              end;
         end;
      if IsNumeric then if not (Key in ['0'..'9', #8]) then
        begin
          DoErr;
          Exit;
        end;
    end;_____________________
    http://lysoft.7u7.net
      

  2.   

    在KeyPress事件中使用procedure TFrmPhoneBook.Edit2KeyPress(Sender: TObject; var Key: Char);
    begin
      EditContentRestriction(Sender as TCustomEdit, 20, True, Key);
    end;_____________________
    http://lysoft.7u7.net
      

  3.   

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

  4.   

    procedure TForm1.Edit2KeyPress(Sender: TObject; var Key: Char);
    begin
      if not (Key in ['0'..'9', '.', #8, #46]) then
        Key := #0;
    end;
      

  5.   

    SetWindowLong(Edit1.Handle, GWL_STYLE,GetWindowLong(Edit1.Handle, GWL_STYLE) 
    or ES_NUMBER);
      

  6.   

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

  7.   

    ES_NUMBER,不过,据msdn,这个window style只在创建窗口时有用:即运行时不可修改。