Recent

Author Topic: TEdit with "FloatOnly"?  (Read 1276 times)

Jvan

  • Full Member
  • ***
  • Posts: 108
TEdit with "FloatOnly"?
« on: August 03, 2020, 02:30:55 am »
How to code an Edit that only allows float/double characters, considering the decimal separator.

Handoko

  • Hero Member
  • *****
  • Posts: 3752
  • My goal: build my own game engine using Lazarus
Re: TEdit with "FloatOnly"?
« Reply #1 on: August 03, 2020, 04:56:00 am »
I usually do not limit user to type certain characters, but I will validate the input before using it. The reason (for me) not to limit the characters is the user may copy/paste something from somewhere and then edit it on the TEdit.

The validation code can be something like this:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   FloatResult: Double;
  4. begin
  5.   case TryStrToFloat(Edit1.Text, FloatResult) of
  6.     True:  ;  // do something
  7.     False: ShowMessage('The value you provided is not valid.');
  8.   end;
  9. end;

You can add more rules if you want, like upper and lower value limits.

Alternatively, you may want to try TFloatSpinEdit, which is on Misc tab.
« Last Edit: August 03, 2020, 04:58:23 am by Handoko »

Jvan

  • Full Member
  • ***
  • Posts: 108
Re: TEdit with "FloatOnly"?
« Reply #2 on: August 03, 2020, 05:17:42 am »
Thanks!

wp

  • Hero Member
  • *****
  • Posts: 7524
Re: TEdit with "FloatOnly"?
« Reply #3 on: August 03, 2020, 11:10:05 am »
Alternatively, you may want to try TFloatSpinEdit, which is on Misc tab.

Or TFloatSpinEditEx on the LazControls tab which has additional useful properties:
  • UpDownVisible - false hides the updown buttons
  • ArrowKeys - false disables incrementing/decrementing the value by the arrow keys; the controls behaves more like a normal TEdit
  • DecimalSeparator - point or comma (or anything else)
  • text automatically right-aligned
  • NullValue, NullValueBehaviour - specifies what happens when field is empty
  • Layout - move spin buttons to the left
and more...
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

Bart

  • Hero Member
  • *****
  • Posts: 3904
    • Bart en Mariska's Webstek
Re: TEdit with "FloatOnly"?
« Reply #4 on: August 03, 2020, 01:06:56 pm »
Or, if you don't need input like 1.3E12, but only like 123.456, you might consider a TMaskEdit.
However TFloatSPinEdit(Ex) has the advantage you dont'have to bother about conversion, since it has a Value property.

Bart

wp

  • Hero Member
  • *****
  • Posts: 7524
Re: TEdit with "FloatOnly"?
« Reply #5 on: August 03, 2020, 01:16:37 pm »
Or, if you don't need input like 1.3E12 [...]
Bart, TFloatSpinEditEx, BTW, does not accept such an input, it just swallows the "E" which can lead to unnoticed errors when the user does so and is not careful. I think there should be an option for exponential numbers.
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

Bart

  • Hero Member
  • *****
  • Posts: 3904
    • Bart en Mariska's Webstek
Re: TEdit with "FloatOnly"?
« Reply #6 on: August 03, 2020, 03:33:33 pm »
Bart, TFloatSpinEditEx, BTW, does not accept such an input, it just swallows the "E" which can lead to unnoticed errors when the user does so and is not careful. I think there should be an option for exponential numbers.

I know, does TFloatSpinEdit accept it?

The problems is that if we allow this we can't do "validation as you type".

Bart

wp

  • Hero Member
  • *****
  • Posts: 7524
Re: TEdit with "FloatOnly"?
« Reply #7 on: August 03, 2020, 04:45:28 pm »
I know, does TFloatSpinEdit accept it?
No.

The problems is that if we allow this we can't do "validation as you type".
The problem with "validation as you type" is that when the user is not careful, for example because he looks on the keyboard during typing, he does not notice that his input has ben changed. Type "1.3E2" into the TFloatSpinEdit(Ex) and do not look at the screen - there is a high chance that the program will continue unnoticed with the input value 1.32 instead of 130... I much more prefer "validation at exit" or even "validation at form close".
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

Bart

  • Hero Member
  • *****
  • Posts: 3904
    • Bart en Mariska's Webstek
Re: TEdit with "FloatOnly"?
« Reply #8 on: August 03, 2020, 06:00:31 pm »
I will have a look when I have the time (maybe open an issue and assign it to me?).
Feel free to implement this yourself though.

