A workaround for this is to not rely on the rather useless Microsoft implementation behind the ES_NUMBER
style, but implement your own logic.
type
TEdit = class(VCL.StdCtrls.TEdit)
protected
FInsideChange: boolean;
function RemoveNonNumbers(const MyText: string): string;
procedure KeyPress(var Key: Char); override;
procedure Change; override;
end;
procedure TEdit.KeyPress(var Key: Char);
begin
if NumbersOnly then begin
if not(Key in ['0'..'9','-',#8,#9,#10,#13,#127]) then begin
Key:= #0;
//Put user feedback code here, e.g.
MessageBeep;
StatusBar.Text:= 'Only numbers allowed';
end else StatusBar.Text:= '';
end;
inherited KeyPress(Key);
end;
procedure TEdit.Change; override;
begin
if FInsideChange then exit;
FInsideChange:= true;
try
inherited Change;
Self.Text:= RemoveNonNumbers(Self.Text);
finally
FInsideChange:= false;
end;
end;
function TEdit.RemoveNonNumbers(const MyText: string): string;
var
i,a: integer;
NewLength: integer;
begin
NewLength:= Length(MyText);
SetLength(Result, NewLength);
a:= 1;
for i:= 1 to Length(MyText) do begin
if MyText[i] in ['0'..'9'] or ((i=1) and (MyText[i] = '-')) then begin
Result[a]:= MyText[i];
Inc(a);
end else begin
Dec(NewLength);
end;
end; {for i}
SetLength(Result, NewLength);
end;
Now non-numbers will not be accepted, not even when pasting text.
NumbersOnly
property is merely enabling theES_NUMBER
window style. – Remy Lebeau