Recent

Author Topic: Am I the only one missing a "allow only numbers" setting in TEditBox  (Read 5295 times)

ArtLogi

  • Full Member
  • ***
  • Posts: 194
Title says pretty much everything. I have found and figured out the routines to do so, in small scale.

Code: Pascal  [Select][+][-]
  1. if not (Key in ['0'..'9', '.', #8, #9]) then Key := #0;
..and many more so since this so common "challenge", as I have understood there is no problems in todays world.

But this seems to be one thing that have been missing from basic libraries for what I can remember (with my almost nonexistent programming experience). I can't be the only one making programs that wants a numbers as an input.

One only for integers with + / -
One for Decimals with +/- and , or .

Would be so convenient to just select the option from code inspector instead of using templates and copypasting and modifying.  :-\
While Record is a drawer and method is a clerk, when both are combined to same space it forms an concept of office, which is alias for a great suffering.

Leledumbo

  • Hero Member
  • *****
  • Posts: 8836
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Am I the only one missing a "allow only numbers" setting in TEditBox
« Reply #1 on: January 20, 2016, 06:42:27 am »
Nah, that's far too limited. So something fancier is invented: http://wiki.lazarus.freepascal.org/jujiboutils

ArtLogi

  • Full Member
  • ***
  • Posts: 194
Re: Am I the only one missing a "allow only numbers" setting in TEditBox
« Reply #2 on: January 20, 2016, 06:52:29 am »
..But simple is beautifull.  ;D
While Record is a drawer and method is a clerk, when both are combined to same space it forms an concept of office, which is alias for a great suffering.

fred

  • Full Member
  • ***
  • Posts: 208
Re: Am I the only one missing a "allow only numbers" setting in TEditBox
« Reply #3 on: January 20, 2016, 09:26:10 am »
You could also try TMaskEdit in the Addinional tab  :)

wp

  • Hero Member
  • *****
  • Posts: 13578
Re: Am I the only one missing a "allow only numbers" setting in TEditBox
« Reply #4 on: January 20, 2016, 09:45:51 am »
I don't like programs swallowing key presses. While typing numbers I often don't look at the screen, and I don't notice that something was wrong. Maybe I am entering the wrong decimal separator; if it is swallowed the entered number will be completely wrong - possibly unnoticed! It would be much better to accept all keys and display an error message when editing is done.

ArtLogi

  • Full Member
  • ***
  • Posts: 194
Re: Am I the only one missing a "allow only numbers" setting in TEditBox
« Reply #5 on: January 20, 2016, 10:47:08 am »
Mask edit is glumsy monster and also with totally different style. I'm not speaking about a form for thermo nuclear warhead launchcode input here, but typical "give value of formula / calculation""give amount of apples" type of thing.

If I need to enter value of apples I have in my bucket then you can not wrote errorchecking that would a) make sure I didn't calculate the amount wrong while piling them from bucket to table, b) that I enter 500 instead of 501. Since the input is totally random inside some loose rules. Ie. must be cardinal and amount is below 10000k apples.

Same for floats, give the peakvalue of current: Person could type -+1713.1321A or +-21.1321uA in same field, for such "idiot proof" errorcheck does not exist.

For dates and ISBN numbers something like MaskBox fits and is designed, but for general shield for "numbers only field" it just doesn't seem to work too nicely and people are homebrewing error check routines etc. around Editbox, what professional programmers do I don't know. Maybe using external library or maybe there is something like this in ie. newest Delphi libraries.

 
« Last Edit: January 20, 2016, 10:49:28 am by ArtLogi »
While Record is a drawer and method is a clerk, when both are combined to same space it forms an concept of office, which is alias for a great suffering.

Bart

  • Hero Member
  • *****
  • Posts: 5731
    • Bart en Mariska's Webstek
Re: Am I the only one missing a "allow only numbers" setting in TEditBox
« Reply #6 on: January 20, 2016, 11:58:27 am »
Title says pretty much everything. I have found and figured out the routines to do so, in small scale.

What is TEditBox?

I can't be the only one making programs that wants a numbers as an input.

There are T(FloatSpinEdit) and if you have trunk T(Float)SpinEditEx.
They were designed for just this purpose.

Mind you, users will still be able to paste anything into the control.
(TMaskEdit prevents that (study the code to see how ugly that is), but in general is not the best control for your use case).

For TEdit, if you only want integers (no floats) and you're on Windows, set NumbersOnly to True.
This works also for TSpinEdit(Ex), and it will on this platform not allow pasting non-numbers into the control.

Bart

Bart

  • Hero Member
  • *****
  • Posts: 5731
    • Bart en Mariska's Webstek
Re: Am I the only one missing a "allow only numbers" setting in TEditBox
« Reply #7 on: January 20, 2016, 12:00:08 pm »
Code: Pascal  [Select][+][-]
  1. if not (Key in ['0'..'9', '.', #8, #9]) then Key := #0;

You missed ^C, ^V, ^X and ^Z here (copy, paste, cut, undo)  >:D

Bart

Thaddy

  • Hero Member
  • *****
  • Posts: 19272
  • Glad to be alive.
Re: Am I the only one missing a "allow only numbers" setting in TEditBox
« Reply #8 on: January 20, 2016, 01:00:48 pm »
@Bart:
That's why OnKeyUp and OnKeyDown also give you the shift state.
In general it is better to write code that uses the key values + shift states than relying f.e. on OnKeyPress which gives you a char from a codepage.
Better to do the mapping yourself.
objects are fine constructs. You can even initialize them with constructors.

Nesto

  • New member
  • *
  • Posts: 9
Re: Am I the only one missing a "allow only numbers" setting in TEditBox
« Reply #9 on: January 20, 2016, 01:21:03 pm »
windows ->  SetWindowLong(Edit1.Handle, GWL_STYLE, GetWindowLong(Edit1.Handle, GWL_STYLE) or ES_NUMBER);

Bart

  • Hero Member
  • *****
  • Posts: 5731
    • Bart en Mariska's Webstek
Re: Am I the only one missing a "allow only numbers" setting in TEditBox
« Reply #10 on: January 20, 2016, 01:53:09 pm »
@Bart:
In general it is better to write code that uses the key values + shift states than relying f.e. on OnKeyPress which gives you a char from a codepage.

Which is not relevant for characters inside the ASCII range.

Bart

Bart

  • Hero Member
  • *****
  • Posts: 5731
    • Bart en Mariska's Webstek
Re: Am I the only one missing a "allow only numbers" setting in TEditBox
« Reply #11 on: January 20, 2016, 01:54:21 pm »
windows ->  SetWindowLong(Edit1.Handle, GWL_STYLE, GetWindowLong(Edit1.Handle, GWL_STYLE) or ES_NUMBER);

What do you think do that TCustomEdit.NumbersOnly property does?

Bart

garlar27

  • Hero Member
  • *****
  • Posts: 652
Re: Am I the only one missing a "allow only numbers" setting in TEditBox
« Reply #12 on: January 20, 2016, 03:59:17 pm »
You can use TSpinEdit and TFloatSpinEdit in "Misc" tab.

 

TinyPortal © 2005-2018