Recent

Author Topic: numerical characters only - how?  (Read 29033 times)

Anonymous

  • Guest
numerical characters only - how?
« on: September 07, 2004, 07:12:28 am »
how can i make an EditBox or MaskedEdit accept only numerical characters. i am not a delphi user, and i am starting to learn lazarus.

i have this calculator project whar the user should enter only numerical character or a backspace. otherwise, the character would simply be ignored, thus not adding anything to the present text within the editbox/MaskedEdit.

Thanks in advance.

scismondo

  • New Member
  • *
  • Posts: 13
    • http://www.icilinux.com
numerical characters only - how?
« Reply #1 on: September 07, 2004, 09:48:23 am »
Hi,

I don't have the whole answer, but I think you should take a look at the OnKeyPress event.
This way you'll be able to chek if the character is a number or not each time a key is pressed.

Seb.

Vincent Snijders

  • Administrator
  • Hero Member
  • *
  • Posts: 2661
    • My Lazarus wiki user page
numerical characters only - how?
« Reply #2 on: September 07, 2004, 10:12:59 am »
In the mask edit control, you can set the EditMask porperty to ####, if you want four digits.

Anonymous

  • Guest
numerical characters only - how?
« Reply #3 on: September 08, 2004, 06:01:40 am »
Vincent,

thanks for your answer. i'm a bit uncomfortable with using EditMask though, since using it would require sacrificing of format of the editbox.

scimondo,

thank you too. actually i've already tried the OnKeyPress event of lazarus before posting the question. the event contains a variable named Key, which is of character type. it absorbs the character being pressed by the user. however, lazarus treats Key and the character added in the textbox to be somewhat independent.

it means that upon keypress, the variable Key recieves the same character. we can then add conditions so that Key would become empty if the key pressed is not numeric. thus, Key remains numeric. but then again Key is not what is entered in the Keypress. no matter how you code it to chop off non-numeric variables will not matter, for the character added to the textbox is not the value of the key, but the actual character as pressed!

thus, the OnKeyPress of the editbox gives at least two outputs.

1. the character added to the textbox.
2. the Key character, which absorbs the same as that pressed, but can be edited to contain only numeric entries.

what we need to edit is the first item, as it is the one seen by the user. how do we do that?

scismondo

  • New Member
  • *
  • Posts: 13
    • http://www.icilinux.com
numerical characters only - how?
« Reply #4 on: September 08, 2004, 09:54:31 am »
Hi,

I don't know exactly how to code it (I'm still a beginner :-) ) but I think I have the theory:

- We still need the OnKeyPress event; Don't worry about the Key variable, we only need to execute some code each time a key is pressed.
- We check if we can make a mathematical operation with the edit box's content. Something like "if FloatToStr(EditBox1.Text) * 10 ==> float" then we continue.
- If the operation doesn't return a float then we delete the last character entered.

Now maybe someone else can help you find the good syntax?

Seb.

eric

  • Sr. Member
  • ****
  • Posts: 267
Re: numerical characters only - how?
« Reply #5 on: September 08, 2004, 12:32:16 pm »
Quote from: "Anonymous"
how can i make an EditBox or MaskedEdit accept only numerical characters.

Set up an OnKeyPress event for the edit box, and put the following in the event handler:

  if not (Key in ['0'..'9', '.', #8, #9]) then Key := #0;

This throws away all key presses except numbers, decimal point, backspace and tab. It also allows you to use the Delete key, which isn't handled by OnKeyPress.

Eric

scismondo

  • New Member
  • *
  • Posts: 13
    • http://www.icilinux.com
Re: numerical characters only - how?
« Reply #6 on: September 08, 2004, 02:25:47 pm »
Quote from: "eric"

  if not (Key in ['0'..'9', '.', #8, #9]) then Key := #0;


lol, a lot easier than my idea :-)

Seb.

Anonymous

  • Guest
numerical characters only - how?
« Reply #7 on: September 09, 2004, 03:38:10 am »
eric,

thanks. i have done the same code before posting here. but then it does not work! but it does in vb, and i think also in delphi. what the editbox does is that it manipulates the Key character as per the code, but it still displays the same character as what is pressed by the user.

this behavior is different from vb and delphi.

eric

  • Sr. Member
  • ****
  • Posts: 267
numerical characters only - how?
« Reply #8 on: September 09, 2004, 08:02:31 pm »
What version of Lazarus are you using? This is a bug which has been fixed. I'm not sure if the latest release snapshot includes the fix, but recent cvs versions certainly do. You might find it worth downloading the latest cvs daily snapshot.

Eric

bswfcw

  • Newbie
  • Posts: 2
Re: numerical characters only - how?
« Reply #9 on: December 13, 2011, 04:05:57 pm »
Still not working in November release

bswfcw

  • Newbie
  • Posts: 2
Re: numerical characters only - how?
« Reply #10 on: December 13, 2011, 04:09:34 pm »
Never mind...it is! :D

TurboRascal

  • Hero Member
  • *****
  • Posts: 672
  • "Good sysadmin. Bad programmer."™
Re: numerical characters only - how?
« Reply #11 on: December 16, 2011, 03:09:06 am »
Set up an OnKeyPress event for the edit box, and put the following in the event handler:

  if not (Key in ['0'..'9', '.', #8, #9]) then Key := #0;

It would still be possible for the user to paste wrong values...  Unfortunately there is no easy way to prevent it that I know of. There should be additional checks for this, you could put some code in the OnChange handler to see if the user has managed to insert something nasty...

The MaskEdit is generally a good idea for such uses, but I don't like the need for exception handling in case something wrong is entered. BTW, there is another useful control for entering numbers only, the SpinEdit.

If working on windows only and willing to use winapi, this works perfectly:
Code: Pascal  [Select][+][-]
  1. SetWindowLong(Edit1.Handle, GWL_STYLE, GetWindowLong(Edit1.Handle, GWL_STYLE) or ES_NUMBER);
You should put this in FormCreate. I don't know it there is something similar which works multi-platform...
Regards, ArNy the Turbo Rascal
-
"The secret is to give them what they need, not what they want." - Scotty, STTNG:Relics

 

TinyPortal © 2005-2018