Jump to content
Sign in to follow this  
Gord P

TNumberBox question regarding mode=nbmFloat in Version 12

Recommended Posts

I have not installed version 12 yet, but I am wondering if the TNumberBox control is still unable to recognize 'e' or 'E' (for exponent) when entering a number in the box when the mode is set to nbmFloat.  

Share this post


Link to post

The current Delphi 12 version doesn't support that out of the box.

Share this post


Link to post

Thanks Uwe.  I should have put in a request a long time ago for it.  I tried going back in to see if someone else had put a request in for that but couldn't login even though it said the site was in "read only" mode until the migration was completed.

 

Currently, to ensure the user has entered a proper float value (allowing for 1.0E10 type of input) I simply handle it after the fact (i.e. after some OK button or CALC button or whatever has been clicked) by using TryStrToFloat() on the contents of the Text string from a basic TEdit and displaying a message if it is not a valid float.

 

I would like to be able to do the checking while the value is being entered similar to what TNumberBox does in how it restricts the user to only using valid characters - except allowing for 'e' or 'E'.  One challenge I see with that is that someone might incorrectly enter an 'e' at the end of the number (eg. "1.234E") which is not a valid float.  Might have to just check OnExit but that might lead to awkward behavior.   Anyway, just thinking out loud.  Thanks again for checking.

 

 

 

Edited by Gord P

Share this post


Link to post

This is indeed very instable. One would need to extend the current internals significantly to make this work. Nevertheless one can achieve some results by setting AcceptExpressions to True and override these two methods:

type
  TNumberBox = class(Vcl.NumberBox.TNumberBox)
  protected
    function GetValidCharSet(AAcceptExpressions: Boolean): TSysCharSet; override;
    function IsNumericText(const AText: string): Boolean; overload; override;
  end;

function TNumberBox.GetValidCharSet(AAcceptExpressions: Boolean): TSysCharSet;
begin
  Result := inherited GetValidCharSet(AAcceptExpressions);
  if Mode = nbmFloat then
    Result := Result + ['e', 'E', '+', '-'];
end;

function TNumberBox.IsNumericText(const AText: string): Boolean;
begin
  Result := inherited;
  if AText.LastIndexOfAny(['e', 'E']) = AText.Length - 1 then
    Result := False;
end;

 

  • Like 1

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×