You are here:About>Computing & Technology>Delphi Programming> Power Using VCL Components> Creating VCL components> TNumEdit
About.comDelphi Programming
RTL reference|Glossary|Tips/Tricks|FREE App/VCL|Essentials|Books|Link Back
 
TNumEdit
TNumEdit is a component, just like TEdit, but it only accepts numerical input. You can adjust whether to accept positive or negative numbers or integer or decimals. You can also limit the input by using MinValue and/or MaxValue. Full source code.
 Win prizes by sharing code!
Do you have some Delphi code you want to share? Are you interested in winning a prize for your work?
Delphi Programming Quickies Contest
 Join the Discussion
"Post your views, comments, questions and doubts to this article."
Discuss!
 Related Resources
• Using VCL components
• Creating components

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

All graphics (if any) in this feature created by Zarko Gajic.

 More Delphi
· Learn another routine every day - RTL Quick Reference.
· Download free source code applications and components.
· Talk about Delphi Programming, real time.
· Link to the Delphi Programming site from your Web pages.
· Tutorials, articles, tech. tips by date: 2003|2002|2001|2000|1999|1998 or by TOPIC.
 Stay informed with all new and interesting things about Delphi (for free).
Subscribe to the Newsletter
Name
Email

 Got some code to share? Got a question? Need some help?
From Zarko Gajic,
Your Guide to Delphi Programming.
FREE Newsletter. Sign Up Now!
Newsletters & RSSEmail to a friendSubmit to Digg
 All Topics | Email Article | | |
Advertising Info | News & Events | Work at About | SiteMap | Reprints | HelpOur Story | Be a Guide
User Agreement | Ethics Policy | Patent Info. | Privacy Policy©2008 About, Inc., A part of The New York Times Company. All rights reserved.