Recent

Author Topic: TEdit.NumbersOnly take two  (Read 1727 times)

atlatl

  • New Member
  • *
  • Posts: 21
TEdit.NumbersOnly take two
« on: February 26, 2025, 06:50:09 pm »
I want an edit field that only accepts numbers.

Started off with TEdit with the NumbersOnly property set...no luck; I can type anything into it.

Found a few takes on numbers-only filter statements; same result.

Read the previous thread about this topic:[https://forum.lazarus.freepascal.org/index.php/topic,51549.msg378499.html#msg378499], tried the suggested solution - TSpinEdit; again same result.

Finally found this in the wiki: LCL Key Handling [https://wiki.lazarus.freepascal.org/LCL_Key_Handling]. It sounds like it might be the solution to the issue. Has anybody translated it to working Pascal code? I haven't got clue where to begin.

Thanks,
Harv



Zvoni

  • Hero Member
  • *****
  • Posts: 3219
Re: TEdit.NumbersOnly take two
« Reply #1 on: February 27, 2025, 08:35:45 am »
Use OnKeyPress-Eventhandler

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: char);
  2. begin
  3.   If Not ((Ord(key)>=48) And (Ord(Key)<=57) Or (Ord(Key)=8)) Then Key:=Char('');
  4. end;          

No decimal separators, no +/- signs. Digits Only

Note: This doesn't defend from Copy/Pasting into the Edit
« Last Edit: February 27, 2025, 08:40:29 am by Zvoni »
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

egsuh

  • Hero Member
  • *****
  • Posts: 1729
Re: TEdit.NumbersOnly take two
« Reply #2 on: February 27, 2025, 08:54:34 am »
It's strange. I cannot type in other characters than numbers, if NumberOnly is set true. On Windows 10 (and afterwards, possibly).

Of course I can assign other text directly to TEdit control, like:

         Edit1.Text := 'abcde';
« Last Edit: February 27, 2025, 08:56:37 am by egsuh »

Zvoni

  • Hero Member
  • *****
  • Posts: 3219
Re: TEdit.NumbersOnly take two
« Reply #3 on: February 27, 2025, 09:43:00 am »
It's strange. I cannot type in other characters than numbers, if NumberOnly is set true. On Windows 10 (and afterwards, possibly).

Of course I can assign other text directly to TEdit control, like:

         Edit1.Text := 'abcde';

https://lazarus-ccr.sourceforge.io/docs/lcl/stdctrls/tcustomedit.numbersonly.html

Quote
NumbersOnly is not supported on all platform for the LCL; GTK 2 does not support the property.
....
Remark:      NumbersOnly is not supported for the GTK2 or GTK3 widgetset.
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Ally

  • Jr. Member
  • **
  • Posts: 81
Re: TEdit.NumbersOnly take two
« Reply #4 on: February 27, 2025, 10:06:38 am »
Have a look at this.

https://forum.lazarus.freepascal.org/index.php/topic,53352.msg394615.html#msg394615

I think this component could solve your problems.

atlatl

  • New Member
  • *
  • Posts: 21
Re: TEdit.NumbersOnly take two
« Reply #5 on: February 27, 2025, 03:50:38 pm »
Thanks to all who responded.

I'm thinking I may not have been clear enough that the issue was with the Cocoa widgetset.

@Zvoni: Nice, concise code, but it doesn't work in my case 😞.
Remark:      NumbersOnly is not supported for the GTK2 or GTK3 widgetset.
Nor, does it seem, is it supported fo Cocoa.

@Ally: Thanks, I'll take a look at it.

atlatl

  • New Member
  • *
  • Posts: 21
Re: TEdit.NumbersOnly take two
« Reply #6 on: February 27, 2025, 06:32:06 pm »
This is what I finally came up with:
Code: Pascal  [Select][+][-]
  1. FUNCTION IsAnInteger(pIntStr: String): Boolean;
  2.   VAR
  3.     sInt: Integer;
  4.   BEGIN
  5.     Result:= True;
  6.     TRY
  7.       sInt:= StrToInt(pIntStr);
  8.     EXCEPT
  9.       Result:= False;
  10.     END;
  11.   END;
  12.  
  13. PROCEDURE TForm1.Timer1Timer(Sender: TObject);
  14.   BEGIN
  15.     IF Form1.SpinEdit1.Text <> '' THEN
  16.       IF Not IsAnInteger(Form1.SpinEdit1.Text) THEN
  17.         BEGIN
  18.           Form1.SpinEdit1.Text:= '';
  19.         END;
  20.   END;
  21.  

Perhaps not the most elegant code, but it does the job.
I set the timer to 1/10 second, so it's responsive enough.
It can be used with any edit control.
It handles pasted text.
It _should_ work with any widgetset.

wp

  • Hero Member
  • *****
  • Posts: 13324
Re: TEdit.NumbersOnly take two
« Reply #7 on: February 27, 2025, 06:52:59 pm »
Code: Pascal  [Select][+][-]
  1. FUNCTION IsAnInteger(pIntStr: String): Boolean;
  2.   VAR
  3.     sInt: Integer;
  4.   BEGIN
  5.     Result:= True;
  6.     TRY
  7.       sInt:= StrToInt(pIntStr);
  8.     EXCEPT
  9.       Result:= False;
  10.     END;
  11.   END;
I'd prefer to use TryStrToInt which does not raise an exception and thus does not interrupt your work in the IDE (even when caught in the "except" part):
Code: Pascal  [Select][+][-]
  1. FUNCTION IsAnInteger(pIntStr: String): Boolean;
  2. VAR
  3.   sInt: Integer;
  4. BEGIN
  5.   Result := TryStrToInt(pIntStr, sInt);
  6. END;

atlatl

  • New Member
  • *
  • Posts: 21
Re: TEdit.NumbersOnly take two
« Reply #8 on: February 27, 2025, 07:00:09 pm »
Thanks wp, I'd never seen 'TryStrToInt' until now.


I'd prefer to use TryStrToInt which does not raise an exception and thus does not interrupt your work in the IDE (even when caught in the "except" part):
Code: Pascal  [Select][+][-]
  1. FUNCTION IsAnInteger(pIntStr: String): Boolean;
  2. VAR
  3.   sInt: Integer;
  4. BEGIN
  5.   Result := TryStrToInt(pIntStr, sInt);
  6. END;

AlexTP

  • Hero Member
  • *****
  • Posts: 2665
    • UVviewsoft
Re: TEdit.NumbersOnly take two
« Reply #9 on: February 27, 2025, 07:06:19 pm »
TATSynEdit gives the TATEdit subclass (single line), which has OptInputNumberOnly property. Leading minus is allowed by OptInputNumberAllowNegative property.

 

TinyPortal © 2005-2018