May 24, 2024, 05:07:25 AM

News:

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


Enter Phone Numbers

Started by jay, October 24, 2007, 05:19:32 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

jay

Here is  a program I put together that will allow you to enter phone numbers in an edit box that accepts numbers only.  The program puts in the correct
spaces, dashes, and open and close brackets for the area code.  All you have to do is hit back space and delete.  Be careful when using delete because I
have not handled input errors for that.  The edit box only allows 18 characters for regular phone numbers meaning area code and then the number, and
it allow for 1 800 numbers.

Enjoy...



CONST EDIT_1 = 1
CONST STATIC_2 = 2
CONST EDIT_3 = 3
CONST STATIC_4 = 4


DEF StringCount, Infocus, BackspacePressed, DeletePressed:INT
DEF temp, oldtemp:STRING

DIALOG d1
CREATEDIALOG d1,0,0,300,134,0x80CA0080,0,"Enter Phone Number",&d1_handler
CONTROL d1,@EDIT,"Edit1",48,79,204,22,0x50812000,EDIT_1
CONTROL d1,@STATIC,"Phone Number Area Code First or Enter 1 for 800 Numbers",63,52,169,27,0x50000101,STATIC_2
CONTROL d1,@EDIT,"Edit2",206,19,46,22,0x50800800,EDIT_3
CONTROL d1,@STATIC,"String Count",144,25,60,16,0x5000010B,STATIC_4

run = 1
SHOWDIALOG d1

'process messages until run = 0
WAITUNTIL run = 0
run = 0
CLOSEDIALOG d1

END



SUB d1_handler
SELECT @MESSAGE
CASE @IDINITDIALOG
CENTERWINDOW d1
StringCount = 0
/* Initialize any controls here */
BackspacePressed  = 0
DeletePressed = 0
CONTROLCMD  d1, EDIT_1, @EDSETLIMITTEXT, 18
CASE @IDCLOSEWINDOW
run = 0
CLOSEDIALOG d1,@IDOK

CASE @IDKEYDOWN
IF GETKEYSTATE(0x2E) AND Infocus = 1: 'Delete Key
DeletePressed = 1
ENDIF

IF GETKEYSTATE(0x08) AND Infocus = 1 : 'Backspace Key
IF StringCount = 0
BackSpacePressed = 0
ELSE
BackspacePressed = 1
ENDIF
ENDIF

CASE @IDCONTROL
SELECT @CONTROLID
CASE EDIT_1
/* respond to edit notifications here */

SELECT @NOTIFYCODE
CASE @ENSETFOCUS
Infocus = 1

CASE @ENKILLFOCUS
Infocus = 0

CASE @ENUPDATE
SENDMESSAGE d1, @IDKEYDOWN, @WPARAM, @LPARAM
temp = GETCONTROLTEXT d1, EDIT_1
StringCount = LEN(temp)
SETCONTROLTEXT d1, EDIT_3, STR$(StringCount)

CASE @ENCHANGE
SENDMESSAGE d1, @IDKEYDOWN, @WPARAM, @LPARAM
IF BackspacePressed = 1 or DeletePressed = 1
BackspacePressed = 0
DeletePressed = 0
StringCount = LEN(temp)
ELSE
GOSUB Count
ENDIF

ENDSELECT
CASE EDIT_3
/* respond to edit notifications here */
ENDSELECT
ENDSELECT
RETURN
ENDSUB


SUB Count
temp = GETCONTROLTEXT d1, EDIT_1
bar = LEFT$(temp, 1)

'Maybe use a SELECT CASE ENDSELECT statement instead of all of the IF statements

IF bar = "1"
CONTROLCMD  d1, EDIT_1, @EDSETLIMITTEXT, 22
GOSUB Eighthundred
RETURN
ENDIF