[ETA]I just looked at TFloatSpinEdit: it also just "eats" 'E'. I don't think (since that one is basically WS driven) we can adjust that, but Maybe for TFloatSpinEditEx we can.

Bart
« Last Edit: August 03, 2020, 06:05:30 pm by Bart »

Ally

  • New Member
  • *
  • Posts: 11
Re: TEdit with "FloatOnly"?
« Reply #9 on: August 03, 2020, 06:36:30 pm »
Hello,

my component Numedit is a component for entering numbers.
The input field only accepts valid numbers that can be limited using the Matrix property.


The component provides four new properties.

1. Matrix (string)
   The number format is defined via this property.

   Examples:
   99.99 Allows you to enter values in the range from 0 to 99.99 with two decimal places.
   -50.9 Allows you to enter values in the range from -50.9 to 50.9 with one decimal place.
   250,000 Allows you to enter values in the range from 0 to 250 with three decimal places.
   -500 Allows you to enter values in the range from -500 to 500 without decimal places.


2. Wert (value) (Extended)
   Returns the extended value of the entered value.
   This value can be evaluated during input and represents the value currently entered in the input field.


3. Entertaste (Enter key) (Boolean)
   Allows the Enter key to be used in the same way as the Tab key.


4. Warnton (Warning) tone (Boolean)
   Outputs a warning tone when illegal entries are made.

-------------------------------------------------- -------------------------------------------------- ----------------------


Number is a component for entering numbers.
The input field only accepts the numbers 0-9, leading zeros are also permitted.


The component provides two new properties.

1. Entertaste (Enter key) (Boolean)
   Allows the Enter key to be used in the same way as the Tab key.


2. Warnton (Warning) tone (Boolean)
   Outputs a warning tone when illegal entries are made.


Greetings Roland

Handoko

  • Hero Member
  • *****
  • Posts: 3752
  • My goal: build my own game engine using Lazarus
Re: TEdit with "FloatOnly"?
« Reply #10 on: August 03, 2020, 06:40:56 pm »
Interesting, thank you for sharing it.

wp

  • Hero Member
  • *****
  • Posts: 7524
Re: TEdit with "FloatOnly"?
« Reply #11 on: August 03, 2020, 07:03:11 pm »
I will have a look when I have the time (maybe open an issue and assign it to me?).
Feel free to implement this yourself though.
https://bugs.freepascal.org/view.php?id=37474
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

Bart

  • Hero Member
  • *****
  • Posts: 3904
    • Bart en Mariska's Webstek
Re: TEdit with "FloatOnly"?
« Reply #12 on: August 03, 2020, 10:27:51 pm »
I just tested what happens if you set Value to e.g. 1E150 in a TFloatSpinEdit: Text will be something like:
Code: [Select]
999999999999999980000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00

This is going to look a bit silly and rather confusing.
I think the control wasn't designed to do something like this.
(And if increment is set to i, you can't "spin" the control anymore, since adding/subtracting 1 doesn't change the value.)

If we want to implement something like this, we need to display "large" numbers in scientific format.
Not sure where the threshold for that should be.
At what point does a Double not have enough digits to represent a large (integer) value?

Bart

Bart

  • Hero Member
  • *****
  • Posts: 3904
    • Bart en Mariska's Webstek
Re: TEdit with "FloatOnly"?
« Reply #13 on: August 03, 2020, 10:33:37 pm »
https://bugs.freepascal.org/view.php?id=37474

Thanks for reporting.

Lets discuss the pro's and cons of accepting scientific notation in TFloatSPinEdit as well as wat to achieve it in the bugreport, not here anymore (we kind of hijacked this discussion).

Bart

Jvan

  • Full Member
  • ***
  • Posts: 108
Re: TEdit with "FloatOnly"?
« Reply #14 on: August 03, 2020, 11:36:24 pm »
Alternatively, you may want to try TFloatSpinEdit, which is on Misc tab.

Or TFloatSpinEditEx on the LazControls tab which has additional useful properties:
  • UpDownVisible - false hides the updown buttons
  • ArrowKeys - false disables incrementing/decrementing the value by the arrow keys; the controls behaves more like a normal TEdit
  • DecimalSeparator - point or comma (or anything else)
  • text automatically right-aligned
  • NullValue, NullValueBehaviour - specifies what happens when field is empty
  • Layout - move spin buttons to the left
and more...

Hi

How can I set the decimal separator of Windows by default? I ask it because it has "." by default. Something like:
Code: Pascal  [Select][+][-]
  1.  FloatSpinEditEx1.DecimalSeparator := ¿WindowsSeparator?

 

TinyPortal © 2005-2018