Recent

Author Topic: TryStrToInt problem  (Read 2911 times)

Zath

  • Sr. Member
  • ****
  • Posts: 391
TryStrToInt problem
« on: May 28, 2015, 12:49:09 am »
Hello.
I'm using TryStrToInt to test the entry of an edit box.
After many attempts , I got it to work but is allows the value of 0.
My field entry must be 1..9.
Do we really call 0 an integer ?
How do I capture it's entry without having to check for keypresses etc. ?

Thanks.

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: TryStrToInt problem
« Reply #1 on: May 28, 2015, 01:28:12 am »
OnChange event handler?

dogriz

  • Full Member
  • ***
  • Posts: 127
Re: TryStrToInt problem
« Reply #2 on: May 28, 2015, 08:25:31 am »
Don't you need something like this, instead of TryStrToInt?
Code: [Select]
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: char);
begin
  if not(Key in ['0'..'9', #13]) then
    Key := #0;
end;
FPC 3.2.2
Lazarus 2.2.4
Debian x86_64, arm

mangakissa

  • Hero Member
  • *****
  • Posts: 1131
Re: TryStrToInt problem
« Reply #3 on: May 28, 2015, 08:33:55 am »
-1, -2, -3, ect are also integers. So yes, 0 is a part of it.
But what are you try to convert?
Lazarus 2.06 (64b) / FPC 3.0.4 / Windows 10
stucked on Delphi 10.3.1

Zath

  • Sr. Member
  • ****
  • Posts: 391
Re: TryStrToInt problem
« Reply #4 on: May 28, 2015, 10:48:03 am »
-1, -2, -3, ect are also integers. So yes, 0 is a part of it.
But what are you try to convert?

The requirement is to test edit1.text to see if its 1..9 then edit2.text to see if its 0.. and do a calculation.
You can move from edit1 to edit2 by return key, tab key and clicking, you can also click on a button to do the calculation.


howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: TryStrToInt problem
« Reply #5 on: May 28, 2015, 03:52:27 pm »
You would probably do best to use the NumbersOnly property of TEdit, and its OnEditingDone event.

 

TinyPortal © 2005-2018