IF StringCount > 0 AND StringCount < 2 AND bar <> "("
temp = APPEND$("(", temp)
CONTROLCMD  d1, EDIT_1, @EDSETLIMITTEXT, 18
SETCONTROLTEXT d1, EDIT_1, temp
CONTROLCMD d1, EDIT_1, @EDSETSELECTION, StringCount,StringCount
RETURN
ENDIF

IF StringCount = 4
REPLACE$ temp, 5, 4, ") - "
SETCONTROLTEXT d1, EDIT_1, temp
CONTROLCMD d1, EDIT_1, @EDSETSELECTION, 8,8
RETURN
ENDIF

IF StringCount = 5 or StringCount = 6 or StringCount = 7
ch = RIGHT$(temp, 1)
REPLACE$ temp, 5, 4, ") - "
temp = APPEND$(temp, ch)
SETCONTROLTEXT d1, EDIT_1, temp
CONTROLCMD d1, EDIT_1, @EDSETSELECTION, 9,9
RETURN
ENDIF
/*
IF StringCount = 7
ch = RIGHT$(temp, 1)
REPLACE$ temp, 5, 4, ") - "
temp = APPEND$(temp, ch)
SETCONTROLTEXT d1, EDIT_1, temp
CONTROLCMD d1, EDIT_1, @EDSETSELECTION, 9,9
RETURN
ENDIF
*/
IF StringCount = 8
ch = RIGHT$(temp, 1)
REPLACE$ temp, 5, 4, ") - "
IF ch <> " "
temp = APPEND$(temp, ch)
ENDIF
SETCONTROLTEXT d1, EDIT_1, temp
CONTROLCMD d1, EDIT_1, @EDSETSELECTION, 10,10
RETURN
ENDIF


IF StringCount = 11
REPLACE$ temp, 12, 3, " - "
SETCONTROLTEXT d1, EDIT_1, temp
CONTROLCMD d1, EDIT_1, @EDSETSELECTION, 14,14
RETURN
ENDIF

IF StringCount = 12
ch = RIGHT$(temp, 1)
REPLACE$ temp, 12, 3, " - "
temp = APPEND$(temp, ch)
SETCONTROLTEXT d1, EDIT_1, temp
CONTROLCMD d1, EDIT_1, @EDSETSELECTION, 15,15
RETURN
ENDIF

IF StringCount = 13
ch = RIGHT$(temp, 1)
REPLACE$ temp, 12, 3, " - "
temp = APPEND$(temp, ch)
SETCONTROLTEXT d1, EDIT_1, temp
CONTROLCMD d1, EDIT_1, @EDSETSELECTION, 15,15
RETURN
ENDIF

IF StringCount = 14
ch = RIGHT$(temp, 1)
REPLACE$ temp, 12, 3, " - "
IF ch <> " "
temp = APPEND$(temp, ch)
ENDIF
SETCONTROLTEXT d1, EDIT_1, temp
CONTROLCMD d1, EDIT_1, @EDSETSELECTION, 15,15
RETURN
ENDIF
ENDSUB



SUB Eighthundred
IF StringCount = 1
REPLACE$(temp,1,4, "1 - (")
SETCONTROLTEXT d1, EDIT_1, temp
CONTROLCMD d1, EDIT_1, @EDSETSELECTION, 5,5
RETURN
ENDIF

IF StringCount = 2
ch = RIGHT$(temp, 1)
REPLACE$(temp,2,5, " - (")
temp = APPEND$(temp, ch)
SETCONTROLTEXT d1, EDIT_1, temp
CONTROLCMD d1, EDIT_1, @EDSETSELECTION, 6,6
RETURN
ENDIF

IF StringCount = 3
ch = RIGHT$(temp, 1)
REPLACE$(temp,2,5, " - (")
temp = APPEND$(temp, ch)
SETCONTROLTEXT d1, EDIT_1, temp
CONTROLCMD d1, EDIT_1, @EDSETSELECTION, 6,6
RETURN
ENDIF

