Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
Member Area
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Become a Member
-Why sign up!
-Chat Online!
-Indexes NEW!!
-Build your resume
-Find a job
-Post a job
-Resume Search
-Link to us
Visit Embarcadero
Embarcadero Community
Restrict TEdit input to floating point numbers and a defined number of decimal p Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
Delphi 3.x
User Rating
No Votes
# Votes
DSP, Administrator
Reference URL:
			Author: Lou Adler

I would like a TEdit box on my form that only accepts keys to enter a floating 
point number that has N number of decimal places. What is the best way to do this?


Derive a new component from TEdit and override its KeyPress method. That is fed 
characters before the control has inserted them into the text. You can examine the 
character, reject it outright if it would not be valid for a floating point number. 
If it would be valid you have to examine the content of the edit, the values of 
SelStart and SelCount and figure out how the content would look like if you let the 
key pass throuogh. Test that new string against what you can accept, if it does not 
match you reject the key. The following OnKeyPress handler for a normal tedit 
control shows the logic. It should be integrated into a new component which also 
would have a property to set the number of allowable decimal digits.
2   procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
3   const
5   var
6     S: string;
7     val: Double;
8     n: Integer;
9   begin
10    if key <> #8 then {Always let backspace through}
11      if key in ['0'..'9', '-', DecimalSeparator] then
12      begin
13        {key is a candidate}
14        with Sender as TEdit do
15        begin
16          S := Text;
17          if SelLength > 0 then {key will replace selection}
18            Delete(S, SelStart + 1, SelLength);
19          Insert(key, S, SelStart + 1);
20          {S now has string as it would look after key is processed. Check if it is a 
21  valid floating point number.}
22          try
23            val := StrToFloat(S);
24            {OK, it computes. Find the decimal point and count the digits after it.}
25            n := Pos(decimalSeparator, S);
26            if n > 0 then
27              if (Length(S) - n) > MAXNUMBEROFDIGITS then
28                {too many digits, reject key}
29                key := #0;
30          except
31            {nope, reject key}
32            key := #0;
33          end;
34        end;
35      end
36      else {key not acceptible}
37        key := #0;
38  end;

Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10


Share this page
Download from Google

Copyright © Mendozi Enterprises LLC