[Lazarus] floating point mask
ik
idokan at gmail.com
Wed Dec 9 19:32:59 CET 2009
You are over engineering this.
Here is what I did (not completed yet):
TfrmCalculator = class (TForm)
edtAmount : TEdit ;
edtPerc : TEdit ;
edtResult : TEdit ;
procedure edtAmountKeyDown (Sender : TObject ; var Key : Word ;
Shift : TShiftState );
procedure edtAmountKeyPress (Sender : TObject ; var Key : char );
procedure FormCreate (Sender : TObject );
private
{ private declarations }
public
{ public declarations }
FloatingPoint : array [1..2] of Boolean;
end;
procedure TfrmCalculator.edtAmountKeyPress (Sender : TObject ; var Key :
char);
function CanFloatingPoint : boolean;
begin
Result := (not FloatingPoint[TCustomEdit(Sender).Tag]) and
(Length(TCustomEdit(Sender).Text) > 0);
end;
var
OldSelStart : integer;
begin
if (Key = '.') and (CanFloatingPoint) then
begin
FloatingPoint[TCustomEdit(Sender).Tag] := True;
end
else
if (Key = #8) then
begin
OldSelStart := TCustomEdit(Sender).SelStart;
TCustomEdit(Sender).SelStart := OldSelStart - 1;
TCustomEdit(Sender).SelLength := 1;
if TCustomEdit(Sender).SelText = '.' then
begin
FloatingPoint[TCustomEdit(Sender).Tag] := False;
end;
TCustomEdit(Sender).SelLength := 0;
TCustomEdit(Sender).SelStart := OldSelStart;
end
else if (Key in ['0'..'9']) then
Key := Key
else //Floating point not allowed
Key := #0;
end;
procedure TfrmCalculator.edtAmountKeyDown (Sender : TObject ; var Key : Word
;
Shift : TShiftState);
var
Selected : Integer;
OldStart : Integer;
begin
Selected := TCustomEdit(Sender).SelLength;
OldStart := TCustomEdit(Sender).OldStart;
if Key = VK_DELETE then
begin
if (Selected = 0) then
begin
TCustomEdit(Sender).SelLength := 1;
if (TCustomEdit(Sender).SelText = '.') then
FloatingPoint[TCustomEdit(Sender).Tag] := False;
TCustomEdit(Sender).SelLength := 0;
TCustomEdit(Sender).SelLength := OldStart;
end
else
if (Selected = 1) then
begin
if (TCustomEdit(Sender).SelText = '.') then
FloatingPoint[TCustomEdit(Sender).Tag] := False;
end
else if Selected > 1 then
begin
if Pos('.', TCustomEdit(Sender).SelText) > 0 then
FloatingPoint[TCustomEdit(Sender).Tag] := False;
end;
end;
end;
procedure TfrmCalculator.FormCreate (Sender : TObject);
begin
FillChar(FloatingPoint, SizeOf(FloatingPoint) , 0);
end;
I'm thinking in creating such a component btw, if anyone is interested.
Ido
http://ik.homelinux.org/
On Wed, Dec 9, 2009 at 8:14 PM, David Emerson <dle3ab at angelbase.com> wrote:
> > David Emerson wrote:
> > > at first I overlooked "e+" which is valid for val (string, real)
>
> Alexander Klenin wrote:
> > No, it is not:
> > Val('e+', x, d);
>
> What I meant is that I first overlooked 'e' and '+' as valid characters
> that can appear together within a string to be converted to a float.
> The following compiles and works as expected:
>
> var x : real;
> begin
> val ('4.1e+5', x); // "e+" is valid for val (within context)
> writeln (x);
> end.
>
> The point I was making was that if someone is going to make a floating
> point mask (note the subject...) that one should be careful not to
> exclude any valid characters if restricting input by character. I'm not
> sure if the set I suggested ['-', '.', '0'..'9', 'e', 'E', '+'] is
> complete.
>
> Cheers,
> ~David.
>
>
> --
> _______________________________________________
> Lazarus mailing list
> Lazarus at lists.lazarus.freepascal.org
> http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus-ide.org/pipermail/lazarus/attachments/20091209/d28bca10/attachment-0004.html>
More information about the Lazarus
mailing list