|
|
 |
 |
 |
|
Join the Discussion
|
"Post your views, comments, questions and doubts to this article."
Discuss!
|
|
 |
 |
|
|
 |
 |
 |
Article submitted by: Michael Klaus for the Delphi Programming Quickies Contest.
TNumEdit
Some time ago I needed a component that worked like a TEdit, but only took numbers. I know, there are lots of them around on the Internet, but none of them I found were able to switch between allowing negatives or not or
between allowing decimals or not. Others seemed to accept the '-' sign only when entered as the first character, or accepted only the '.' as decimal seperator and not the one defined in Windows.
So here's my workaround to those troubles!
TNumEdit is a component, an TEdit descedant, that accepts only numerical input. You can adjust whether to accept positive or negative numbers (AllowNeg) or integer or decimals (AllowDec). You can also limit the input by using MinValue and/or MaxValue.
Here's a code snippet, the WhenKeyPress event handler procedure:
procedure TNumEdit.WhenKeyPress(Sender: TObject; var Key: Char);
var
p : integer;
begin
case Key of
'0'..'9' : ;
'.',',' : if AllowDec AND
(pos(DecimalSeparator,Text)=0)
then Key:=DecimalSeparator
else Key:=#0;
#8 : ;
#45 : if FAllowNeg then
begin
p :=SelStart;
eValue:=-eValue;
if eValue>0
then SelStart:=p-1
else SelStart:=p+1;
Key:=#0;
end;
else
Key:=#0;
end;
end;
|
Download full source code!
Of course, if you have any question please post and discuss on the Delphi Programming Forum
|