如何判断一个edit.text输入的是数字(50分)

  • 主题发起人 主题发起人 apache
  • 开始时间 开始时间
A

apache

Unregistered / Unconfirmed
GUEST, unregistred user!
如何判断一个edit.text输入的是数字
 
IsNumber := True;
try
StrToInt(edit.Text);
except
IsNumber ;= False;
end;
 
在OnChange事件中可以判断。
 
在MouseDown事件中拦截键盘‘0’~‘9’是否可以?
 
我用onchange老出问题,比如edit1.text:='er'
只改变r就处罚,而我的初衷是把'er'全换掉。不知道各位大侠是怎么解决的
 
如果你想输入纯数字最好使用MaskEdit,这样就不用判断,
否则可以在keypress判断数字的key在一定的范围之内
 
把edit1.text转为int.并对此进行保护。。如下

var S1:integer;
begin
try
S1:=strtoint(edit1.text); //强制转换为数值型
except
begin
Messagebox('只能输入数字'); //这里可为出现提示,或改为处理事件
end;
end;
end.
 
在OnKeyPress中屏蔽掉字母键。只许0—9输入。
 
try
StrtoInt(edit.text);
except
showmessage('不是数字’);
end;
 
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
case Key of
'0'..'9': ; //对数字键响应
#8: ; //对退格键响应
#13: seekClick(Sender);//进行查询
else
begin
key:=#8;
playsound('',0,snd_async);//声音提示
end;
end;
end;

 
本来想发表点意见的。看了windwy兄的发言!我觉得还是保持沉默比较好!
 
看看问题!
>如何判断一个edit.text输入的是数字
~~~~ ~~~~~
只需要
val(edit1.text,n,code);
if code <> 0 then //不是数字
 
我觉得以上几位的建议都不错,不过,最好,还是用 MASKEDIT ,这样可以省去许多麻烦
 
试试这个输入数字的Edit控件

unit OopsNumberEdit;

{$D-}

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;

type
TOopsNumberType = (ntInteger, ntDouble, ntMoney);

TOopsNumberEdit = class(TCustomEdit)
private
FCanvas: TControlCanvas;
FFocused: Boolean;
FValue: Extended;
FisNull: Boolean;
FNumberType: TOopsNumberType;
FValidChars: set of Char;
function GetTextMargins: TPoint;
procedure SetNumberType(Value: TOopsNumberType);
procedure CMEnter(var Message: TCMEnter); message CM_ENTER;
procedure CMExit(var Message: TCMExit); message CM_EXIT;
procedure WMPaint(var Message: TWMPaint); message WM_PAINT;
protected
procedure Change; override;
procedure KeyPress(var Key: Char); override;
public
constructor Create(AOwner: TComponent); override;
procedure Clear; override;
property IsNull: Boolean read FIsNull;
property Value: Extended read FValue;
published
property Anchors;
property AutoSelect;
property AutoSize;
property BiDiMode;
property BorderStyle;
property Color;
property Constraints;
property Ctl3D;
property Enabled;
property Font;
property MaxLength;
property NumberType: TOopsNumberType read FNumberType write SetNumberType;
property ParentBiDiMode;
property ParentColor;
property ParentCtl3D;
property ParentFont;
property ParentShowHint;
property PopupMenu;
property ReadOnly;
property ShowHint;
property TabOrder;
property TabStop;
property Text;
property Visible;
property OnChange;
property OnClick;
property OnDblClick;
property OnEnter;
property OnExit;
property OnKeyDown;
property OnKeyPress;
property OnKeyUp;
property OnMouseDown;
property OnMouseMove;
property OnMouseUp;
end;

procedure Register;

implementation

const
TOopsNumberFmt: array[TOopsNumberType]of string =
('#,##0','#,##0.##########','#,##0.00');

constructor TOopsNumberEdit.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
ControlStyle := ControlStyle + [csReplicatable];
Text:='';
end;

procedure TOopsNumberEdit.Change;
var c: Extended;
begin
if Trim(Text)='' then begin
FisNull:=True;
FValue:=0; end
else
if TextToFloat(PChar(Text), FValue, fvExtended)
then begin
FisNull:=False;
c:=FValue;
case FNumberType of
ntInteger: c:=Round(FValue);
ntMoney: c:=Round(FValue)+Round(Frac(FValue)*100)/100;
end;
if c<>FValue then Text:=FloatToStr(c);
end else begin
Text:=FloatToStr(FValue);
end;
inherited;
Invalidate;
end;

procedure TOopsNumberEdit.KeyPress(var Key: Char);
begin
inherited KeyPress(Key);
if (Key in [#32..#255]) and not(Key in FValidChars) then
begin
MessageBeep(0);
Key := #0;
end;
end;

procedure TOopsNumberEdit.SetNumberType(Value: TOopsNumberType);
begin
if FNumberType<>Value then FNumberType:=Value;
Change;
if FNumberType=ntInteger
then FValidChars := ['-', '0'..'9']
else FValidChars := [DecimalSeparator, '-', '0'..'9'];
end;

procedure TOopsNumberEdit.Clear;
begin
FisNull:=True;
FValue:=0;
inherited;
end;

procedure TOopsNumberEdit.CMEnter(var Message: TCMEnter);
begin
FFocused:=True;
inherited;
end;

procedure TOopsNumberEdit.CMExit(var Message: TCMExit);
begin
FFocused:=False;
if FisNull then Text:=''
else Text:=FloatToStr(FValue);
inherited;
Invalidate;
end;

procedure TOopsNumberEdit.WMPaint(var Message: TWMPaint);
const
AlignStyle : array[Boolean, TAlignment] of DWORD =
((WS_EX_LEFT, WS_EX_RIGHT, WS_EX_LEFT),
(WS_EX_RIGHT, WS_EX_LEFT, WS_EX_LEFT));
var
Left: Integer;
Margins: TPoint;
R: TRect;
DC: HDC;
PS: TPaintStruct;
S: string;
AAlignment: TAlignment;
ExStyle: DWORD;
begin
if FFocused then AAlignment := taLeftJustify
else AAlignment := taRightJustify;
if UseRightToLeftAlignment then ChangeBiDiModeAlignment(AAlignment);
if ((AAlignment = taLeftJustify) or FFocused) and
not (csPaintCopy in ControlState) then
begin
if SysLocale.MiddleEast and HandleAllocated and (IsRightToLeft) then
begin { This keeps the right aligned text, right aligned }
ExStyle := DWORD(GetWindowLong(Handle, GWL_EXSTYLE)) and (not WS_EX_RIGHT) and
(not WS_EX_RTLREADING) and (not WS_EX_LEFTSCROLLBAR);
if UseRightToLeftReading then ExStyle := ExStyle or WS_EX_RTLREADING;
if UseRightToLeftScrollbar then ExStyle := ExStyle or WS_EX_LEFTSCROLLBAR;
ExStyle := ExStyle or
AlignStyle[UseRightToLeftAlignment, AAlignment];
if DWORD(GetWindowLong(Handle, GWL_EXSTYLE)) <> ExStyle then
SetWindowLong(Handle, GWL_EXSTYLE, ExStyle);
end;
inherited;
Exit;
end;
if FCanvas = nil then
begin
FCanvas := TControlCanvas.Create;
FCanvas.Control := Self;
end;
DC := Message.DC;
if DC = 0 then DC := BeginPaint(Handle, PS);
FCanvas.Handle := DC;
try
FCanvas.Font := Font;
with FCanvas do
begin
R := ClientRect;
if not (NewStyleControls and Ctl3D) and (BorderStyle = bsSingle) then
begin
Brush.Color := clWindowFrame;
FrameRect(R);
InflateRect(R, -1, -1);
end;
Brush.Color := Color;
if not Enabled then
Font.Color := clGrayText;
if FisNull then S:='' else begin
S := FormatFloat(TOopsNumberFmt[FNumberType],FValue);
if FNumberType=ntMoney then S:=CurrencyString+S; end;
Margins := GetTextMargins;
case AAlignment of
taLeftJustify: Left := Margins.X;
taRightJustify: Left := ClientWidth - TextWidth(S) - Margins.X - 1;
else
Left := (ClientWidth - TextWidth(S)) div 2;
end;
if SysLocale.MiddleEast then UpdateTextFlags;
TextRect(R, Left, Margins.Y, S);
end;
finally
FCanvas.Handle := 0;
if Message.DC = 0 then EndPaint(Handle, PS);
end;
end;

function TOopsNumberEdit.GetTextMargins: TPoint;
var
DC: HDC;
SaveFont: HFont;
I: Integer;
SysMetrics, Metrics: TTextMetric;
begin
if NewStyleControls then
begin
if BorderStyle = bsNone then I := 0 else
if Ctl3D then I := 1 else I := 2;
Result.X := SendMessage(Handle, EM_GETMARGINS, 0, 0) and $0000FFFF + I;
Result.Y := I;
end else
begin
if BorderStyle = bsNone then I := 0 else
begin
DC := GetDC(0);
GetTextMetrics(DC, SysMetrics);
SaveFont := SelectObject(DC, Font.Handle);
GetTextMetrics(DC, Metrics);
SelectObject(DC, SaveFont);
ReleaseDC(0, DC);
I := SysMetrics.tmHeight;
if I > Metrics.tmHeight then I := Metrics.tmHeight;
I := I div 4;
end;
Result.X := I;
Result.Y := I;
end;
end;

procedure Register;
begin
RegisterComponents('OopsWare', [TOopsNumberEdit]);
end;

end.
 
用这个控件感觉不错
 
多人接受答案了。
 
后退
顶部