IF StringCount = 4
ch = RIGHT$(temp, 1)
REPLACE$(temp,2,5, " - (")
temp = APPEND$(temp, ch)
SETCONTROLTEXT d1, EDIT_1, temp
CONTROLCMD d1, EDIT_1, @EDSETSELECTION, 6,6
RETURN
ENDIF

IF StringCount = 5
ch = RIGHT$(temp, 1)
REPLACE$(temp,2,5, " - (")
temp = APPEND$(temp, ch)
SETCONTROLTEXT d1, EDIT_1, temp
CONTROLCMD d1, EDIT_1, @EDSETSELECTION, 6,6
RETURN
ENDIF

IF StringCount = 6
ch = RIGHT$(temp, 1)
REPLACE$(temp,2,5, " - (")
IF ch <> "(" AND ch <> " "
temp = APPEND$(temp, ch)
ENDIF
SETCONTROLTEXT d1, EDIT_1, temp
CONTROLCMD d1, EDIT_1, @EDSETSELECTION, 6,6
RETURN
ENDIF

IF StringCount = 8
REPLACE$ temp, 9, 11, ") - "
SETCONTROLTEXT d1, EDIT_1, temp
CONTROLCMD d1, EDIT_1, @EDSETSELECTION, 12,12
RETURN
ENDIF

IF StringCount = 9
ch = RIGHT$(temp, 1)
REPLACE$ temp, 9, 11, ") - "
temp = APPEND$(temp, ch)
SETCONTROLTEXT d1, EDIT_1, temp
CONTROLCMD d1, EDIT_1, @EDSETSELECTION, 13,13
RETURN
ENDIF

IF StringCount = 10
ch = RIGHT$(temp, 1)
REPLACE$ temp, 9, 11, ") - "
temp = APPEND$(temp, ch)
SETCONTROLTEXT d1, EDIT_1, temp
CONTROLCMD d1, EDIT_1, @EDSETSELECTION, 13,13
RETURN
ENDIF

IF StringCount = 11
ch = RIGHT$(temp, 1)
REPLACE$ temp, 9, 11, ") - "
temp = APPEND$(temp, ch)
SETCONTROLTEXT d1, EDIT_1, temp
CONTROLCMD d1, EDIT_1, @EDSETSELECTION, 13,13
RETURN
ENDIF

IF StringCount = 12
ch = RIGHT$(temp, 1)
REPLACE$ temp, 9, 11, ") - "
IF ch <> " "
temp = APPEND$(temp, ch)
ENDIF
SETCONTROLTEXT d1, EDIT_1, temp
CONTROLCMD d1, EDIT_1, @EDSETSELECTION, 13,13
RETURN
ENDIF

IF StringCount = 15
REPLACE$ temp, 16, 19, " - "
SETCONTROLTEXT d1, EDIT_1, temp
CONTROLCMD d1, EDIT_1, @EDSETSELECTION, 19,19
RETURN
ENDIF

IF StringCount = 16
ch = RIGHT$(temp, 1)
REPLACE$ temp, 16, 19, " - "
temp = APPEND$(temp, ch)
SETCONTROLTEXT d1, EDIT_1, temp
CONTROLCMD d1, EDIT_1, @EDSETSELECTION, 19,19
RETURN
ENDIF

IF StringCount = 17
ch = RIGHT$(temp, 1)
REPLACE$ temp, 16, 19, " - "
temp = APPEND$(temp, ch)
SETCONTROLTEXT d1, EDIT_1, temp
CONTROLCMD d1, EDIT_1, @EDSETSELECTION, 20,20
RETURN
ENDIF

IF StringCount = 18
ch = RIGHT$(temp, 1)
REPLACE$ temp, 16, 19, " - "
IF ch <> " "
temp = APPEND$(temp, ch)
ENDIF
SETCONTROLTEXT d1, EDIT_1, temp
CONTROLCMD d1, EDIT_1, @EDSETSELECTION, 20,20
RETURN
ENDIF
ENDSUB