## Phone Book v1

Expand Messages
• Small program, but one which posed a good challenge for me, and which ended up being one of my best. Henrik (and Sarmed), You Asked For It. ----code start
Message 1 of 1 , Dec 4, 2012
• 0 Attachment
Small program, but one which posed a good challenge for me, and which ended up being one of my best.

Henrik (and Sarmed),

'----code start
'kill DefaultDir\$ + "\phone.txt" :wait'for testing
nomainwin

'open datafile if not already existing
open DefaultDir\$ + "\phone.txt" for append as #1
lenFile=lof(#1)
close #1
if lenFile=0 then [ShowWin]

[start]
tot=0
open DefaultDir\$+"\phone.txt" for input as #1
while eof(#1)=0 :line input #1, line\$ :tot=tot+1 'tot records
wend
close #1

dim List\$(tot,2),name\$(tot),phon\$(tot) 'will be tot+1 on update
'
x=0
open DefaultDir\$+"\phone.txt" for input as #1
while eof(#1)=0
x=x+1 :line input #1,line\$
List\$(x,1)=trim\$(word\$(line\$,1,"="))
List\$(x,2)=trim\$(word\$(line\$,2,"="))
wend
close #1

sort List\$(),1,tot,1

for x=1 to tot
name\$(x)=List\$(x,1) :phon\$(x)=List\$(x,2)
next

[ShowWin]
WindowWidth = 475 : WindowHeight = 300
UpperLeftX=10 :UpperLeftY=10
Stylebits #w.nameList, 0,_CBS_DROPDOWN,0,0 'show List
Stylebits #w.phonList, 0,_CBS_DROPDOWN,0,0
TextboxColor\$="yellow"

statictext #w.nm, "Name",25, 50, 100, 15
statictext #w.ph, "Phone",250, 50, 100, 15

textbox #w.nameBox, 20,20,200,25
Listbox #w.nameList, name\$(),[nameSelected],20, 65, 200, 150
textbox #w.phonBox, 245,20,200,25
Listbox #w.phonList, phon\$(),[phonSelected],245, 65, 200, 150
Button #w.srch, "Search", [srch], UL, 20,230,70,25
Button #w.contSrch, "Continue Search", [contSrch], UL, 120,230,110,25

open "Phone Book" for dialog as #w 'for win?
#w, "font Arial 10" :#w "trapclose [quit]"
if tot=0 then #w.srch "!disable" :#w.contSrch "!disable"
#w.nameList,"singleclickselect" :#w.phonList,"singleclickselect"
#w.nameBox "No Entry" :#w.phonBox "Click [Search] or [Add] to begin"
wait

[nameSelected]
#w.nameList, "selectionindex? sel"
#w.phonList, "select ";List\$(sel,2)
#w.nameBox name\$(sel) :#w.phonBox phon\$(sel)
'select nothing, remove highlight
#w.nameList, "selectindex 0" :#w.phonList, "selectindex 0"
wait

[phonSelected]
#w.phonList, "selectionindex? sel"
#w.nameList, "select ";List\$(sel,1)
#w.nameBox name\$(sel) :#w.phonBox phon\$(sel)
#w.nameList, "selectindex 0" :#w.phonList, "selectindex 0"
wait

[srch]
srch\$=""
prompt "Search what?";srch\$ :srch\$=lower\$(srch\$)
#w.srch, "!disable":#w.contSrch, "!enable"
if srch\$="" then wait
fnd\$=""
for x=1 to tot: content\$=""
content\$=content\$+List\$(x,1)+List\$(x,2)+""
content\$=lower\$(content\$)
if instr(content\$,srch\$)>0 then 'if found
fnd\$="found"
#w.nameBox name\$(x) :#w.phonBox phon\$(x)
'select the string called name\$(x)and phon\$(x)
'so that List will vScroll if necessary, then...
'remv highlight
#w.nameList,"select ";name\$(x)
#w.phonList,"select ";phon\$(x)
#w.nameList, "selectindex 0" :#w.phonList, "selectindex 0"
wait
end if
[contSrch]
next
#w.srch,"!enable":#w.contSrch,"!disable"
wait

name\$="" :phon\$=""
prompt "Name?";name\$ :name\$=trim\$(name\$):if name\$="" then wait
prompt "Phone?";phon\$:phon\$=trim\$(phon\$):if phon\$="" then wait
gosub [upDate]
#w.nameBox name\$ :#w.phonBox phon\$
#w.nameList, "select ";name\$ :#w.phonList, "select ";phon\$
wait

[quit]
close #w
end
'========================================================================
[upDate]
tot=0
open "phone.txt" for input as #1 'open file to get current tot
while eof(#1)=0
line input #1, line\$ :tot=tot+1 'tot records
wend
close #1

tot=tot+1 :#w.srch "!enable" 'to add 1 more rec
dim List\$(tot,2),name\$(tot),phon\$(tot) 'redim

open "phone.txt" for append as #1 'add new rec at end of file
print #1, name\$+" = "+phon\$
close #1

open "phone.txt" for input as #1
for x=1 to tot
line input #1, line\$
List\$(x,1)=trim\$(word\$(line\$,1,"=")) 'create List\$ array
List\$(x,2)=trim\$(word\$(line\$,2,"="))
next
sort List\$(),1,tot,1
for x=1 to tot 'create other two arrays
name\$(x)=List\$(x,1) :phon\$(x)=List\$(x,2)
next
close #1

sort List\$(),1,tot,1

open "phone.txt" for output as #1 'deletes old datafile, creates new
for x=1 to tot
line\$=List\$(x,1)+" = "+List\$(x,2)
print #1, line\$ 'write to datafile, line by line
next
close #1

'open again to carry on, else must run from start
open "phone.txt" for input as #1
for x=1 to tot
line input #1, line\$
List\$(x,1)=trim\$(word\$(line\$,1,"=")) :name\$(x)=List\$(x,1)
List\$(x,2)=trim\$(word\$(line\$,2,"=")) :phon\$(x)=List\$(x,2)
next