Recent

Author Topic: [SOLVED] Changing TEdit to an integer  (Read 41047 times)

WickedDum

  • Full Member
  • ***
  • Posts: 211
[SOLVED] Changing TEdit to an integer
« on: November 17, 2013, 09:49:42 am »
The user enters their Country Code (an integer < 1000).  In the class(TForm), CountryCode:TEdit.  How do I change the TEdit (string) input to an integer?   I am consistent:  the program keeps locking up...

For entry validation, I figured I should use Try/Finally.  The conversion routine would be in the try/finally construct.  Correct?

Did you figure out I am a noob yet?

Appreciate the help!

< Steve >
« Last Edit: November 25, 2013, 07:09:08 am by WickedDum »
Practice Safe Computing!!

Intel i5-4460K @ 3.2GHz | Win8.1 64-bit | FPC: v3.0 | Lazarus:  v1.6.0

Mujie

  • Jr. Member
  • **
  • Posts: 64
Re: Changing TEdit to an integer
« Reply #1 on: November 17, 2013, 11:54:06 am »
Code: [Select]
StrToInt(edit1.text)

this is what you looking for?

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: Changing TEdit to an integer
« Reply #2 on: November 17, 2013, 01:18:25 pm »
StrToInt function raises an exception if the user input is not a valid integer, so you could use a TSpinEdit.

String to Integer conversion is a very simple operation which involves no critical code, so you probably don't need to use try/finally block.
« Last Edit: November 17, 2013, 01:42:13 pm by typo »

zeljko

  • Hero Member
  • *****
  • Posts: 1959
    • http://wiki.lazarus.freepascal.org/User:Zeljan
Re: Changing TEdit to an integer
« Reply #3 on: November 17, 2013, 01:22:59 pm »
StrToInt function raises an error if the user input is not a valid integer, so you could use a TSpinEdit.

Maybe he should use StrToIntDef() or TryStrToInt() but anyway, TSpinEdit is better solution.

Avishai

  • Hero Member
  • *****
  • Posts: 1021
Re: Changing TEdit to an integer
« Reply #4 on: November 17, 2013, 01:38:58 pm »
You can use this code to ensure that the user only enters valid data.

Code: [Select]
procedure TForm1.Edit1KeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
const
  GoodKeys = [
    VK_0..VK_9,
    VK_NUMPAD0..VK_NUMPAD9,
    VK_Left,VK_Right,
    VK_BACK,VK_DELETE,
    VK_END,VK_HOME,
    VK_RETURN,VK_TAB
  ];
begin
  if not(Key in GoodKeys) then Key:= 0;
end;
Lazarus Trunk / fpc 2.6.2 / Win32

Bart

  • Hero Member
  • *****
  • Posts: 5731
    • Bart en Mariska's Webstek
Re: Changing TEdit to an integer
« Reply #5 on: November 17, 2013, 02:32:49 pm »
You could youse a MaskEdit with EditMask set to '9999;1;0' adn then use EditText (not Text) for the value, it will always be an integer value.

Bart

WickedDum

  • Full Member
  • ***
  • Posts: 211
Re: Changing TEdit to an integer
« Reply #6 on: November 17, 2013, 09:09:59 pm »
Thank you, all, for your great suggestions!

Mujie - It's the exception Typo alluded to that caused the StrToInt function to give me a problem.

Typo/Zeljko - I have some studying to do.  Thank you.  I have no idea about StrToIntDef()TryStrToInt(), or TSpinEdit.

Avishai - That's the style of coding I used to do...before "GUI" and IDEs!  I like it!  (Maybe add VK_ESC?)  Why are you using "VK_" instead of just the key nomenclature itself?  Reserved words?

Bart - more studying.  I don't think I have ever used a MaskEdit or EditText(not text).

Again, thanks for your time and solutions!  Back to the books for me!

< S >

Practice Safe Computing!!

Intel i5-4460K @ 3.2GHz | Win8.1 64-bit | FPC: v3.0 | Lazarus:  v1.6.0

Avishai

  • Hero Member
  • *****
  • Posts: 1021
