Recent

Author Topic: TEdit.Text - content - CHR OR Number  (Read 14444 times)

jmLandsvik

  • New Member
  • *
  • Posts: 29
TEdit.Text - content - CHR OR Number
« on: April 05, 2011, 04:14:29 pm »
Hi

I'm trying to detect wheter the content of a TEdit.Text is acharacter or a number.
Pretty sure that TP/Delhpi used to have "IS...." for this stuff, like ISALPHA, or ISALPHANUMERIC...

OR do I have to do the job myself ?

if X=65, then CHR(X) => 'A'
What I need is to get 65 out of 'A' ....

regards
« Last Edit: April 05, 2011, 04:50:40 pm by jmLandsvik »

jmLandsvik

  • New Member
  • *
  • Posts: 29
Re: TEdit.Text - content - CHR OR Number
« Reply #1 on: April 05, 2011, 05:22:58 pm »
puhh....

This post put me on track...
http://www.lazarus.freepascal.org/index.php/topic,9198.msg44926.html#msg44926

so my result ended up as:

Code: [Select]
VAR
  i : INTeger ;

bla bla bla

  i := ORD(Edit1.Text[1]) ;

more bla bla

thus, typing A in the edit field, gives i =65...

Tnx.
/*JM*/

rajivsoft

  • New Member
  • *
  • Posts: 48
Re: TEdit.Text - content - CHR OR Number
« Reply #2 on: April 05, 2011, 06:11:04 pm »
Hi

I'm trying to detect wheter the content of a TEdit.Text is acharacter or a number.
Pretty sure that TP/Delhpi used to have "IS...." for this stuff, like ISALPHA, or ISALPHANUMERIC...

OR do I have to do the job myself ?

if X=65, then CHR(X) => 'A'
What I need is to get 65 out of 'A' ....

regards

fast google search pointed me to:
Code: Pascal  [Select][+][-]
  1. FUNCTION IsNumeric(s: STRING): boolean;
  2. VAR
  3.   j: integer;
  4. BEGIN
  5. //TODO:Replace ',' with Systemdelemiter
  6.   Result := (length(s) > 0);
  7.   FOR i := 1 TO length(s) DO
  8.     IF NOT ((Char(s[j]) IN ['0'..'9']) or
  9.        (Char(s[j]) = DecimalSeparator) or
  10.        (Char(s[j]) = '-') or (Char(s[j]) = '+')) THEN
  11.       BEGIN
  12.         Result := False;
  13.         exit;
  14.       END;
  15. END;
use this with something like
Code: Pascal  [Select][+][-]
  1. if IsNumeric(Edt_Field.text) then
  2.   my_number := StrToInt(Edt_Field.text)
  3. else
  4.   exit; // or return false or whatever is your context
« Last Edit: April 05, 2011, 06:14:52 pm by rajivsoft »

Leledumbo

  • Hero Member
  • *****
  • Posts: 8831
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: TEdit.Text - content - CHR OR Number
« Reply #3 on: April 05, 2011, 08:02:09 pm »
Another way would be to use StrToInt in a try-except block, because the function will raise an exception on non-integer string input.

Bart

  • Hero Member
  • *****
  • Posts: 5619
    • Bart en Mariska's Webstek
Re: TEdit.Text - content - CHR OR Number
« Reply #4 on: April 06, 2011, 12:14:29 pm »
Another way would be to use StrToInt in a try-except block, because the function will raise an exception on non-integer string input.

Or even easier:

Code: [Select]
var dummy: LongInt;
  ...
  if not TryStrToInt(Edit1.Text, dummy) then //not an integer

Bart

jmLandsvik

  • New Member
  • *
  • Posts: 29
Re: TEdit.Text - content - CHR OR Number
« Reply #5 on: April 06, 2011, 12:20:15 pm »
Leledumbo
Bart

That part I did figure out, problem here was how to get the value 65 from 'A'.

And here's what I came up with:
Code: [Select]
...
     OK := TryStrToInt(Edit1.Text, i) ;

  // now we do the work.
  IF NOT OK THEN  // we have ourself a char.
    BEGIN
      ii := ORD(Edit1.Text[1]) ;
      ListBox.Items.Add(CHR(ii) +'=' +IntToStr(ii)) ;
    end
    ELSE          // we have a number(?hopefully!)
      ii := StrToInt(Edit1.Text) ;
...

regards
Jan Magne

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12537
  • FPC developer.
Re: TEdit.Text - content - CHR OR Number
« Reply #6 on: April 06, 2011, 02:44:08 pm »
Leledumbo
Bart

That part I did figure out, problem here was how to get the value 65 from 'A'.

Use ORD()  eg. ord(s[2])

   
 

rajivsoft

  • New Member
  • *
  • Posts: 48
Re: TEdit.Text - content - CHR OR Number
« Reply #7 on: April 06, 2011, 04:03:10 pm »
Leledumbo
Bart

That part I did figure out, problem here was how to get the value 65 from 'A'.

And here's what I came up with:
Code: [Select]
...
     OK := TryStrToInt(Edit1.Text, i) ;

  // now we do the work.
  IF NOT OK THEN  // we have ourself a char.
    BEGIN
      ii := ORD(Edit1.Text[1]) ;
      ListBox.Items.Add(CHR(ii) +'=' +IntToStr(ii)) ;
    end
    ELSE          // we have a number(?hopefully!)
      ii := StrToInt(Edit1.Text) ;
...

regards
Jan Magne
I don't understand what are you trying to do... the else part ii := StrToInt(Edit1.Text); is useless because if you come there you have same value in i , in other case if you have more that one char it dosen't work and loose sense for me...

jmLandsvik

  • New Member
  • *
  • Posts: 29
Re: TEdit.Text - content - CHR OR Number
« Reply #8 on: April 08, 2011, 03:51:15 pm »
Leledumbo
Bart

That part I did figure out, problem here was how to get the value 65 from 'A'.

And here's what I came up with:
Code: [Select]
...
     OK := TryStrToInt(Edit1.Text, i) ;

  // now we do the work.
  IF NOT OK THEN  // we have ourself a char.
    BEGIN
      ii := ORD(Edit1.Text[1]) ;
      ListBox.Items.Add(CHR(ii) +'=' +IntToStr(ii)) ;
    end
    ELSE          // we have a number(?hopefully!)
      ii := StrToInt(Edit1.Text) ;
...

regards
Jan Magne
I don't understand what are you trying to do... the else part ii := StrToInt(Edit1.Text); is useless because if you come there you have same value in i , in other case if you have more that one char it dosen't work and loose sense for me...

rajivsoft:
Tnx for the replys...

hehe, I can see that.
The App. I'm building is a "template" to show how a program is supposed to work in AutoCad.
So, if you have a termninal bar with 10 terminals, and need to renumber those, this prog. will do it for you. Problem is, they did not put in support for numbering lik (a, b, c, or A1, B1, C1), any combination with prefix & suffix, but numbers only.
So, when I have the letter 'D' in my textbox, the programs the start with D, using a step number=2, and prefix='', suffix[1] to produse this output: (D[1], F[1], H[1]...)
Or any other sequensial output...

But for other purposes, I'm going to try out the code you came up with, for more than one char...

tnx,
Jan Magne

 

TinyPortal © 2005-2018