Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
28595 Discussions

Checking a real number is entered in an edit box

Intel_C_Intel
Employee
418 Views
Is there a simple way of checking that the text entered in an Edit Box is a real number, i.e. '12.345' would be OK, but '12.34x' would be wrong.
I tried using the WRITE statement with ERR but sometimes it doesn't work - it converted 'ty' to '1.0'.

Thanks,
David
0 Kudos
4 Replies
Steven_L_Intel1
Employee
418 Views
I think you meant READ, not WRITE.

Don't use * format for this - it's much too flexible. Use G format (eg. G10.0) in an explicit format instead. Don't forget to check for a blank field.

Steve
0 Kudos
Intel_C_Intel
Employee
418 Views
Thanks, I had been using '*', I had never used 'G' before.

David
0 Kudos
Jugoslav_Dujic
Valued Contributor II
418 Views
In addition, you could subclass the edit box to accept only numbers, decimal point, +, and - sign. That is not foolproof either (cannot prevent things like 1.234.5) but IMO looks more coherent to the user:

CASE (WM_KEYDOWN)
!Filter all but 1-9, +, -, and special keys:
SELECT CASE (wParam)
CASE (#30:#39, VK_NUMPAD0:VK_NUMPAD9, VK_DECIMAL, VK_END:VK_DOWN, VK_DELETE, VK_SUBTRACT, VK_ADD)
ResultProc=CallWindowProc(lpOldEditProc, hWnd, Msg, wParam, lParam)
CASE DEFAULT
ResultProc=0
END SELECT

Jugoslav
0 Kudos
isn-removed200637
418 Views
Here is something I programmed with the same idea in mind.
I knew that I would not be entering numbers such as 1.2345d-09
or -0.23456e+07 (i.e. no 'e,E, d or D in it).
BAsically I get the character string from the edit box and
then call a routine to ignore all but the first occurrence of - and '.'
with the '-' sign having to be the
first non-blank character to be accepted.
(INSIDE SOME CALLBACK ROUTINE...)
.....
.....
REAL*8 REALBOX4
CHARACTER*256 EDIT4
INTEGER*4 EDIT4LENG
.......
......
 CASE(IDC_EDITBOX4)
 	!GET THE NUMBER (CHARACTER STRING)
	retlog=DLGGET(dlg, IDC_EDITBOX4,EDIT4,DLG_STATE)
	EDIT4LENG=LEN_TRIM(EDIT4)
! call routine to filter the character string
	CALL FILTERSTRING(EDIT4,EDIT4LENG)
! Write out the filtered entry to the edit box
        retlog=DLGSET(dlg, IDC_EDITBOX4,TRIM(ADJUSTL(EDIT4)),DLG_STATE)
	REALBOX4=0
	IF(EDIT4LENG.GT.0)	READ(EDIT4,*) REALBOX4
!	WRITE(*,*) 'NUMBER 4 =',REALBOX4
 END SELECT
!
......
.....

SUBROUTINE FILTERSTRING(STRING,LEN)
CHARACTER*(LEN) STRING
CHARACTER*256 TEMP
INTEGER LEN,ICOUNT, I
!
! COPY A NON-ZERO LENGTH STRING TO ITSELF, REMOVING
! NON-NUMERIC CHARACTERS EXCEPT '-' (IF FIRST CHARACTER) 
! AND '.' IF IT IS THE FIRST OCCURRENCE OF '.'
!
! FILL TEMP WITH BLANKS
!
IF(LEN.EQ.0)RETURN
TEMP=' '
TEMP=ADJUSTR(ADJUSTL(TEMP))
ICOUNT=0
! INITIALISE FLAG USED TO DETECT FIRST OCCURRENCE OF A DECIMAL POINT
IDOT=0
DO I=1,LEN

IF(ICHAR(STRING(I:I)).GT.47.AND.ICHAR(STRING(I:I)).LT.58)THEN
ICOUNT=ICOUNT+1
IF(ICOUNT.LT.256+1)TEMP(ICOUNT:ICOUNT)=STRING(I:I)
ENDIF

IF(ICHAR(STRING(I:I)).EQ.45.AND.I.EQ.1)THEN
ICOUNT=ICOUNT+1
IF(ICOUNT.LT.256+1)TEMP(ICOUNT:ICOUNT)=STRING(I:I)
ENDIF

IF(ICHAR(STRING(I:I)).EQ.46.AND.IDOT.EQ.0) THEN
ICOUNT=ICOUNT+1
IF(ICOUNT.LT.256+1)TEMP(ICOUNT:ICOUNT)=STRING(I:I)
! SET FLAG TO SHOW DECIMAL POINT DETECTED - MAYBE MAKE
! USE OF IT SOME OTHER TIME...
IDOT=1
ENDIF

ENDDO

STRING=ADJUSTL(ADJUSTR(TEMP))
LEN=LEN_TRIM(STRING)
END SUBROUTINE FILTERSTRING
0 Kudos
Reply