Re: Changing TEdit to an integer
« Reply #7 on: November 18, 2013, 01:19:44 am »
Oops is good and I like it a lot.  But oops can be a trap.  You need a Numeric Edit so you build one.  But you need an Integer Edit and a Float Edit.  Now you have 2 controls.  But you also need a Currency Edit.  Now it's 3 controls - or 1 complex control with flags.  But there are other things you need as well.  Soon you have hundreds of controls but none do exactly what you need.  So you create a new one...  Or you work with the basic controls and write simple wrappers to do what you need and only write the controls that are truly needed.  I'm old school (Turbo Pascal 1.9) and prefer simplicity.  A blend of oops and procedural works best for me.
« Last Edit: November 18, 2013, 06:13:53 am by Avishai »
Lazarus Trunk / fpc 2.6.2 / Win32

WickedDum

  • Full Member
  • ***
  • Posts: 211
Re: Changing TEdit to an integer
« Reply #8 on: November 18, 2013, 01:53:53 am »
Avishai - Turbo 1.9???  You're pre-me!  I think I started with TP2 or 3.

Ok - What is the logic behind TSpinEdit??  It doesn't seem to relate to TEdit...

Where does the term SPIN come from? 

What is spin control?

Also, TCustomEdit?

If you offer me a webpage, fine.  But I couldn't find any that explain it...  I found:

lazarus-ccr.sourceforge.net/docs/lcl/spin/tspinedit.html and
http://lazarus-ccr.sourceforge.net/docs/lcl/spin/tcustomspinedit.html but there are no examples or discussions of use.

Appreciate the help!!

< S >
« Last Edit: November 18, 2013, 01:55:25 am by WickedDum »
Practice Safe Computing!!

Intel i5-4460K @ 3.2GHz | Win8.1 64-bit | FPC: v3.0 | Lazarus:  v1.6.0

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: Changing TEdit to an integer
« Reply #9 on: November 18, 2013, 01:59:52 am »
SpinEdit has came from Delphi, it is simple compatibility. Anyway, the dictionary says that "spin" is synonymous of "turn, revolve, etc".

TCustomX has the same properties as TX, but protected instead of published. So you have a basic class from which to derive another classes without having those properties published.
« Last Edit: November 18, 2013, 02:11:23 am by typo »

WickedDum

  • Full Member
  • ***
  • Posts: 211
Re: Changing TEdit to an integer
« Reply #10 on: November 18, 2013, 02:52:12 am »
I know what the word "spin" means.  I'm having trouble with the logic of going from TEdit for text and then out in left field for TSpinEdit for integers...  Just looking for how they relate or simply something that I can grasp onto.

TCustomX - got it!

Thanks!
Practice Safe Computing!!

Intel i5-4460K @ 3.2GHz | Win8.1 64-bit | FPC: v3.0 | Lazarus:  v1.6.0

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: Changing TEdit to an integer
« Reply #11 on: November 18, 2013, 03:00:08 am »
Well, you can consider SpinEdit a particular case of an Edit, on which you have ranges of numbers that 'spin".
« Last Edit: November 18, 2013, 03:08:14 am by typo »

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: Changing TEdit to an integer
« Reply #12 on: November 18, 2013, 03:13:00 am »
simply something that I can grasp onto.

I can almost claim that the name has something to do with the popular game show Wheel of Fortune.


Avishai

  • Hero Member
  • *****
  • Posts: 1021
Re: Changing TEdit to an integer
« Reply #13 on: November 18, 2013, 07:29:42 am »
Quote
I can almost claim that the name has something to do with the popular game show Wheel of Fortune.

Actually I think it's probably more closely related to pre-digital gas pumps. :)
Lazarus Trunk / fpc 2.6.2 / Win32

WickedDum

  • Full Member
  • ***
  • Posts: 211
Re: Changing TEdit to an integer
« Reply #14 on: November 18, 2013, 09:48:48 pm »
Thank you all for your input!  I appreciate it!

Since I have found the TSpinEdit control :D, I now see what you all were talking about.  And, I have to agree Avishai about its similarity with the pre-digital gas pumps.  ;)

Considering the  implementation of the TSpinEdit, I do not want the user to have to sit and "spin" waiting for their Country Code.  I know they can type it in.  Also, the arrows don't make it clean.

Avishai - Your code didn't work...because I am sure I didn't implement it correctly.   :(

How do I identify/assign the VK_ variables?  I found them in the wiki (http://wiki.freepascal.org/LCL_Key_Handling) and they appear to be some form of a constant. 

In addition, how do I declare Goodkeys?

Sorry to be such a noob...

< S >
Practice Safe Computing!!

Intel i5-4460K @ 3.2GHz | Win8.1 64-bit | FPC: v3.0 | Lazarus:  v1.6.0

 

TinyPortal © 2005-2018