Recent

Author Topic: TEdit.NumbersOnly  (Read 2601 times)

apeoperaio

  • Sr. Member
  • ****
  • Posts: 272
TEdit.NumbersOnly
« on: September 23, 2020, 02:54:10 pm »
Dear all,
I noticed that the behaviour of TEdit when NumbersOnly = True is different between Windows and Mac.
When NumbersOnly = True on Windows it is not allowed to type any character different from digits in the TEdit.
On mac (cocoa) with NumbersOnly = True I can still type any character in the TEdit.
Is it a bug?

Thank you,
Andrea

Lazarus 2.0.11 r63871 FPC 3.2.0 x86_64-darwin-cocoa

zeljko

  • Hero Member
  • *****
  • Posts: 1594
    • http://wiki.lazarus.freepascal.org/User:Zeljan
Re: TEdit.NumbersOnly
« Reply #1 on: September 23, 2020, 03:09:08 pm »
Yes

Hansaplast

  • Hero Member
  • *****
  • Posts: 674
  • Tweaking4All.com
    • Tweaking4All
Re: TEdit.NumbersOnly
« Reply #2 on: September 23, 2020, 03:09:41 pm »
For MacOS and Linux I can confirm this unexpected behavior.


Sounds like a bug since this is not the same as Windows.
You may want to report that in the BugTracker.


I tested this with Lazarus/FPC from trunk.


Failed:
MacOS: Lazarus 2.1.0 r63883 FPC 3.3.1 x86_64-darwin-cocoa
Linux: Lazarus 2.1.0 r63883 FPC 3.3.1 x86_64-linux-gtk2


Worked:
Windows: Lazarus 2.1.0 r63883 FPC 3.3.1 i386-win32-win32/win64

apeoperaio

  • Sr. Member
  • ****
  • Posts: 272
Re: TEdit.NumbersOnly
« Reply #3 on: September 23, 2020, 03:15:40 pm »

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: TEdit.NumbersOnly
« Reply #4 on: September 23, 2020, 03:24:12 pm »
Hi!

Until the bug is fixed:
Do it yourself!


Code: Pascal  [Select][+][-]
  1. procedure TForm1.Edit1KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
  2. var isOk : boolean;
  3. begin
  4. isOk := (key in [VK_0..VK_9,VK_Back,VK_delete,VK_Left,VK_right]) and
  5.    NOT ( ssShift in Shift);
  6. if NOT isOk then key := 0;
  7. end;
  8.  

If you need PLUS and/or MINUS then add

VK_OEM_PLUS       
VK_OEM_MINUS         

to the set.

Winni
 

wp

  • Hero Member
  • *****
  • Posts: 11853
Re: TEdit.NumbersOnly
« Reply #5 on: September 23, 2020, 03:24:26 pm »
NumbersOnly is a pretty much useless property because even on Windows it does not behave correctly: try to enter '-1' -- it will swallow the '-'. It is here only for Delphi compatibility (which does the same nonsense).

For numeric input you should select TSpinEdit, TFloatSpinEdit, or even better (because you can turn off the up/down buttons) TSpinEditEx or TFloatSpinEditEx.

Your bug report probably will be rejected because the NumbersOnly propery is documented as widgetset-dependent. (see page "Restricted" in Object Inspector, or menu "View" > "Restriction Browser").

apeoperaio

  • Sr. Member
  • ****
  • Posts: 272
Re: TEdit.NumbersOnly
« Reply #6 on: September 23, 2020, 03:50:44 pm »
Dear wp, thank you for the info.
I already replace TEdit with TSpinEdit in my project.

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: TEdit.NumbersOnly
« Reply #7 on: September 23, 2020, 04:25:12 pm »
The NumbersOnly property is Widgetset dependant, you can see on the "Restricted" tab in the onbejct inspecotr that is not implemented for all widgetsets.

It was added because Delphi has it.
And in Delphi it behaves like in Lazarus for Windows.
It is the widgetset (win32/win64) that enforces this behaviour, not our code, it is just a binding.
And, as one might expect, the implementation by MS is a bit odd, to say the least.

As wp said: the behaviour that MS introduced is pretty useless.
And, yes we could implement this in KeyPress, but we would also have to forbid pasting non-numbers into the edit.
That is a PITA to do (see TMaskEdit if you are interested to what kind of mess that leads).

If the WS doesn't support it, we (the Lazarus team) are not going to implement it.
At least, that the common opinion ATM.

Bart

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: TEdit.NumbersOnly
« Reply #8 on: September 23, 2020, 04:31:06 pm »
Reported as: 0037801
https://bugs.freepascal.org/view.php?id=37801

And that was the third bugreport about the same issue.
Please first search the bugtracker if an issue alreadys has been reported before opening a new ticket.

Bart
« Last Edit: September 23, 2020, 10:33:47 pm by Bart »

apeoperaio

  • Sr. Member
  • ****
  • Posts: 272
Re: TEdit.NumbersOnly
« Reply #9 on: September 23, 2020, 04:51:59 pm »
Thank you Bart.

jwdietrich

  • Hero Member
  • *****
  • Posts: 1232
    • formatio reticularis
Re: TEdit.NumbersOnly
« Reply #10 on: September 24, 2020, 09:10:47 am »
The number of submitted bug reports suggests that the pure existence of NumbersOnly may be puzzling, at least for newcomers. It should be marked as deprecated.
function GetRandomNumber: integer; // xkcd.com
begin
  GetRandomNumber := 4; // chosen by fair dice roll. Guaranteed to be random.
end;

http://www.formatio-reticularis.de

Lazarus 2.2.6 | FPC 3.2.2 | PPC, Intel, ARM | macOS, Windows, Linux

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: TEdit.NumbersOnly
« Reply #11 on: September 24, 2020, 12:56:52 pm »
In the mean time, this has been implemented for GTK3 (the WS provides a binding for that) and GTK2 (coded in our WS implementation, in the exact same moronic way that MS has done it, where -1 is not a number, nor is 1.23).

I think it is a mistake.
But now you have a case to have it implemented in QT as well.
Feel free to re-open the ticket you opened in the bugtraker.

We have better controls for entering numbers: T(Float)SpinEdit(Ex).
IIRC you can remove the "spinners" form these controls if you don't like them.

Bart
« Last Edit: September 24, 2020, 12:58:25 pm by Bart »

 

TinyPortal © 2005-2018