May 11, 2024, 02:14:17 PM

News:

Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........


Edit Control and dots and more

Started by RitchieF, August 16, 2008, 11:51:34 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

RitchieF

Hi all,
trying to input numbers with decimal-dot into an edit-control seems not to work with @CTEDITNUMBER activated .

I found Paul's thread http://www.ionicwind.com/forums/index.php/topic,749.0.html
QuoteNumeric edit controls are numbers only.  It's a Windows feature.

If you want to create an edit control that handles decimal you'll need to create your own by deriving from the class, overriding WndProc and trapping WM_CHAR.
which I as hobbiest programmer don't really understand.

So I tried to create a solution which I will understand and the following code came up:

CONST EDIT_4 = 4
CONST EDIT_6 = 6
CONST EDIT_8 = 8
CONST EDIT_10 = 10
CONST EDIT_12 = 12
CONST EXIT = 100

def win as window
int i,id,Length,result,ID_Nxt_Ctrl
string test$
DEF Valid[13]:INT
'Fill the array with valid ASCII-values
'from 48 to 57 = 0 - 9, 43 is plus-sign, 45 is minus-sign, 46 is dot

Valid = 43,45,46,48,49,50,51,52,53,54,55,56,57

OPENWINDOW win,0,0,300,250,0x80C80080,0,"Edit-Controls with decimal-dot ",&win_handler
CENTERWINDOW win
CONTROL win,@EDIT,"",26,25,70,20,0x50810000|@CTEDITCENTER,EDIT_4
SETCONTROLNOTIFY(win,4,1,1)
CONTROL win,@EDIT,"",26,50,70,20,0x50810000|@CTEDITCENTER,EDIT_6
SETCONTROLNOTIFY(win,6,1,1)
CONTROL win,@EDIT,"",26,75,70,20,0x50810000|@CTEDITCENTER,EDIT_8
SETCONTROLNOTIFY(win,8,1,1)
CONTROL win,@EDIT,"",26,100,70,20,0x50810000|@CTEDITCENTER,EDIT_10
SETCONTROLNOTIFY(win,10,1,1)
CONTROL win,@EDIT,"",26,125,70,20,0x50810000|@CTEDITCENTER,EDIT_12
SETCONTROLNOTIFY(win,12,1,1)
CONTROL win,@BUTTON,"Exit",26,175,100,20,0,EXIT

run = 1

SETFOCUS win,4

WAITUNTIL run = 0
CLOSEWINDOW win

END

SUB win_handler
SELECT @MESSAGE
CASE @IDINITDIALOG
CENTERWINDOW win

CASE @IDCLOSEWINDOW
run = 0
CASE @IDCONTROL
SELECT @CONTROLID

CASE EDIT_4
SELECT @NOTIFYCODE
CASE @ENSETFOCUS

CASE @ENENTERKEY
ID = 4
ID_Nxt_Ctrl = 6
test$ = GETCONTROLTEXT (win,ID)
CheckForNonValidChar()
CASE @ENTABKEY
ID = 4
ID_Nxt_Ctrl = 6
test$ = GETCONTROLTEXT (win,ID)
CheckForNonValidChar()
ENDSELECT
CASE EDIT_6
SELECT @NOTIFYCODE
CASE @ENSETFOCUS

CASE @ENENTERKEY
ID = 6
ID_Nxt_Ctrl = 8
test$ = GETCONTROLTEXT (win,ID)
CheckForNonValidChar()
CASE @ENTABKEY
ID = 6
ID_Nxt_Ctrl = 8
test$ = GETCONTROLTEXT (win,ID)
CheckForNonValidChar()
ENDSELECT
CASE EDIT_8
SELECT @NOTIFYCODE
CASE @ENSETFOCUS

CASE @ENENTERKEY
ID = 8
ID_Nxt_Ctrl = 10
test$ = GETCONTROLTEXT (win,ID)
CheckForNonValidChar()
CASE @ENTABKEY
ID = 8
ID_Nxt_Ctrl = 10
test$ = GETCONTROLTEXT (win,ID)
CheckForNonValidChar()
ENDSELECT
CASE EDIT_10
SELECT @NOTIFYCODE
CASE @ENSETFOCUS

CASE @ENENTERKEY
ID = 10
ID_Nxt_Ctrl = 12
test$ = GETCONTROLTEXT (win,ID)
CheckForNonValidChar()
CASE @ENTABKEY
ID = 10
ID_Nxt_Ctrl = 12
test$ = GETCONTROLTEXT (win,ID)
CheckForNonValidChar()
ENDSELECT
CASE EDIT_12
SELECT @NOTIFYCODE
CASE @ENSETFOCUS

CASE @ENENTERKEY
ID = 12
ID_Nxt_Ctrl = 4
test$ = GETCONTROLTEXT (win,ID)
CheckForNonValidChar()
CASE @ENTABKEY
ID = 12
ID_Nxt_Ctrl = 4
test$ = GETCONTROLTEXT (win,ID)
CheckForNonValidChar()
ENDSELECT
CASE exit
run = 0
ENDSELECT
ENDSELECT
RETURN
ENDSUB

SUB validation(test as string),INT
Length = len(test)
int counter,result
counter = 0
for i = 1 to Length
'extract each element of the input-string
found = MID$ (test$, i ,1)
'compare it with the array-values
for k = 0 to 11
if found = CHR$(Valid[k]) then counter = counter + 1
next k
next i

if counter <> Length THEN ' if all elements are valid then the counter has the same value as the length
result = 1
ELSE
result = 0
ENDIF
RETURN result
RETURN -1:
ENDSUB

SUB CheckForNonValidChar
result = validation(test$)
if result = 1 THEN
MESSAGEBOX (win,"Non valid Input","Warning",@MB_OK|@MB_ICONEXCLAMATION)
SETCONTROLTEXT win,ID,""
SETFOCUS win,ID
ELSE
SetFocus win,ID_Nxt_Ctrl
ENDIF
ENDSUB



Will this work with every input or does anybody see a problem coming up ?

Thanks

Richard

pistol350

September 02, 2008, 04:56:36 AM #1 Last Edit: September 02, 2008, 04:58:14 AM by pistol350
The only problem i saw is about the character buffer defined as
DEF Valid[13]:INT

In order to make these given characters valid :
Quote'from 48 to 57 = 0 - 9, 43 is plus-sign, 45 is minus-sign, 46 is dot


Whereas in the test routine we have :

for i = 1 to Length
'extract each element of the input-string
found = MID$ (test$, i ,1)
'compare it with the array-values
for k = 0 to 11
if found = CHR$(Valid[k]) then counter = counter + 1
next k
next i


The "9" character will always be seen as non valid since the line for k = 0 to 11 tells the program to ignore the 13th character which is "9"
In fact the line should be changed to for k = 0 to 12
Regards,

Peter B.

RitchieF

Peter,

I added the plus-sign-value and forgot to increase the counter.

Thanks for telling me this

Richard

aurelCB

Just a small sugestion:
Better use if you wannt window in center of screen:
SELECT @MESSAGE
      CASE @IDCREATE
         CENTERWINDOW win
instead of:
OPENWINDOW win,0,0,300,250,0x80C80080,0,"Edit-Controls with decimal-dot ",&win_handler
'CENTERWINDOW win
regards
zlatko