Instruction and example testing lain /. audre' (elgium) mmar li 1urd (2emen) /im 3oo4 (nited &tates) Neti'en%%$ (nited &tates)
Advisors: lain /. audre' (elgium) *an Roper (ustralia) How to Gambas 3 – Building GUI Applications (this work) is licensed by Willy J.L. Raets under the 3reative 3ommons ttribution"Non3ommer5ial"No6erivs $.0 nported 7i5ense
*n short meaning that everyone 5an share and read this wor4 8or 8ree as is. 3ommer5ial use is not al" lowed neither is 5hanging the wor4. 2ou need to attribute in the manner re9uired by the li5ensor (see below). 3he54 the 8ull li5ense te:t te:t 8or a detailed e:planation. ;or 5onditions o8 use use prohibited by this li5ense 5onta5t the l i5ensor. T*& W=R1 *& !R=*6?6 N6?R T? T?R& =; T*& 3R?T*? 3==N& !7*3 7*3?N&? 7*3?N&? (A33!7A =R A7*3?N&?A). T*& W=R1 *& !R=T?3T?6 2 3=!2R*BT N6C=R =T?R !!7*37? 7W. N2 &? =; T*& W=R1 =T?R TN & T=R*D?6 N6?R T*& 7*3?N&? =R 3=!2R*BT 7W *& !R=*" *T?6. UNLESS OTERW!SE "UTU#LL$ #%REE& '$ TE #RT!ES !N WR!T!N% L!*ENSOR O++ERS T!S WOR, #S-!S #N& "#,ES NO RERESENT#T!ONS RERESENT#T!ONS OR W#RR#NT!ES O+ #N$ ,!N& *ON*ERN!N% T!S WOR, E RESS !"L!E& ST#TUTOR$ ST#TUTOR$ OR OTERW!SE !N*LU&!N% W!TOUT L!"!T#T!ON L!"!T#T!ON W#RR#NT!ES O+ T!TLE "ER*#NT!'!L!T$ "ER*#NT!'!L!T$ +!TN ESS +OR # #RT!*UL#R UROSE NON!N+R!N%E"ENT OR TE #'SEN*E O+ L#TENT L#TENT OR OTER &E+E*TS #**UR#*$ #**UR#*$ OR TE RESEN*E O+ #'SEN*E O+ ERRORS WETER OR NOT &!S*O/ER#'LE. SO"E JUR!S&!*T!ONS &O NOT #LLOW TE E*LUS!ON O+ !"L!E& W#RR#NT!ES SO SU* E*LUS!ON "#$ NOT #L$ TO $OU. +or 0ull license te1t 2isit3 htt4355creati2eco66ons.or75licenses5by-nc-nd58.95le7alcode
Attribution is required as follows: 'ow to !ambas " # $uilding !%I Applications' b& ill& (aets )http:**howtogambas+org,
Coding and testing +round 1,.............................. ......................................... ................. 37 %dapting) coding and and testing +round 2,............................................................... .......... 3%dding ne controls) controls) code and test test +round 3,.................................. ................... ......... ...... ... !1 %dd some some last controls) code and test test +round !,.......................... ..................... ........... ....... .... !6
7.5.1 Do you need a button /ho.............................................. .................................. ....... ...... . 53 7.5.2 Could i/itch format be more clear.......................................... .............. ............. ...... ............. ...... 53 7.5.3 /hould the button %dd be enabled if no %dd is chec&ed................... chec&ed......................... ............. .............. ............ ..... 5!
7.5.! /hould the button Clear be enabled if there is nothing to clear..................................5!
B.> #NSWERS 7.6.1 7.6.2 7.6.3 7.6.!
TO TE L#ST RE/!EW ...............................................................................................=<
Do you need a button /ho.............................................. .................................. .......5! Could i/itch format be more clear.......................................... ................................. 56 /hould the button %dd be enabled if no %dd is chec&ed............................................ 56 /hould the button Clear be enabled if there is nothing to clear..................................57
'OU! "CO)# A,,.ICATIO)/ T"0T,.A'T4W5O$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ *6 A.: W#T W!LL 'E &ONE !N T!S *#TER......................................................................................=@ A.; ST#RT # NEW ROJE*T #N& &ES!%N TE +!RST +OR"...................................................................=@ A.8 L#$!N% W!T TET TE OTER W#$ #ROUN&.............................................................................>9 -.3.1 /ome first coding to split the tet.................................... .......................... ................... 60 -.3.2 Determine the number of splits.......................................... ......................... ................. 6!
a&e a ne form.......................................... ........................................ ....................... 6a&e rm/earch4eplace open from btn/earch on rmain...................... ................70 #ro(ect intermeo orm class interaction eplained................................. ................. 71 ac& on trac& to code a #ublic method on rmain....................................... ............. 73 8ets search some string....................................... ......................................... ............... 7!
-.5.1 #repare the forms for /earch and 4eplace......................................... ......................... 76 -.5.2 a&ing rm/earch4eplace appear depending on selection in rmain.................... .77 -.5.3 4eplace searched strings....................................... .............................. ....................... -0
6 'OU! THI!# A,,.ICATIO)/ )U+,.A'$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$% @.: W#T W!LL 'E &ONE !N T!S *#TER......................................................................................A; @.; ST#RT # NEW ROJE*T #N& &ES!%N # +OR"...............................................................................A; @.8 &O!N% TE "#TS................................................................................................................A8 $.3.1 %nalyse the needs and determine a strategy................................ ...............................-! $.3.2 mplement error handling.................................................... ......................... ................ --
TE #N& T!"E...................................................................................................@=
$.5.1 %dding some code........................................ ......................................... ...................... $6 $.5.2 ore date and time............................................. ......................................... ................ $7
11.!.1 'n a mission.................................................. ......................................... ................. 120 11.!.2 8ets save some tet..................................... ......................................... ................... 121 11.!.3 ;et name and path to save to................................... ...................................... ......... 123
11.5.1 Code for opening........................................................ ............................................. 126 11.5.2 a&e sure an opened file can be saved................................... ............................... .127
::.> L!TTLE !NTER"ECCO..........................................................................................................:;A ::.B '#*, ON TR#*, +OR S#/E #S...........................................................................................:8; 11.7.1 Code /ave %s menu item........................................ ........................................ ......... 132 11.7.2 /implifying the code for saving......................................... ....................................... 133
.ist of Illust8ations !llustration :3 Or7anisation o0 D&e2elo46entD 0older...........................................................> !llustration ;3 Or7anisation o0 D&istributionsD 0older.............................................................A !llustration 83 #44lication 0older with 2ersion 0olders containin7 the 4acka7es.............. .....@ !llustration <3 Or7anisation o0 a 4roect 0older in the %a6bas !&E.....................................@ !llustration =3 %a6bas 8.<.: o4enin7 screen...................................................................:< !llustration >3 DNew 4roectD screen ste4 :. Select the ty4e o0 a44lication........................:= !llustration B3 DNew 4roectD screen ste4 ;. Select the 0older 0or the new 4roect..............:= !llustration A3 *reate a new 0older 0or Learnin7 4roects..................................................:> !llustration @3 Select the 0older D%a6bas8-Learnin7D........................................................:> !llustration :93 DNew 4roectD screen ste4 8. Na6e the 4roect 0older and 4roect ...........:B !llustration ::3 "essa7e when 4roect has been created success0ully............................:B !llustration :;3 New 4roect DTe1tlayD o4ened in %a6bas 8 !&E.....................................:A !llustration :83 +"ain in Sources 0older o0 the 4roect.....................................................:A !llustration :<3 E64ty 0or6 in !&E....................................................................................:@ !llustration :=3 Label icon in Tool'o1...............................................................................;9 !llustration :>3 Label control drawn on the 0or6..............................................................;9 !llustration :B3 Te1t'o1 icon in Tool'o1...........................................................................;9 !llustration :A3 Te1t'o1 control drawn on the 0or6...........................................................;: !llustration :@3 'utton icon in Tool'o1..............................................................................;: !llustration ;93 Two button controls drawn on the 0or6....................................................;: !llustration ;:3 Select #li7n6ent o0 the Label control.......................................................;; !llustration ;;3 Label: ali7ned ri7ht..................................................................................;8 !llustration ;83 Label: Te1t 4ro4erty chan7ed..................................................................;8 !llustration ;<3 *ontrols a0ter chan7in7 the 4ro4erties.....................................................;8 !llustration ;=3 Select E2ent *lick 0or button control........................................................;= !llustration ;>3 +"ain.class..............................................................................................;= !llustration ;B3 "enu &ebu7 -F Run to test the a44lication..............................................;> !llustration ;A3 #44lication runnin7 with 6essa7e shown.................................................;B !llustration ;@3 "essa7e when no na6e entered.............................................................;@ !llustration 893 "essa7e when na6e entered..................................................................;@ !llustration 8:3 %a6bas welco6e screen DRecent 4roectsD.............................................89 !llustration 8;3 "ouse a44earance will chan7e where the 4ointer is................................ 8: !llustration 883 +or6 dra77ed wider and Label and Te1t#rea selected............................8: !llustration 8<3 &ra7 the buttons to the botto6 o0 the 0or6..............................................8; !llustration 8=3 Result a0ter ada4tin7 the 0or6..................................................................8; !llustration 8>3 asted controls on the 0or6.....................................................................88 !llustration 8B3 *ontrols dra77ed to the s4ot you need the6...........................................88 !llustration 8A3 Result a0ter co4y54astin7 all needed controls...........................................88 !llustration 8@3 Label: a0ter chan7in7 the Te1t 4ro4erty...................................................8< !llustration <93 Na6e 4ro4erties o0 both Te1t'o1es chan7ed..........................................8< !llustration <:3 "ain 0or6 a0ter the chan7es in the e1ercise............................................8= !llustration <;3 Na6es o0 buttons chan7ed in +6ain.class as well..................................8> !llustration <83 "ain 0or6 a0ter addin7 two 6ore controls................................................8> !llustration <<3 Result a0ter clickin7 button #dd................................................................8B !llustration <=3 ierarchy o0 the controls on 6ain 0or6....................................................8A !llustration <>3 ierarchy a0ter ada4tation........................................................................8A !llustration !llustration =<3 Testin7 the new code...............................................................................<@ !llustration ==3 Selectin7 Radio'utton DShowD deselects Radio'utton D: lineD...................=9
!llustration =>3 Tool'o1 tab *ontainer showin7 +ra6e....................................................=9 !llustration =B3 +ra6e Show 4laced on 0or6....................................................................=: !llustration =A3 Radio'uttons 4asted into +ra6e Show....................................................=: !llustration =@3 #ll Radio'uttons 4laced in their +ra6es..................................................=; !llustration >93 Radio'uttons show workin7 as su44osed................................................=; !llustration >:3 No button Show but still doin7 the sa6e ob............................................== !llustration >;3 "ain 0or6 in !&E desi7n 6ode.................................................................>9 !llustration >83 "ain 0or6 when run 0ro6 !&E..................................................................>9 !llustration ><3 Te1t s4litted on the dot (.).........................................................................>; !llustration >=3 Result o0 s4littin7 on Gn.............................................................................>8 !llustration >>3 Renewed result o0 s4littin7 on HGnH...........................................................>< !llustration >B3 Result o0 s4littin7 on HGnH showin7 ; lines.................................................>= !llustration >A3 S4lit 0or < lines.........................................................................................>B !llustration >@3 Ne1t a s4lit 0or : line (other lines are now cleared)..................................>A !llustration B93 #d a new 0or6 to the 4roect....................................................................>A !llustration B:3 &ialo7 to na6e the 0or6...........................................................................>@ !llustration B;3 +r6SearchRe4lace with controls 4laced..................................................>@ !llustration B83 +r6SearchRe4lace o4ened 0ro6 btnSearch............................................B: !llustration B<3 Runnin7 btnS4lit code 0ro6 btnSearch on another 0or6..........................B; !llustration B=3 !nteraction between 6ethods in di00erent 0or6 classes............................B8 !llustration B>3 Search strin7 4assed as ar7u6ent o0 a ublic 6ethod............................B< !llustration BB3 See6s like search 4er0or6s as e14ected................................................B> !llustration BA3 Re4lace Te1t'o1 and 'utton added to +r6SearchRe4lace.....................B> !llustration B@3 +r6Search 4ro4erty Enabled set to true on +r6"ain..............................BB !llustration A93 Search shown when search selected.......................................................B@ !llustration A:3 Search and Re4lace shown when search5re4lace selected.....................B@ !llustration A;3 Search still works well..............................................................................A: !llustration A83 Re4lace does a 7ood ob as well.............................................................A: !llustration A<3 +r6"ain a0ter 4lacin7 all controls............................................................A8 !llustration A=3 +r6"ain when run...................................................................................A8 !llustration A>3 lblO4erator shows selected O4erator.......................................................A< !llustration AB3 *onsole out4ut in !&E..............................................................................A> !llustration AA3 *alculatin7 2alues :9 and ;.....................................................................AB !llustration A@3 &i2ision by Iero error...............................................................................AB !llustration @93 Error ;> returned.....................................................................................A@ !llustration @:3 &i2ision by Iero now handled 4ro4erly.....................................................@9 !llustration @;3 +r6"ain a0ter addin7 so6e controls........................................................@9 !llustration @83 *alculatin7 the te1t see6s to work..........................................................@: !llustration @<3 &i2ision by Iero taken care o00.................................................................@< !llustration @=3 Unknown o4erator taken care o00.............................................................@< !llustration @>3 So6e e1tra controls added to 4lay with date and ti6e.............................@> !llustration @B3 #0ter &ate is selected the other controls are u4dated accordin7ly...........@B !llustration @A3 Ti6er obect can be 0ound in the toolbo1 on tab DS4ecialD.............. ...........@A !llustration @@3 Ti6er obect can be anywhere on the 0or6..............................................@A !llustration :993 E2ery second the tb1&ate: and ; chan7e their content.........................@@ !llustration :9:3 *ontrols 4laced on +r6"ain................................................................:9; !llustration :9;3 *reate a new 0older D!6a7esD in the 4roect &ata 0older........................:98 !llustration :983 "ade so6e 6odern art i6a7e.............................................................:98 !llustration :9<3 !6a7e co4ied edited and sa2ed. That 6akes two works o0 art............:9< !llustration :9=3 "ake 4anel a44ear..............................................................................:9< !llustration :9>3 O4enin7 the DSelect a 4ictureD 0or6......................................................:9= !llustration :9B3 !6a7e D"y!6a7e.4n7D in icture'o1 4icOne.........................................:9= !llustration :9A3 'oth i6a7es in their icture'o1...........................................................:9= !llustration :9@3 Runnin7 !6a7elay 0or the 0irst ti6e....................................................:9> !llustration ::93 *lickin7 the button switches the i6a7es between the icture'o1es.. ..:9B !llustration :::3 Labels now showin7 the i6a7e di6ensions.........................................:9A
!llustration ::;3 Two 6ore 4n7 i6a7es 6ade in the editor............................................:9A !llustration ::83 Runnin7 the a44lication with the new i6a7es......................................:9@ !llustration ::<3 !6a7es switch but labels show wron7 di6ensions...............................:9@ !llustration ::=3 #ll works as e14ected..........................................................................::9 !llustration ::>3 #ll new controls 4laced on +r6"ain.....................................................::: !llustration ::B3 roect 0older structure.........................................................................::; !llustration ::A3 #uto-co64lete 0unction o0 !&E will reco7nise the 4ath.........................::; !llustration ::@3 The 4ictures see6 to load but the labels arenDt ada4ted......................::8 !llustration :;93 Loadin7 i6a7es now ada4ts the Labels accordin7ly............................::< !llustration :;:3 +r6"ain a0ter 4lacin7 the control.........................................................::= !llustration :;;3 Select "enu editor...............................................................................::> !llustration :;83 #ddin7 @ 6enu entries in the "enu editor............................................::> !llustration :;<3 !ndent the 6enu entry..........................................................................::B !llustration :;=3 The end result o0 editin7 the 6enu entries...........................................::B !llustration :;>3 There is a 6enu +ile on +r6"ain in !&E..............................................::A !llustration :;B3 'e0ore and a0ter clickin7 6enu +ile -F New.........................................::A !llustration :;A3 Set the keyboard shortcut 0or 6enu New.............................................::@ !llustration :;@3 Selectin7 the new icon.........................................................................::@ !llustration :893 +ile 6enu with the newly added icon 0or New......................................:;9 !llustration :8:3 "ake sure your 6enu entry 0or Sa2e has an icon and keyboard entry :;: !llustration :8;3 Ready to sa2e 6y 0irst 0ile....................................................................:;< !llustration :883 &ialo7 Sa2e te1t 0ile.............................................................................:;= !llustration :8<3 Noti0ication that the 0ile has been sa2ed...............................................:;= !llustration :8=3 &ialo7 O4en te1t 0ile.............................................................................:;B !llustration :8>3 Sa2ed the chan7es to the te1t 0ile........................................................:;A !llustration :8B3 art o0 the +ile 6enu disabled as should be........................................:;@ !llustration :8A3 Oo4s.. no way to sa2e a new te1t 0ile..................................................:89 !llustration :8@3 The end result o0 so6e 4olishin7 o0 the +ile 6enu...............................:8: !llustration :<93 Sa2e #s dialo7.....................................................................................:8; !llustration :<:3 Noti0ication that the 0ile was sa2ed usin7 a new na6e.........................:88 !llustration :<;3 The auto-hel4 will know your new routine............................................:8< !llustration :<83 *losin7 a0ter editin7 7i2es a warnin7 and o4tion to sa2e.....................:8A !llustration :<<3 *han7ed shown in *onsole 6eans *han7e e2ent is tri77ered......... ...:8@ !llustration :<=3 &ra77in7 the window bi77er doesnDt chan7e the Te1t#rea...................:<: !llustration :<>3 Set the #rran7e6ent 4ro4erty o0 +r6"ain...........................................:<: !llustration : !llustration :==3 Toolbar with 4ro4erty S4acin7 set to +alse..........................................:3 Toolbar with 4ro4erty S4acin7 set to True............................................:93 With Tool'ar checked..........................................................................:<@ !llustration :>:3 With Toolbar unchecked.......................................................................:=9 !llustration :>;3 "enu el4 -F #bout shows so6e in0o on the a44lication.................. ...:=9 !llustration :>83 DNew te1t docu6entD added to a44lication ca4tion...............................:=: !llustration :><3 *a4tion showin7 the na6e o0 the o4ened te1t 0ile...............................:=; !llustration :>=3 Sa2e the new te1t docu6ent................................................................:=8 !llustration :>>3 #0ter sa2in7 the ca4tion chan7ed.........................................................:=8 !llustration :>B3 +r6"ain.class code or7anised in certain order...................................:=<
!llustration :>A3 %a6bas website - "enu "ailin7 lists5+oru6s..........................................!! !llustration :>@3 %a6bas website - O2er2iew "ailin7 Lists +oru6s...............................!! !llustration :B93 Subscribe to the %a6bas-user 6ailin7list...............................................!!! !llustration :B:3 %a6bas 6ailin7 list +oru6.....................................................................!/ !llustration :B;3 %a6bas !&E - Syste6 in0or6ation 0or6................................................../ !llustration :B83 White !sland and %a6bas+oru6 (circled the lo7in and oin)................../! !llustration :B<3 Search ost since last 2isit and To4ics with unread 4osts..................../!! !llustration :B=3 %a6bas based 4roects with sub 0oru6 Dow to %a6bas 8D................/!!! !llustration :B>3 Sub 0oru6s and to4ics o0 Dow to %a6bas 8D 0oru6...............................! !llustration :BB3 To4ics in sub 0oru6 D'uildin7 %U! #44licationsD......................................! !llustration :BA3 %a6bas Learnin7 - a learnin7 aid 0or %a6bas........................................
Appendices #44endi1 !3 Linu1 co66and re0erence...............................................................................! #44endi1 !!3 Subscribe to the o00icial %a6bas 6ailin7 lists5+oru6....................................!! #44endi1 !!!3 Re4ortin7 4roble6s on the o00icial %a6bas 6ailin7 list................................/ #44endi1 !/3 White !sland So0tware and %a6bas+oru6 Kuick tour................................./! #44endi1 /3 %ettin7 hel4 with the Dow to %a6bas 8D 7uides......................................./!!! #44endi1 /!3 #44lication %a6bas Learnin7 a learnin7 aid 0or %a6bas......................... #44endi1 /!!3 O2er2iew o0 the Dow To %a6bas 8D 7uides..............................................!
1. How this got started
1 How t9is got sta8ted So6ewhere in ;99@ ! 0inally decided to install Linu1 on one o0 6y syste6s. #0ter 6y 0irst initial e14lorin7 o0 the o4eratin7 syste6s the 4ro2ided a44lications and 6ore a new world o4ened u4 to 6e. ! soon disco2ered the stability and 4ower o0 Linu1 and 6oreo2er the 4ower o0 an o4en source co66unity. So a0ter e14lorin7 Linu1 a while ! 0elt like 4ro7ra66in7. Since 6ost o0 6y last and current work in2ol2ed 4ro7ra66in7 client inter0aces 0or databases in /' ! went lookin7 0or a 7ood alternati2e on the Linu1 4lat0or6. #nd then ! 0ound %a6bas (at that ti6e in 2ersion ;) and lo2ed it ri7ht 0ro6 the start. ! tDs ease o0 use and lo7ic in synta1 sur4rised 6e ca4tured 6e and ne2er let 6e 7o. Since that day all 6y codin7 that is not ob related is done in %a6bas :. +or me as a 4ro0essional a44lication de2elo4er 0indin7 6y way in %a6bas wasnDt always that easy. &ocu6entation is 2ery basic and e14lains the synta1 o0 the lan7ua7e with little or no e1a64les. There are so6e 7uides out there but they are scares. Ne2ertheless ! 6ana7e because o0 6y years o0 e14erience with 4ro7ra66in7. ! can see how a new 4ro7ra66er 6i7ht 0ind it hard to understand how %a6bas and Linu1 work then lea2e in disa44oint6ent and 0rustration. 'ack in the old days about e2ery co64uter had a 4ro7ra66in7 lan7ua7e and 6anual deli2ered alon7 with it. # lot o0 youn7 4eo4le in those days learned 4ro7ra66in7 this way and %a6bas is an e1cellent tool to do ust that on a Linu1 4lat0or6. #s it is based on '#S!* ( Be7inners All 4ur4ose y6bolic Instruction Code) a lan7ua7e 6any in the ei7hties learned the6sel2es it is 4er0ect 0or a be7inner to learn 4ro7ra66in7. #lthou7h a lot has chan7ed since 'asic saw birth it still is the sa6e lan7ua7e but 6uch 6ore 4ower0ul because o0 %a6bas 4ro2idin7 an en2iron6ent to 6ake e2ent dri2en %U! (Gra4hical User Inter0ace) a44lications. !tDs 4ossible to build easy %U! a44lications doin7 si64le thin7s in no ti6e. #nd that is what this series ai6s at. eo4le new to 4ro7ra66in7 and wantin7 to learn the skills. So that is what will be co2ered in a si64le 6anner so any newbie can learn. E2ery 4art o0 the series will co2er a s4eci0ic to4ic. #nd in ti6e you will ha2e all the basic skills to build your own a44lications in %a6bas 8. Enoy... Willy Raets "ain author "ay ;9:;
# s4ecial thanks to all the contributors on the White !sland So0tware and %a6bas+oru6 0or their hel4 in i64ro2in7 this 7uide and o0 course all de2elo4ers o0 %a6bas 0or their e00ort s4ent on creatin7 the 4ower0ul 'asic lan7ua7e na6ed %a6bas. This 7uide is dedicated to an old 0riend Jos (a.k.a. $ellow6oor) to hel4 hi6 7et started with %a6bas 4ro7ra66in7. ! would like to dedicated the Dow To %a6bas 8D-series to 6y three sons Nicky Luca and Nino. 1
Meanwhile Gambas has been introduced at my job and is being used there as well. The environment has gone from Windows only to a mixed Windows/inux one.!added "e#tember $%th& $'1%(
1
2. Foreword
% &o8ewo8d This 7uide is the second in the series Dow To %a6bas 8D. The 7uide is ai6ed at 4eo4le new to %a6bas and 4ro7ra66in7 and discusses the buildin7 o0 si64le standalone %U! a44lications. This 4re-release : is a 0irst 4ublic release o0 a still un0inished 7uide. +act that it isnDt 0inished yet doesnDt 6ean it canDt 4ro2e use0ul to 4eo4le new to 4ro7ra66in7 in %a6bas. The 7uide will learn you the basics o0 6akin7 new %U! 4roects in the %a6bas !&E. &rawin7 controls on 0or6s learn to code their e2ents and run the 4ro7ra6. !t will e14lain ter6inolo7y co66on to 4ro7ra66in7 and %a6bas in s4eci0ic. $ou will learn about user interaction in4ut and out4ut o0 data handlin7 te1t-based and nu6eric data localisation the basics o0 loadin7 and sa2in7 data to 0iles and o0 handlin7 i6a7es in your a44lication. 'esides that %a6bas synta1 and use0ul strin7 arith6etical and other 0unctions are e1 4lained and de6onstrated. #ll is learned in a ste4 by ste4 a44roach e14lainin7 each and e2ery new detail acco64anied by code screenshots ; and hands on 4ractice in usin7 the %a6bas !&E ty4in7 code co64ilin7 and runnin7 code and see thin7s work (or 0ail). O0ten e1a64les are a44roached in a 6anner that letDs you stu6ble u4on 4roble6s that 7radually 7et sol2ed. This will slowly 7et you acKuainted with detectin7 4roble6s di00erent 6anners o0 sol2in7 the sa6e 4roble6 and a broader insi7ht in the %a6bas lan7ua7e in 7eneral. #t the end o0 4re-release : you should be ca4able o0 buildin7 2ery basic %U! a44lications handlin7 te1t and nu6bers sa2e and load 0iles and i6a7es workin7 with 6enus and toolbars and ha2e a basic understandin7 o0 conce4ts like ublic ri2ate 6ethods 0unctions obects controls and such. !n the cha4ters still in de2elo46ent thin7s like 4rintin7 data drawin7 on a &raw#rea 6ore co64le1 sa2in7 and loadin7 o0 data 0or6 classes 6odules the %a6bas !&E and itDs 4ossibilities 0older structures o0 4roects %a6bas co64onents and their 0unction and 6aybe e2en 6ore will be e14lained. +or now enoy 4re-release : Willy Raets Se4te6ber ;Ath ;9:8
Practical note: When you co4y54aste code 0ro6 the code bo1es into the %a6bas !&E you 6i7ht 7et errors runnin7 the code. To 4re2ent this 0ro6 ha44enin7 si64ly co4y54aste the code into a 4lain te1t editor 0irst and ne1t co4y54aste 0ro6 the te1t editor into the %a6bas !&E and you wonDt ha2e this trouble. 'etter 4ractice is to si64ly ty4e the code directly into the %a6bas !&E as this will 7et you 0a 6iliar with ty4in7 code in the editor and workin7 with the auto-hel4 0eature durin7 ty4in7. $
)ll screenshots are made on inux Mint 1% * Mate 1.+ with ,nglish language installed using Gambas %.+.$& exce#t otherwise mentioned. "creenshots might loo different on your distribution and with your localisation settings.
2
3. Before you get started
3 Befo8e :ou get sta8ted $ou will need to ha2e at least %a6bas 8.8.1 or hi7her installed on your syste6 8. !nstallin7 %a6bas will not be co2ered in this book. Dow to %a6bas 8 !nstallin7 %a6basD co2ers the installation o0 %a6bas in detail and 0or di00erent distributions. $ou will also need to know so6e con2entions used in t his book 0or clear understandin7 o0 the 6aterials 4resented herein.
3.1 Background information When e14lainin7 how to built and code in %a6bas 8 ! will so6eti6es 4ro2ide e1tra back7round. This e1tra back7round is 6eant to 7i2e so6e dee4er insi7ht into the world o0 both Linu1 and %a6bas. This back7round is not necessary 0or co64letin7 the e1a64les but will 7i2e you a better co64rehension o0 %a6bas in 7eneral. This is how back7round in0or6ation is 4resented in this book. !t will 7i2e you so6e 6ore insi7ht on the ite6 bein7 e14lained. 'ack7round in0or6ation will be 4laced in a yellow bo1.
3.2 Example code E1a64le code is used to e14lain %a6bas usin7 sa64le a44lications that 7et built u4 and e14lained in detail 0ro6 the scratch. DSa64le code D ublic Sub +or6MO4en() "e.*enter "e.*a4tion Sa64le a44licationP End The %a6bas code that you need to write in the e1a64les will be 4laced in a white bo1.
3.3 Good Practice !0 so6ethin7 is considered as a 7ood 4ractice it will be 4laced in a 7reen bo1. This is how 7ood 4ractice in0or6ation is 4resented in this book. !t is ad2ised to start usin7 this 7ood 4ractice in a44lications you built. %ood 4ractice will 6ake 0or 6ore e00ecti2e 6aintenance o0 your syste6 or a44lications.
3.4 Terminal !0 you would e2er need to run a co66and in a ter6inal window it will be 4laced in a black bo1. This 6eans you will need to o4en a ter6inal inter0ace like %no6e ter6inal L ter6inal or whate2er a44lication lets you 7o to the co66and line inter0ace on your distribution. %
ode will most liely wor as well in Gambas %.1.x or higher& although this was not tested. ust give it a try if that is your situation& since nothing 0 now of has changed in the syntax and use of the core Gambas com#onents discussed and used in this guide.
3
3. Before you get started
una6e -a
3.5 Warning !0 it would e2er be needed to 7et your attention to 6atters that are o0 7reat i64ortance they will be 4laced in a red bo1. This is an i64ortant 4rocedure. ay attention as thin7s can 7o seriously wron7 3-)
3. !ontrol" and menu" When testin7 the a44lications built in %a6bas you will need to do thin7s like click on a button to see i0 the code does what it is su44osed to do. When 0or e1a64le re0errin7 to the button with *ancel on it ! will re0er to it as3 *lick on the button Cancel When 0or e1a64le re0errin7 to a 6enu #bout... in the 6enu el4 ! will re0er to it as3 *lick on 6enu Help ; About$$$
4
4. Introduction
( Int8oduction 4.1 W#at i" Gam$a"% #s the %a6bas website states3 D%a6bas al6ost 6eans 'asicD. With the D GD 0or %a6bas the Da D 0or al6ost the DmD 0or 6eans and the D basD 0or 'asic. # nice little 4layin7 on words. #ccordin7 to the o00icial website D Gam$a" is a 0ree de2elo46ent en2iron6ent based on a a= sic inter4reter with obect e1tensionsD. !t is co64arable to a 4o4ular counter4art on an 6ainstrea6 4lat0or6. The 4erson behind the 4roect and 6ain de2elo4er is 'enoQt "inisini 0ro6 +rance with the hel4 o0 others. %a6bas 4ro2ides you with an !&E 0or easy de2elo46ent o0 %U! a44lications. %a6bas co64onents 4ro2ide you with e1tra synta1 and 0 unctionality to code 0or thin7s like database access networkin7 access *%! web a44lications internationalisation and 6ore. /isit the %a6bas !ntroduction at the o00icial %a6bas website 0or 6ore details. < !&E or Inte7rated #e2elo46ent "n2iron6ent is a so0tware a44lication that 4ro2ides co64rehensi2e 0acilities to co64uter 4ro7ra66ers 0or so0tware de2elo46ent. : !n %a6bas this 6eans you ha2e an en2iron6ent where you can easily desi7n your 0or6s 4ut controls on the6 (like buttons te1t 0ields check bo1es and so on) and code the6 in a source code window. $ou 7et a tree 2iew on the 4roect so you can easily browse 0ro6 one 0or6 desi7n to another and see other 0iles like icons contained in the 4roect. The %a6bas !&E also has a debu77er so you can run a44lications in de2elo46ent ti6e without ha2in7 to 6ake e1ecutables or distribution 4acka7es a thin7 the ! &E does as well. $ou can add or re6o2e co64onents to the 4roect in the !&E and in this way add or re6o2e 0unctionality to your a44lications. :. Source3 htt4355en.wiki4edia.or75wiki5!nte7ratedMde2elo46entMen2iron6ent
%a6bas 4ro2ides you with the tools 0or R#& ( !a4id A44lication #e2elo46ent) throu7h use o0 the !&E. This 6akes %U! a44lication desi7n debu77in7 and 4acka7in7 0or distribution easy and by %a6bas su44ortin7 OO (see below in yellow) a conce4t that 6akes reuse o0 4arts already built 4ossible sa2in7 ti6e ener7y and reducin7 errors. No need to do thin7s o2er and o2er a7ain. Obect-oriented 4ro7ra66in7 (OO) is a 4ro7ra66in7 4aradi76 usin7 HobectsH data structures consistin7 o0 data 0ields and 6ethods t o7ether with their interactions to desi7n a44lications and co64uter 4ro7ra6s. ro7ra66in7 techniKues 6ay include 0eatures such as data abstraction enca4sulation 6essa7in7 6odularity 4oly6or4his6 and inheritance. "any 6odern 4ro7ra66in7 lan7ua7es now su44ort OO at least as an o4tion : :. Source3 htt4355en.wiki4edia.or75wiki5Obect-orientedM4ro7ra66in7
+
htt#//gambas.sourceforge.net
5
5. Some good practices
* ome good p8actices !0 you de2elo4 a44lications there are so6e 7ood 4ractices that can 6ake your li0e easier. !Dll try to 7i2e an o2er2iew on how ! or7anise 6y 4roects code and 6ore 0or %a6bas. This is the way ! ha2e done it 0or 6y own needs and it is a sort o0 e2olution that took 4lace o2er the years o0 4ro7ra66in7 in di00erent lan7ua7es. This doesnDt ha2e to be how you want to or7anise your 4roects code and 6ore it is ust a su77estion that could 4ro2e use0ul.
5.1 &older organi"ation This is 4robably not the 0irst thin7 6ost o0 you would think about but to 6e it is an i64ortant one es4ecially when ha2in7 se2eral 4roects runnin7. ere is how ! or7anise 6y 0olders 0or %a6bas de2elo46ent. ! de2elo4 in both %a6bas ; and 8. $ou can do it di00erently when only de2elo4in7 in %a6bas 8. +irst ! create ; 0olders in 6y ho6e 0older or in a 0older 6ade 0or it. One is called D&e2elo46entD the other D&istributionsD. This you can si64ly do 0ro6 within your +ile "ana7er a44lication belonin7 to your Linu1 distribution.
*$1$1 #e
Illustration 1: Organisation of 'Development' folder
6
5. Some good practices
1$ #44lication docu6entation This 0older is used to 4lace docu6entation 0iles 6ade to acco64any a44lications ! distribute.
%$ %a6bas8-#rchi2ed#44s #rchi2ed a44lications can be any a44lication ! ha2enDt looked at 0or a lon7 ti6e. ! ne2er trow the6 away but relocate the6 to this 0older. They can so6eti6es still be use0ul ust to re6e6ber how you done so6ethin7 in those days or as a base 0or an entire new a44lication.
3$ %a6bas8-%a6eCone The 0older ! store 6y %a6bas 7a6e 4roects
($ %a6bas8-roects The 0older ! use 0or all 6y distributed a44lication 4roects. Each 4roect will 7et his own 0older na6ed a0ter the 4roect. This will be done when creatin7 a new 4roect in the %a6bas !&E.
*$ %a6bas8-SourceOthers !n this 0older ! un4ack 4roect source code 0ro6 other de2elo4ers they shared. ! can then o4en the6 as nor6al 4roects in the %a6bas !&E. !t could be 0or 4ur4ose o0 checkin7 out code testin7 the a44lication or hel4 sol2in7 an error. When not use0ul they 7et deleted instead o0 archi2ed.
-$ %a6bas8-TestCone This 0older is 0or 6akin7 a Kuick 4roect 0or testin7 so6ethin7 or 0or tryin7 out thin7s or 0or re4licatin7 a Kuestion asked on a 0oru6 to see i0 ! can co6e u4 with a solution. They are no serious 4roects but e1actly what the 0older says test Ione a44lications. They o0ten 7et deleted instead or archi2ed.
$ %a6bas8-Tools The 0older ! use 0or a44lications (libraries) ! de2elo4 0or use in 6y distributed 4roects or 0or tools ! use 6ysel0. $ou can 6ake 6ore or less 0olders to cate7orise and or7anise your 4roects on your syste6. $ou can add new 0olders later and re4lace 4roects. +or the 4roects you will be 6akin7 in this 7uide you could 6ake a D%a6bas8-Learnin7ConeD 0older to 4ut the6 in. #ll that is u4 to you.
*$1$% #ist8ibutions folde8 This 0older contains the distribution 4acka7es 0or all 6y a44lications. !n 6y case in the D&istributionsD 0older ! create a D%a6bas;D and a D%a6bas8D 0older. Within the D%a6bas8D 0older ! create 0ollowin7 0olders3 #rchi2ed ➢ Website ➢ Cone-%a6es ➢ Cone-owToE1a64les ➢ Cone-!deas ➢ Cone-Libraries ➢ Cone-"yTools ➢ Cone-acka7e!nstallers ➢ Cone-roects ➢ Cone-Testin7 ➢
1$ #rchi2ed 0older The D#rchi2edD 0older contains distribution 4acka7es o0 a44lications no lon7er 6aintained.
%$ Website 0older 7
5. Some good practices
Illustration 2: Organisation of 'Distributions' folder
The DWebsiteD 0older contains in0or6ation 0or website o0 the distributed a44lications and thin7s like online 2ersion 0iles data 0iles belonin7 to certain a44lications and so on
3$ Cone 0olders Within the di00erent DConeD 0olders there are 0olders na6ed a0ter the a44lication they contain distribution 4acka7es 0or. Within the a44lication 0older there is a 0older na6ed a0ter each 2ersion release nu6ber containin73 # source 4acka7e o0 that 2ersion o0 the a44lication ➢ # 0older 0or each distribution an installation 4acka7e is 6ade 0or ➢ (!0 needed) a 7a6bas e1ecutable o0 that 2ersion o0 the a44lication ➢
1$ Source 4acka7e o0 that 2ersion o0 the a44lication The source 4acka7es will enable you to re2ert back t o a 4re2ious 2ersion o0 the a44lication.
%$ # 0older 0or each distribution an installation 4acka7e is 6ade 0or The distribution s4eci0ic 0olders will be 6ade by the %a6bas !&E u4on 6akin7 the 4acka7es i0 you select so. They will contain both installation and source 4acka7es s4eci0ic to that distribution.
3$ # 7a6bas e1ecutable o0 that 2ersion o0 the a44lication E1ecutables 6ake 0or easy Kuick testin7 by other de2elo4ers or in a 2irtual 6achine. They are not always reKuired but co6e in handy in so6e situations.
8
5. Some good practices
Illustration : !pplication folder "ith version folders containing the pac#ages
Note that in %a6bas 8 you can 6ake 4acka7es 0or 6ore than ust the distributions shown abo2e.
5.2 Pro'ect organi"ation Within the %a6bas !&E you can also work on or7anisin7 0or your own bene0it. !t is 7ood 4ractice to 6ake 0olders in there that contain s4eci0ic data like a 0older 0or i6a7es or icons. This can all be done 0ro6 within the %a6bas !&E.
Illustration 4: Organisation of a project folder in the Gambas IDE
ow this all is done will be discussed later in this 7uide.
5.3 (rgani"ing )our code One 2ery i64ortant as4ect o0 buildin7 a44lications is to 6ake sure you still understand what you where doin7 a year later when you want to chan7e 4arts o0 it or (re)use the6 so6ewhere else.
9
5. Some good practices
The other thin7 is that i0 you built your a44lications o4en source it 6akes the code 6ore readable to those lookin7 at it.
*$3$1 Commenting :ou8 code # 0irst 7ood start is by 4uttin7 co66ents into your code. *o66entin7 in %a6bas is easy. Just start the line with D What could you co66ent about. Well this is what ! tend to do3 !denti0y each class and its 0unction ➢ &escribe the 0unctionality o0 each block o0 code in one line o0 co66ent ➢ Write co66ents at 4arts that need i64ro2e6ent (includin7 ideas on i64ro2e6ent) ➢
1$ !denti0y each class and its 0unction This is an e1a64le o0 the identi0ication co66ent in a class in one o0 6y 4roects. D %a6bas class 0ile D D --------------------------------------------------D #44lication3 Sys!n0o D *lass3 Sys!n0o D +unction3 Library 0or syste6 in0or6ation retrie2al D #uthor3 W.J.L. Raets D %NU %eneral ublic Licence - /ersion 8 D --------------------------------------------------$ou could include 6ore in there like 6aintenance or re2isions you do in the code and the date you did the6. This is es4ecially use0ul in 4roects where 6ore that one 4ersons works on the sa6e 4roect. This way all de2elo4ers in2ol2ed will ha2e an idea on what has chan7ed. 'ut e2en in your own 4ersonal 4roects you 6i7ht be ha44y so6eday when lookin7 back at year old code 0or ha2in7 done this.
%$ &escribe the 0unctionality o0 each block o0 code in one line o0 co66ent This a7ain is an e1a64le o0 describin7 blocks o0 code by 0unctionality in one o0 6y 4roects (see ne1t 4a7e). The (rest o0 code) blocks are Kuite lon7 4ieces o0 code so ! le0t the6 out. Without these si64le co66ents di2idin7 the code into 4arts that are coded in that s4eci0ic area ! would 7et lost in 6y own code. !n the e1a64le on the ne1t 4a7e ! ha2e 6ade the co66ents bold. So donDt be s4arse with co66entin7 your a44lication.
3$ Write co66ents at 4arts that need i64ro2e6ent #7ain this will only hel4 you. ! ha2e had to deal with code that worked on certain distributions and 7a2e errors on others. 'ein7 clear with co66ents on what distros 7a2e t rouble and needed 0urther in2esti7ation enabled 6e to 7radually 7et it workin7 on all distros. #7ain an e1a64le o0 how that could look with the co66ents 6ade bold. Static ublic Sub Read&b!n0o() &i6 s"ySKl sSKlite sost%reSKl #s Strin7 &i6 ios' iosE #s !nte7er =table on most 8etu8ned dist8os 7 .0#" a bit t8oublesome =TO#O/ slite3 needs testing 8esults 8etu8ned fo8 anal:sing =TO#O/ figu8e out slite 8et8ie>c9ec@ fo8 +:sl >> (rest o0 code) End #nd belie2e 6e one day when you look back at an a44lication you built and 0or7ot about you will be 2ery ha44y with all your co66ents in your code. So6e s4ecial keywords like DTO&O3 in your co66ents will 7enerate an inline task. This 6akes it easy trackin7 code where thin7s need to be done and also works 2ery well as a re6inder o0 thin7s that still need to be done in your code. Later in this 7uide ! will 7o into this a bit 0urther in an e1a64le a44lication.
*$3$% Clea8 c9oices fo8 names ickin7 the ri7ht na6es 0or 2ariables you use in your code can 6ake 0or 6ore readable code. Two thin7s ! tend to do are3 ick a na6e that describes the content ➢ 'e7in the na6e with a lower case letter identi0yin7 the ty4e o0 content it re4resents ➢
1$ ick a na6e that describes the content
11
5. Some good practices
Na6es o0 2ariables that su77est what their content will be are easier to co64rehend and re6e6ber when readin7 or writin7 6any lines o0 code. !n one o0 6y a44lications ! try to e1tract the window 6an7er used on the syste6. #n easy na6e 0or that could be Win"an. The na6e tells us that the in0or6ation on window 6an7er retrie2ed on the syste6 will be stored in the 2ariable na6ed Win"an.
%$ 'e7in the na6e with a lower case letter identi0yin7 the ty4e o0 content it re4resents %oin7 back to the 4re2ious e1a64le o0 the window 6ana7er bein7 stored in a 2ariable na6ed Win"an it is best 6ake so6e 6inor adust6ent to the na6e to identi0y the ty4e o0 content. The content that will be e1tracted 0ro6 the syste6 will contain te1t so in %a6bas itDs ty4e is Strin7. Now to indicate that the 2ariable Win"an will contain data o0 the ty4e Strin7 ! na6ed it sWin"an. 'y 4uttin7 a lower case DsD in 0ront o0 DWin"anD ! identi0y the 2ariable as bein7 a strin7 containin7 window 6ana7er in0or6ation. !n the course o0 this 7uide and all the e1a64les you will 7et 6ore e1a64les on how you could use this to your bene0it.
*$3$3 Indenting code #nother 7ood 4ractice 0or clear and readable code is indentin7 code. +or e1a64le say you want to check i0 so6e condition is 6et and do one thin7 or another de4endin7 on the result o0 the condition. !n %a6bas you would code this with an !0 ...Then .. .Else. Lets say you want to test i0 our sWin"an is e64ty or not. ! will 0irst 7i2e an e1a64le o0 less readable code without any indentin7. Static ublic Sub Test() &i6 sWin"an #s Strin7 sWin"an Te1t'o1:.Te1t !0 Len(sWin"an) 9 Then sWin"an Unable to traceP Else sWin"an Tri6(sWin"an) Endi0 End Ne1t the sa6e code but with indentin7 a44lied 0or 6ore readable and structured code. Static ublic Sub Test() &i6 sWin"an #s Strin7 sWin"an Te1t'o1:.Te1t !0 Len(sWin"an) 9 Then sWin"an Unable to traceP Else sWin"an Tri6(sWin"an) Endi0 End The indentin7 6akes clear what 4art is e1ecuted i0 the condition is 6et and what 4art is e1ecuted i0 the condition isnDt 6et.
12
5. Some good practices
!n the e1a64les 4ro2ided in this 7uide you will 7et to see 6ore e1a64les o0 indentin7 as ! use it in all 6y codin7.
*$3$( "liminate 8epeating code 1$ Subroutines and 0unctions When you 0ind that you ha2e code in a 0or6 class that re4eats itsel0 it would 6ake 0or better and 6ore readable code to abstract it into a subroutine or 0unction that can be called. This also 6akes 0or easier 6aintenance 0or the code as you only need to chan7e it at one 4lace in your 0or6 class instead o0 se2eral. $ou will see e1a64les o0 this in this 7uide.
%$ "odules When you 0ind that you ha2e code in se2eral 0or6 classes re4eatin7 itsel0 or 4er0or6in7 like wise tasks try to abstract it and 4lace it in a 6odule. This way it can be used within the entire 4roect. This also 6akes 0or easier 6aintenance o0 t he code as you only need to chan7e it at one 4lace in the 4roect instead on se2eral 0or6s classes. "odules are actually e14orted classes that is why t hey work 0or the entire 4roect. $ou will 7et to see so6e e1a64les on usin7 6odules in this 7uide.
3$ *lasses *lasses can be considered as the te64lates 0or obects. When ha2in7 likewise code in 6odules in di00erent a44lications you should consider 6akin7 a new 4roect and co4yin7 the 6odule code into classes abstract so6e code and 0inally E14ort these classes and 6ake a library out o0 this a44lication 0or installation on your or other 4eo4leDs syste6. Use this library in all the 4roects you ha2e a need 0or its 4ro2ided 0unctionality. This also 6akes 0or easier 6aintenance o0 the code as you only need to chan7e it in one 4ro ect (the library) instead o0 in each 4roect o2er and o2er a7ain. The sco4e o0 this book does not co2er libraries and classes other than the +or6 classes.
13
6. first !"I app#ication$ %e&t'#ay
- A fi8st GUI application/ Te?t,la: Lets 7et started with usin7 %a6bas 8 and build a 0irst si64le %U! a44lication.
.1 W#at *ill $e done in t#i" c#apter "ain 0ocus will be creatin7 a 0or6 with so6e controls write so6e si64le code 0or the controls and run the a44lication. This will in2ol2e O4enin7 %a6bas !&E and 6ake a new 4roect ➢ "ake a 6ain 0or6 ➢ lace 0our controls on the 0or6 (Two buttons a Te1t'o1 and a Label) ➢ Write so6e si64le code ➢ Run the a44lication 0ro6 %a6bas !&E ➢ $ou can learn about3 ow to 6ake a new 4roect in %a6bas8 ➢ Workin7 with the %a6bas !&E ➢ 'utton Te1t'o1 and Label controls ➢ E2ent dri2en 4ro7ra66in7 obects 6ethods and 4ro4erties ➢ User interaction (in4ut 0ro6 user out4ut 0ro6 a44lication) ➢
.2 +tarting a ne* pro'ect +irst you start %a6bas 0ro6 ter6inal or 6enu de4endin7 on how you installed it =. On 0irst o4enin7 %a6bas it should show the e1a64les.
Illustration $: Gambas %4%1 opening screen
2
"ee 34ow To Gambas % * 0nstalling Gambas3
14
6. first !"I app#ication$ %e&t'#ay
Now click on )ew ,8oect$$$ to 6ake a new 4roect. !n the t he DNew 4roectD screen you select the ty4e o0 a44lication you wish to build.
Illustration Illustration &: 'e" project' screen step 1% (elect the t)pe of application
Select G8ap9ical application as that is what you are 7oin7 to build a %U! a44lication. No 0urther o4tions need to be selected.
Illustration Illustration *: 'e" project' screen step 2% (elect the folder for the ne" project
15
6. first !"I app#ication$ %e&t'#ay
'rowse to the 0older 5&e2elo46ent5%a6bas8> and select it. Now ri7ht click and select C8eate di8ecto8: in the 6enu.
Illustration +: ,reate a ne" folder for -earning projects
Na6e the 0older Gambas37.ea8ning and select it.
Illustration .: (elect the folder 'Gambas/-earning'
Ne1t click button )e?t . 5
"ee 2.1. 6older organisation
16
6. first !"I app#ication$ %e&t'#ay
!n the DNew 4roectD screen ste4 8 you can na6e both the 0older where the 4roect will be located and the na6e the actual a44lication will will ha2e. They can be di00erent i0 you like.
Illustration Illustration 10: 'e" project' screen step % ame the project folder folder and project project
+or both Droect na6eD na6eDB and Droect titleD titleDA ty4e Te?t,la:. Ne1t click the button OD and you 7et a 6essa7e a new 4roect has been created.
Illustration Illustration 11: essage "hen project has been created successfull) 7 9
8ame of the folder the #roject will be located in and the name of installed executable. 8ame of the actual a##lication as used in a distro menu once installed.
17
6. first !"I app#ication$ %e&t'#ay
#0ter clickin7 OD the %a6bas 8 !&E o4ens with a new e64ty 4roect na6ed Te1tlay.
Illustration 12: e" project 'e3tla)' opened in Gambas IDE
$ou ha2e ust created a new 4roect in %a6bas 0or buildin7 a 0irst %U! a44lication. Each 4roect o4ened in !&E has < 6aor Iones3 Cone :3 "enu and toolbar Ione ➢ Cone ;3 roect browser ➢ Cone 83 Workin7 area ➢ Cone <3 &ebu7 area ➢ They will 7radually 6ake 6ore sense when workin7 with the e1a64les that will be built in t his 7uide.
.3 !reating a form and control" Ne1t ste4 is to create a 0or6 with so6e controls on it. Since you chose to create a new D%ra4hical a44licationD a 0or6 is already in 4lace na6ed &+ain@. $ou will 0ind this 0or6 in the roect browser (Ione ;) in the 0older DSourcesD.
Illustration 1: 5ain in (ources folder of the project :
8ote the small blac arrow in the #roject browser in front of the 6Main icon. That arrow indicates that this form is the first one to run& after starting the a##lication
18
6. first !"I app#ication$ %e&t'#ay
Just double click &main and the e64ty 0or6 will be in the Workin7 area (Ione 8)
Illustration 14: Empt) form in IDE
!n the DWorkin7 areaD (Ione 8) you see an e64ty 0or6 and at t he ri7ht o0 it a bo1 with Dro4erties5ierarchyD. Underneath the Dro4erties5ierarchyD you will 0ind a Tool'o1 to build the controls. The D ro4erties5ierarchyD is only 2isible when 0or6s are in the DWorkin7 #reaD. !0 code is 2iewed in the DWorkin7 #reaD there will be no D ro4erties5ierarchyD bo1 nor Toolbo1.
-$3$1 ,lacing cont8ols on a fo8m +irst you will 4lace a Label on the 0or6. +or that you 7o to the Tool'o1 and ho2er your 6ouse o2er each o0 the icons until you 0ind the DLabelD icon (a bi7 #).
19
6. first !"I app#ication$ %e&t'#ay
Illustration 1$: -abel icon in ool6o3
*lick on the Label icon and ne1t dra7 a rectan7le on the 0or6.
Illustration 1&: -abel control dra"n on the form
This should 7i2e you a Label control. Ne1t search the Tool'o1 0or a Te1t'o1 icon.
Illustration 1*: e3t6o3 icon in ool6o3
20
6. first !"I app#ication$ %e&t'#ay
*lick on the Te1t'o1 icon and ne1t dra7 a rectan7le on the 0or6.
Illustration 1+: e3t6o3 control dra"n on the form
This should 7i2e you a Te1t'o1 control. Ne1t search the 'utton icon in the Tool'o1.
Illustration 1.: 6utton icon in ool6o3
*lick on the 'utton icon and dra7 two rectan7les on the 0or6.
Illustration 20: "o button controls dra"n on the form
This should 7i2e you two buttons.
21
6. first !"I app#ication$ %e&t'#ay
-$3$% A close8 loo@ at t9e cont8ols +irst select the Label by clickin7 on it. The Dro4erties5ierarchyD 'o1 will show 4ro4erties o0 the selected control Label:.
$ou are about to chan7e so6e o0 the6 and see what they do. Note that when you selected Label: in the Dro4erties5ierarchyD 'o1 it says DLabel: LabelD (Label: is the na6e o0 the control and Label is the ty4e o0 control). !n the bo1 below you can 0ind el4 on .abel 4gb$gui5. +irst click on the D#li7n6entD 4ro4erty on )o8mal and a selection bo1 will a44ear. Select !ig9t.
Illustration 21: (elect !lignment of the -abel control
22
6. first !"I app#ication$ %e&t'#ay
This will ali7n the te1t o0 the label to the ri7ht.
Illustration 22: -abel1 aligned right
Ne1t 7o to the DTe1tD 4ro4erty and click on .abel1 and ty4e "nte8 :ou8 name/
Illustration 2: -abel1 e3t propert) changed
$ou will see the ty4ed te1t a44ear in your label on your 0or6. $our label is now ready 0or use in the a44lication. Label controls are usually used in co6bination with an in4ut control. The Label is used to 6ake clear what the in4ut control is 0or and should contain. +or e1a64le a Label could show DNa6eD 0ollowed by an in4ut control like a Te1t'o1 where the user can ty4e his5her na6e. This doesnDt 6ean you can use Labels otherwise. Ne1t lets 0ocus on the other controls by 6eans o0 e1ercise. ere is what needs to be done3 Select Te?tBo?1 and e64ty the Te1t 4ro4erty. ➢ Select Button1 and chan7e the Te1t 4ro4erty to DShowD. ➢ Select Button% and chan7e the Te1t 4ro4erty to D*learD. ➢
Illustration 24: ,ontrols after changing the properties
$our end result should look like the illustration abo2e.
23
6. first !"I app#ication$ %e&t'#ay
.4 !oding, te"ting, coding, te"ting... Now that you ha2e 0inished the 6akin7 and custo6isin7 o0 the 0or6 and controls 0ocus will 7o to 6ake it do so6ethin7.
-$($1 ObectsE p8ope8tiesE e
-$($% W8ite some code and test 48ound 15 Now lets start with the 0un 4art the actual codin7 or writin7 %a6bas code. This is what you want to ha44en when runnin7 your code3 :. $ou want to ty4e your na6e in the Te1t'o1. ;. When clickin7 on 'utton DShowD you want a 6essa7e bo1 with the na6e ty4ed in the Te1t'o1. 8. When clickin7 on the 'utton D*learD you want to e64ty the contents o0 the Te1t'o1. LetDs analyse this and di2ide it into two 4ieces3 :. User in4ut3 - Ty4e na6e - *lick on 'utton Show - *lick on 'utton *lear ;. #44lication out4ut3 - "essa7e with na6e - *lear a Te1t'o1 !n the use8 input you can see so6e e2ents (or thin7s that can ha44en to an obect). Te1t bein7 entered by the user in a Te1t'o1 and clickin7 on buttons. So these are e2ents w9e8e :ou need to w8ite :ou8 code. !n the application output you see what needs to ha44en so w9at :ou need to code. Now 0or the te1t bein7 entered nothin7 realy needs to be coded as t hin7s ha44en when clickin7 the buttons. *lickin7 the Show button needs to show you a 6essa7e bo1 with the entered te1t. *lickin7 the *lear button needs to clear the entered te1t. 1'
) ;creatable; object is an object that can be instantiated by itself.
24
6. first !"I app#ication$ %e&t'#ay
Ri7ht click on button 9ow select "
Illustration 2$: (elect Event ,lic# for button control
This will o4en the +"ain.*lass in a new tab in the DWorkin7 #reaD.
Illustration 2&: 5ain%class
25
6. first !"I app#ication$ %e&t'#ay
#s you see the cursor is 4ositioned in ublic Sub 'utton:M*lick(). !n short this 6eans that this Subroutine (Sub) belon7in7 to obect 'utton: and takin7 4lace on the E2ent *lick is ublic 0or +6ain.class. Write this code 0or ublic Sub 'utton:M*lick()3 ublic Sub 'utton:M*lick() "essa7e(Te1t'o1:.Te1t) End The obect na6ed Te1t'o1: holds the te1t entered by the user in the 4ro4erty Te1t. To 7et there you write Te1t'o1:.Te1t ::. The 6ethod "essa7e() dis4lays a Strin7 2alue in a 6essa7e bo1. Ne1t double click &+ain in the 4roect browser (or click tab &+ain$fo8m) to 7o back to the 0or6. Ri7ht click button *lear and select "
Illustration 2*: enu Debug /7 8un to test the application
11
26
6. first !"I app#ication$ %e&t'#ay
Now lets see i0 the a44lication does what you want it to do. Enter your na6e in the Te1t'o1 and click 9ow. # 6essa7e with the entered na6e will a44ear. *lick OD to close the 6essa7e.
Ne1t click Clea8 and the Te1t'o1 will be e64ty. So all see6s to work as e14ected but that doesnDt 6ean you are ready yet as there certainly is so6e roo6 0or i64ro2e6ent.
-$($3 Code some imp8o
)nother o#tion is to sim#ly double clic the form to go to 6orm>?#en!(
27
6. first !"I app#ication$ %e&t'#ay
$ou will also need to add a test to the *lick e2ent 0or button 9ow to act one way or the other de4endin7 on the result o0 the test. What needs testin7 is i0 te1t has been entered into Te1t'o1:. So you need to check the 4ro4erty Te1t i0 it is e64ty or not. *han7e the code 0or ublic Sub 'utton:M*lick() to3 ublic Sub 'utton:M*lick() !0 !sNull(Te1t'o1:.Te1t) Then DTest i0 na6e entered "essa7e(+irst 0ill in a na6eP) Else "essa7e(The na6e entered is Te1t'o1:.Te1t) Endi0 End !sNull(Te1t'o1:.Te1t) will test i0 Te1t'o1:.Te1t is e64ty :8 and return True i0 it is. !0 true a 6essa7e will tell you to 0irst 0ill in a na6e. !0 0alse the na6e will be shown but a bit 6ore nicer this ti6e. Note3 f s9ull+:eto1, :hen is the sa6e as3 f s9ull+:eto1.:et, > :rue :hen. !sNull() is a dataty4e 0unction o0 ty4e 'oolean 6eanin7 it will be true or 0alse. &e4endin7 on the e14ression tested. !sNull(expre""ion) Returns True i0 expre""ion is NULL. NULL 6eans3 - The NULL constant - # null obect re0erence - # Iero len7th strin7 - # null date - #n uninitialiIed 2ariant "ore on !sNull() see htt43557a6basdoc.or75hel45lan75isnull?a28
!0 expre""ion Then..Else..Endi0 'ehind If you need a test 4expre""ion- t9at 8etu8ns a Boolean (True5+alse) The code 0or T9en is run w9en test 8etu8ns t8ue. The code 0or "lse is run w9en test 8etu8ns false. "ndif ends the code block. $ou can use lo7ic o4erators (like #nd and Or) in the expre""ion to test 0or 6ore than one condition. "ore on !0 see htt43557a6basdoc.or75hel45lan75i0?28 "essa7e() needs to contain strin7 2alues. Te1t between Kuotes is considered strin7 as is the content o0 a Te1t 4ro4erty. To connect the6 to7ether to one strin7 you use . Ne1t it is ti6e to test. ress &* and ne1t click the button 9ow. 1%
) Text@ox with a s#ace entered by the user might loo em#ty but actually isn3t as it is not eAual to 8ull !it contains a s#ace and thus as string loos lie B B and has a length of 1 instead of looing lie this BC with a length of '(.
28
6. first !"I app#ication$ %e&t'#ay
Illustration 2.: essage "hen no name entered
#s you can see the a44lication is now centred on the screen. *lickin7 9ow results in a 6essa7e to 0irst 0ill in a na6e. Now click OD enter a na6e and click 9ow a7ain.
Illustration 0: essage "hen name entered
$ou see this results in a nicer result than in the 0irst round o0 testin7. *on7ratulations you ha2e ust build a 0irst %U! a44lication in %a6bas 8. Now close the 4roect in 6enu &ile ; Fuit. This will also close %a6bas and set you at the 4oint to start 0or the ne1t cha4ter.
29
(. )&tending app#ication %e&t'#ay
"?tending "?tending applicatio application n Te?t,l Te?t,la: a: .1 .1 W#at W#at *ill *ill $e don done e in t#i t#i" " c#ap c#apte ter r "ain 0ocus will be on addin7 so6e 6ore controls and code to 0urther e14lore %a6bas and run the a44lication. a44lication. This will in2ol2e3 O4en an e1istin7 4roect 0or chan7es ➢ #dd new controls (Radio 'utton *heck'o1 Te1t#rea Te1t#rea +ra6e) ➢ #dd5chan7e code to e2ents o0 so6e o0 the controls ➢ Run the a44lication 0ro6 !&E ➢ $ou can learn about3 Workin7 with the %a6bas !&E ➢ "ore controls ➢ "ore 4ro4erties e2ents and 6ethods ➢ User interaction ➢
.2 .2 (pen (pen an exi" exi"ti ting ng pro'e pro'ect ct +irst you start %a6bas and it should o4en on the !ecent ,8oects tab. !0 not click on it.
The 4roect Te?t,la: should be in the list so6ewhere on to4 i0 recently o4ened :<. *lick on Te?t,la: to o4en in !&E. 1+
?r it could be the only a##lication in there if TextDlay is your first Gambas % a##lication.
30
(. )&tending app#ication %e&t'#ay
.3 .3 /odi /odif) f) mai main n form form and and add add con contr trol ol" " Ne1t ste4 will brin7 us back to +6ain to redesi7n the 0or6 0or so6e additional controls to be added later. To 7et 7et there ust double click &main in the 4roect browser. Once you ha2e the +"ain in the DWorkin7 #reaD 6o2e your 6ouse to 6iddle o0 the ri7ht border o0 the 0or6.
Illustration Illustration 2: ouse appearance "ill change "here the pointer is
$our 4ointer will chan7e a44earance and you will be able to dra7 the 0or6 wider. So dra7 the 0or6 a bit wider.
Illustration Illustration : 5orm dragged "ider and -abel and e3t!rea selected
Ne1t click the .abel hold down ct8l key and click the Te?tBo?.
31
(. )&tending app#ication %e&t'#ay
This will select both o0 the6 and you will be able to dra7 the6 a bit to the le0t as there is still so6e roo6 le0t o2er.
Illustration Illustration 4: Drag the buttons to the bottom of the form
Ne1t select button 9ow hold ct8l key and select button Clea8. &ra7 the6 to the botto6 o0 the 0or6.
Illustration Illustration $: 8esult after adapting the form
$our 0or6 should look like in the illustration abo2e.
32
(. )&tending app#ication %e&t'#ay
$3$1 Cop: cont8ols on t9e fo8m !n the ne1t ste4 you are 7oin7 to add a Label Te1t'o1 and a 'utton to the 0or6. !nstead o0 usin7 the Tool'o1 and draw the6 you will si64ly co4y and 4aste e1istin7 controls on the 0or6. +or this 0irst select the .abel and Te?tBo? and ne1t 4ress ct8lc (to co4y the controls). Ne1t 4ress ct8l< (to 4aste the controls on the 0or6).
Illustration &: asted controls on the form
Now dra7 the newly 4asted controls below the ones already there.
Illustration *: ,ontrols dragged to the spot )ou need them
Ne1t select one o0 the buttons co4y54aste it and dra7 it to the botto6-le0t o0 the 0or6.
Illustration +: 8esult after cop)9pasting all needed controls
33
(. )&tending app#ication %e&t'#ay
$3$% A close8 loo@ at t9e cont8ols Now that all the new controls are in 4lace lets ha2e a closer look at so6e o0 their 4ro4erties. +irst select the 6ost to4 .abel and check its Na6e 4ro4erty #li7n6ent 4ro4erty and Te1t 4ro4erty in the Dro4erties5ierarchyD 'o1. Now select the other .abel and do the sa6e check. $ou will 0ind one Label na6ed Label: the other na6ed Label;. #ll other 4ro4erties (e1ce4t 0or and $ location on the 0or6) are co4ied 0ro6 Label: to Label; durin7 the *o4y5aste o0 Label:. When co4y54astin7 controls their na6e will always be a co4y o0 the na6e o0 the ori7inal control with an added nu6ber. The nu6ber will be one hi7her than the 4re2ious one. E1a64le3 !0 you co4y54aste Label: the co4y will be na6ed Label;. !0 you ne1t co4y54aste either Label: or Label; the ne1t co4y will be na6ed Label8 Select the Te?t 4ro4erty 0or .abel1 and ty4e "nte8 :ou8 fi8st name/
Illustration .: -abel1 after changing the e3t propert)
Ne1t select Te?tBo?1. *han7e the )ame 4ro4erty to tb?&i8st)ame. Ne1t select Te?tBo?% and chan7e the name 4ro4erty to tb?)ame.
Illustration 40: ame properties of both e3t6o3es changed
So6e 6ore ada4tations as a little e1ercise3 - *han7e the Na6e 4ro4erties o0 the le0t button to btnShow - *han7e the Na6e 4ro4erties o0 the 6iddle button to btn*lear - *han7e the Na6e 4ro4erties o0 the ri7ht button to btn#dd - *han7e the Te1t 4ro4erties o0 the ri7ht button to #dd
34
(. )&tending app#ication %e&t'#ay
Illustration 41: ain form after the changes in the e3ercise
!n case ! do not use the na6e o0 a control on a 0or6 in code ! will kee4 the ori7inal na6e 7i2en to the control when 0irst drawn on the 0or6 (or co4ied onto the 0or6). !n case ! a6 7oin7 to use the na6e o0 the control in code ! like to ha2e a na6e that 6akes so6e sense. Two thin7s are i64ortant to 6e3 :. ! would like to know the ty4e o0 control ;. ! would like to 7et an idea what the control is 0or +or this ! ha2e de2elo4ed 6y own syste6 based on the na6in7 o0 2ariables accordin7 to ty4e (see =.8.; *lear choices 0or na6es). ! use three lower case letter to identi0y the ty4e o0 control ne1t a *a4ital letter 0ollowed by lower case identi0yin7 the 4ur4ose o0 the control. ome e?amples/ Te1t'o1: V tb1+irstNa6e 'utton: V btnShow Label8 V lblResult Te1t#rea: V t1aResult *heck'o1: V cb1Show: Radio'utton: V rbn$es
$ou will 7et to see 6ore o0 this as the 7uide continues. !0 all chan7es are 6et ha2e a look at the +"ain.class. *lick the tab &main or in the 4roect browser ri7ht click &+ain and select "dit code
35
(. )&tending app#ication %e&t'#ay
Illustration 42: ames of buttons changed in 5main%class as "ell
$ou will notice that chan7in7 the na6e 4ro4erties 0or 'utton: and 'utton; also chan7ed their na6es in the +"ain.class. 'e aware that when chan7in7 a Na6e 4ro4erty o0 a control on a 0or6 only the 0or6 class the control belon7s to will chan7e the na6e in code as well.
Now two 6ore thin7s are le0t to do and they 6ake 0or a 7reat e1ercise as well3 - #dd a Label (below tb1Na6e) to the 0or6 na6e it lblResult - #dd a Te1t'o1 (below lblResult) to the 0or6 na6e it tb1Result and set ReadOnly to True - #dd a Te1t#rea (below tb1Result) to the 0or6 na6e it t1aResult and set ReadOnly to True - E64ty the Te1t 4ro4erty o0 lblResult and t1aResult
Illustration 4: ain form after adding t"o more controls
The result should look like the illustration abo2e.
36
(. )&tending app#ication %e&t'#ay
.4 !oding, te"ting and adapting !n this 4art you are 0irst 7oin7 to add new code test it i64ro2e it test it a7ain as so on. $ou will not always start with the best solution but 7radually work towards one and this is done on 4ur4ose (you will be seein7 6ore o0 that). This way you will learn what can and canDt be done in %a6bas. $ou will learn what can 7o wron7 and how to 0i1 it. !t is like learnin7 to ride a bicycle it is 0allin7 down that 6akes 0or a 7reatest learnin7 cur2e.
$($1 Coding and testing 48ound 15 +irst 0ocus will be to 6ake the #dd button do so6ethin7. Since there are two in4ut 0ields now (tb1+irstNa6e and tb1Na6e) it would be nice to let the #dd button DaddD both na6es to7ether and show the6 in lblResult tb1Result and tarResult. LetDs code 0or this. Select button Add ri7ht click and 7o to "
Illustration 44: 8esult after clic#ing button !dd
Now click Clea8 and see what ha44ens. #s it see6s only the 0irst na6e 7ets cleared. !t would be nice i0 not only 0irst na6e 7ets cleared but na6e and the results as well.
37
(. )&tending app#ication %e&t'#ay
Ne1t click in the 0irst na6e bo1 and 4ress Tab key a 0ew ti6es. The order o0 how Tab u64s 0ro6 one control to the other can be deter6ined by you. So here is two 4oints 0or chan7e. Note the di00erence in how the result is shown in lblResult t1bResult and t1aResult. #lso note that the lblResult tb1Result and t1aResult are not editable 6eanin7 you canDt chan7e the content. +or tb1Result and t1aResult you needed to set 4ro4erty ReadOnly to True. Labels donDt ha2e a ReadOnly 4ro4erty as they are de0ault read only.
$($% AdaptingE coding and testing 48ound %5 +irst ha2e a look at the order controls will be selected when usin7 the Tab key. +or this 7o to the Dro4erties5ierarchyD 'o1 o0 &main and click on the Tab Hie8a8c9:.
Illustration 4$: ierarch) of the controls on main form
$ou can select a control in the ierarchy 'o1 and 6o2e it u4 or down chan7in7 the order it will be selected. The only 0ields that need to be DTabbedD are +irstNa6e Na6e and the three buttons. So 6o2e the6 u4 in the hierarchy. Label: and Label; are o0 no i64ortance so 6o2e the6 down.
Illustration 4&: ierarch) after adaptation
38
(. )&tending app#ication %e&t'#ay
Ne1t click on the ro4erties Tab and select Label: Label; lblResult tb1Result and t1aResult. !n the 4ro4erties scroll to the 4ro4erty )oTab&ocus and set this to T8ue
Illustration 4*: (et selected controls oab5ocus propert) to rue
This should take care o0 the Tab key order. Now lets ha2e a look at the code o0 btn*lear and ada4t it to 6eet your reKuire6ents. *han7e code 0or ublic Sub btn*learM*lick() to3 ublic Sub btn*learM*lick() Dtb1+irstNa6e.Te1t P tb1+irstNa6e.Te1t Null tb1Na6e.Te1t Null lblResult.Te1t Null tb1Result.Te1t Null t1aResult.*lear End The D be0ore tb1+irstNa6e.Te1t P 6akes the line a co66ent. So it will not be e1ecuted. ere you ust try another 6anner o0 clearin7 the content usin7 Null. When re4lacin7 a 4iece o0 code with new code a 7ood 4ractice is to co66ent the old code rather than deletin7 it. Ne1t write the new code below the co66ented code. !0 the new code turns out to be 4roble6atic or bu77y you can delete the new code and re6o2e the co66ents 0ro6 the old one rather than ha2in7 to code it all o2er a7ain. !0 the new code turns out a success you could consider re6o2in7 the old (co66ented) code. The t1aResult could be cleared usin7 t1aResult.Te1t Null but ! wanted to show you another way o0 clearin7 content. Just 4lay around with the di00erent o4tions and see what works and what doesnDt i0 you want to learn so6e 6ore. One 6ore 0eature would be nice to add. When clickin7 #dd i0 one o0 the na6e bo1es is e64ty you should be told that it is e64ty and set the 0 ocus on that bo1. +or this you will need to ada4t the code 0or the btn#dd. *han7e the code 0or ublic Sub btn#ddM*lick() to3
39
(. )&tending app#ication %e&t'#ay
ublic Sub bnt#ddM*lick() &i6 sResult #s Strin7 !0 !sNull(tb1+irstNa6e.Te1t) Then "essa7e(+ill in 0irst na6eP) tb1+irstNa6e.Set+ocus Else !0 !sNull(tb1Na6e.Te1t) Then "essa7e(+ill in na6eP) tb1Na6e.Set+ocus Else sResult tb1+irstNa6e.Te1t tb1Na6e.Te1t lblResult.Te1t sResult tb1Result.Te1t sResult t1aResult.Te1t sResult Endi0 Endi0 End +irst you test i0 tb1+irstNa6e is e64ty. !0 so a 6essa7e will be dis4layed and 0ocus will be set to +irst Na6e bo1 (tb1+irstNa6e.Set+ocus V Set+ocus is a 6ethod that 7i2es 0ocus to the control). !0 tb1+irstna6e is not e64ty you test i0 tb1Na6e is e64ty. !0 so a 6essa7e is dis4layed and 0ocus is set to the Na6e bo1. !0 tb1Na6e is not e64ty the result will be shown. #s you can see you can use an !0..Then..Else in another !0..Then..Else.. ! ndentin7 will 6ake clear where each block o0 the !0..Then..Else.. is resided. Now it is ti6e to run the a44lication. +irst click on #dd without enterin7 na6es. See what ha44ens. Ne1t enter a na6e 0or +irst Na6e and click #dd and see what ha44ens.
Illustration 4+: ,lic#ing !dd "ithout filling in a name
40
(. )&tending app#ication %e&t'#ay
Ne1t enter a na6e 0or Na6e and click #dd and see what ha44ens. Ne1t click the *lear button and see what ha44ens. !0 all is well the code should be workin7 as su44osed to.
$($3 Adding new cont8olsE code and test 48ound 35 +or the ne1t 4art you need to 7o back to the 0or6 desi7n. lace three *heck'o1es and three Radio'uttons on the 0or6.
Illustration 4.: hree ,hec#6o3es and three 8adio6uttons
Ne1t run the a44lication and click on the three *heck'o1es then click on the three Radio'uttons and note the di00erence.
Illustration $0: !ll ,hec#6o3es selected; onl) one 8adio6utton
Where all three *heck'o1es can be selected only one Radio'utton can at any 7i2en ti6e. ,ee4 that in 6ind. Now back to 0or6 desi7n.
41
(. )&tending app#ication %e&t'#ay
ere is what you need to do (yes e1ercise ti6e)3 - *han7e !adioButton1 na6e 4ro4erties to 8bt.ine1 Te1t 4ro4erties to 1 .ine - *han7e !adioButton% na6e 4ro4erties to 8bt.ine% Te1t 4ro4erties to % .ines - &elete !adioButton3 - *han7e C9ec@Bo?1 na6e 4ro4erties to cb?&i8st)ame Te1t 4ro4erties to Add - *han7e C9ec@Bo?% na6e 4ro4erties to cb?)ame Te1t 4ro4erties to Add - *han7e C9ec@Bo?3 /isible 4ro4erty to &alse What you reKuire is when : Line is selected clickin7 the button #dd shows the in0or6ation in : Line. When ; Lines is selected clickin7 the button #dd shows the in0or6ation in two lines. 'ut that is not all. +irst na6e will only be added i0 cb1+irstNa6e is selected Na6e will only be added i0 cb1Na6e is selected. Ne1t you need to code #dd button to incoo4erate the new reKuire6ents. When a *heck'o1 is selected its 2alue is set to -: i0 it is deselected its 2alue is set to 9. When a Radio'utton is selected its 2alue is set to True i0 not selected it is set to +alse. So this is what you need to to be able to test what needs to be done. Lets take it ste4 by ste4. +irst you code 0or the Radio'uttons to work. *han7e the code 0or ublic Sub btn#ddM*lick() to3 ublic Sub bnt#ddM*lick() &i6 sResult #s Strin7 !0 !sNull(tb1+irstNa6e.Te1t) Then "essa7e(+ill in 0irst na6eP) tb1+irstNa6e.Set+ocus Else !0 !sNull(tb1Na6e.Te1t) Then "essa7e(+ill in na6eP) tb1Na6e.Set+ocus Else !0 rbtLine:./alue Then sResult tb1+irstNa6e.Te1t tb1Na6e.Te1t Else sResult tb1+irstNa6e.Te1t Gn tb1Na6e.Te1t Endi0 lblResult.Te1t sResult tb1Result.Te1t sResult t1aResult.Te1t sResult Endi0 Endi0
D: line selected D; lines selected DGn 7enerates a new line
End #s you can see only rbtLine: 7ets tested. #s there are only two Radio'uttons one o0 the6 will always be selected. So i0 rbtLine: is not selected rbtLine; is. Run and see i0 this 0irst 4art o0 code does the ob. Just see what ha44ens and 4ay s4ecial attention to the di00erent result out4uts when ; Lines is selected.
42
(. )&tending app#ication %e&t'#ay
Illustration $1: 8esult of 2 lines selected
#s you can see in the illustration abo2e a Te1t'o1 Te1t'o1 can not hold two lines o00 te1t. 'oth Label and Te1t#rea can. The stran7e character in the Te1t'o1 is the line 0eed shown (GnP). Ne1t lets add the code 0or the *heck'o1es and re6o2e the Te1t'o1 Te1t'o1 as it is o0 no use any lon7er. 'ack to +or6 desi7n3 - Re6o2e tb1Result - *heck +6ain.class code i0 tb1Result is used and re6o2e i0 needed. #7ain a nice e1ercise. #nd #nd i0 you 6i7ht 0or7et to re6o2e tb1Result 0ro6 code so6e where donDt worry !&E will tell you once you try runnin7 the a44lication. Just so you know. Ne1t chan7e the code 0or ublic Sub btn#ddM*lick() btn#ddM*lick() to3 ublic Sub bnt#ddM*lick() bnt#ddM*lick() &i6 sResult #s Strin7 &i6 iSwitch #s !nte7er DiSwitch will be used to deter6ine what needs to be shown iSwitch 9 D DTest DTest i0 0irst na6e needs to be added be0ore checkin7 i0 0ield is e64ty !0 cb1+irstNa6e./alue cb1+irstNa6e./alue -: Then !0 !sNull(tb1+irstNa6e.T !sNull(tb1+irstNa6e.Te1t) e1t) Then "essa7e(+ill in 0irst na6eP) tb1+irstNa6e.Set+ocus iSwitch -: Else iSwitch : Endi0 Endi0
43
(. )&tending app#ication %e&t'#ay
DTest i0 Na6e needs to be added be0ore checkin7 i0 0ield is e64ty !0 iSwitch F - : Then !0 cb1Na6e./ cb1Na6e. /alue -: Then !0 !sNull(tb1Na6e.Te1t) !sNull(tb1 Na6e.Te1t) Then "essa7e(+ill in na6eP) tb1Na6e.Set+ocus iSwitch -: Else !0 iSwitch : Then iSwitch :; Else iSwitch ; Endi0 Endi0 Endi0 Endi0 D D*heck iSwitch what needs to be done !0 iSwitch F -: Then Select iSwitch *ase : DShow 0irst na6e sResult tb1+irstNa6e.Te1t tb1+irst Na6e.Te1t *ase ; DShow na6e sResult tb1Na6e.T tb1Na6e. Te1t *ase :; DShow 0irst na6e and na6e !0 rbtLine:./ rbtLine:. /alue Then D*heck 0or : line sResult tb1+irstNa6e.Te1t tb1+irst Na6e.Te1t tb1Na6e.T tb1Na6e. Te1t Else sResult tb1+irstNa6e.Te1t tb1+irst Na6e.Te1t Gn tb1Na6e.T tb1Na6e. Te1t DGn 7enerates a new line Endi0 End Select lblResult.T lblResult. Te1t sResult t1aResult.T t1aResult .Te1t e1t sResult Endi0 End iSwitch is set to -: e2ery ti6e one o0 the na6e bo1es needs to be 0illed in and 7ets the 0ocus. !n such a case nothin7 0urther needs to be tested or shown. Select expre""ion *ase expre""ion End Select Select enables you to test 0or di00erent results o0 one and the sa6e e14ression. 'ehind Case you need an expre""ion elect 4as long as t:pes matc95. Each Case can be tested against t9e e?p8ession be9ind elect $ou can use Case "lse 0or all other outco6e i0 needed "nd elect ends the code block. E14ression in e1a64le code abo2e is iSwitch o0 ty4e !nte7er. So behind the *ase you need an !nte7er 2alue or e14ression to test 4ossible outco6e o0 iSwitch. "ore on select see htt43557a6basdoc.or75hel45lan75select?28
44
(. )&tending app#ication %e&t'#ay
!n an e14ression you can use o4erators to 6ake co64arisons. a b V a s6aller than b a b V a eKuals b a b V a s6aller than or eKual to b a F b V a bi77er than b a F V a eKual to or bi77er than b a F b V a di00erent 0ro6 b $ou can co64are both nu6ber 2alues and strin7s. +or strin7s al4habetic order is used to deter6ine outco6e. :9 :: V returns 0alse :9 :: V returns true WordP NoneP V returns 0alse WordP WordP V returns true WordP NoneP V returns 0alse WordP F NoneP V returns true "ore on o4erators e2aluation order see htt43557a6basdoc.or75hel45lan75e2alorder?28 "ore on strin7 o4erators see htt43557a6basdoc.or75hel45cat5strin7o4?28 "ore on arith6etic o4erators see htt43557a6basdoc.or75hel45cat5aritho4?28 Now back to runnin7 the 4roect. !0 you 7et errors you 4robably 0or7ot to re6o2e tb1Result so6ewhere in the code. +i1 it. Ty4e Ty4e a 0irst na6e and a na6e and select only 0irst na6e to add. Ne1t click #dd and see what ha44ens. Now select na6e to add and deselect 0irst 0 irst na6e. *lick #dd and see what ha44ens. Now select both and click #dd to see what ha44ens.
$($( Add some last cont8olsE code and test 48ound (5 Lets co64licate thin7s a bit 6ore by addin7 so6e 6ore reKuire6ents. +irst you would like to enter *ountry as well and ha2e a *heck'o1 to #dd. +urther 6ore you would like the o4tion to 4resent the in0or6ation in 8 lines as well. #nd last but not least you want to be able to deter6ine what to Show when clickin7 button Show. Outco6e could be either 0irst na6e na6e or country. So lets start with yet another e1ercise3 - lace a new Label and Te1t'o1 below Label; and tb1Na6e to hold the country in0or6ation - Na6e the new Te1t'o1 tb1*ountry - #da4t the 4ro4erties o0 both controls to con0ir6 to those o0 Label; and tb1Na6e - #dd a new Radio'utton and na6e it rbtLine8 - *han7e rbtLine8 4ro4erties to con0ir6 to the other two Radio'uttons - "ake *heck'o18 2isible a7ain and na6e it cb1*ountry - #da4t 4ro4erties to con0ir6 to the other *heck'o1es #s you 6i7ht notice the instructions 7et less detailed so 6ore will be u4 to you. This is done intentionally. Just 0ollow the lo7ic o0 na6e and te1t 4ro4erties already on the 0or6 and see i0 you 6ana7e. &onDt 0or7et to check the hierarchy o0 the controls on the 0 or6X
Illustration $: e" controls added and adapted
+irst letDs 0ocus on code to 6ake tarResult show 4ro4er te1t when clickin7 btn#dd. +or this you will need to add so6e code to the btn#ddM*lick routine. ublic Sub bnt#ddM*lick() &i6 sResult #s Strin7 &i6 iSwitch #s !nte7er DiSwitch will be used to deter6ine what needs to be shown iSwitch 9
46
(. )&tending app#ication %e&t'#ay
D DTest i0 0irst na6e needs to be added be0ore checkin7 i0 0ield is e64ty !0 cb1+irstNa6e./alue -: Then !0 !sNull(tb1+irstNa6e.Te1t) Then "essa7e(+ill in 0irst na6eP) tb1+irstNa6e.Set+ocus iSwitch -: Else iSwitch : Endi0 Endi0 D DTest i0 Na6e needs to be added be0ore checkin7 i0 0ield is e64ty !0 iSwitch F - : Then !0 cb1Na6e./alue -: Then !0 !sNull(tb1Na6e.Te1t) Then "essa7e(+ill in na6eP) tb1Na6e.Set+ocus iSwitch -: Else !0 iSwitch : Then iSwitch :; Else iSwitch ; Endi0 Endi0 Endi0 Endi0 D--------- #&&E& *O&E -----DTest i0 *ountry needs to be added be0ore checkin7 i0 0ield is e64ty !0 iSwitch F - : Then !0 cb1*ountry./alue -: Then !0 !sNull(tb1*ountry.Te1t) Then "essa7e(+ill in a countryP) tb1*ountry.Set+ocus iSwitch -: Else !0 iSwitch : Then iSwitch :8 Else !0 iSwitch ; Then iSwitch ;8 Else !0 iSwitch :; Then iSwitch :;8 Else iSwitch 8 Endi0 Endi0 Endi0 Endi0 Endi0 Endi0 D---------- EN& #&&E& *O&E ------
47
(. )&tending app#ication %e&t'#ay
D D*heck iSwitch what needs to be done !0 iSwitch F -: Then Select iSwitch *ase : DShow 0irst na6e sResult tb1+irstNa6e.Te1t *ase ; DShow na6e sResult tb1Na6e.Te1t *ase :; DShow 0irst na6e and na6e rbtLine:./alue Then D*heck 0or : line sResult tb1+irstNa6e.Te1t tb1Na6e.Te1t Else sResult tb1+irstNa6e.Te1t Gn tb1Na6e.Te1t DGn 7enerates a new line Endi0 D----------- #&&E& *O&E -----------------*ase 8 DShow country sResult tb1*ountry.Te1t *ase :8 DShow 0irst na6e and country !0 rbtLine:./alue Then D*heck 0or : line sResult tb1+irstNa6e.Te1t tb1*ountry.Te1t Else sResult tb1+irstNa6e.Te1t Gn tb1*ountry.Te1t Endi0 *ase ;8 DShow na6e and country !0 rbtLine:./alue Then D*heck 0or : line sResult tb1Na6e.Te1t tb1*ountry.Te1t Else sResult tb1Na6e.Te1t Gn tb1*ountry.Te1t Endi0 *ase :;8 DShow 0irst na6e na6e and country !0 rbtLine:./alue Then D*heck 0or : line sResult tb1+irstNa6e.Te1t tb1Na6e.Te1t tb1*ountry.Te1t Else !0 rbtLine;./alue Then D*heck 0or ; lines sResult tb1+irstNa6e.Te1t tb1Na6e.Te1t Gn tb1*ountry.Te1t Else sResult t1b+irstNa6e.Te1t Gn t1bNa6e.Te1t Gn t1b*ountry.Te1t Endi0 Endi0 D----------- EN& #&&E& *O&E -----------------End Select lblResult.Te1t sResult t1aResult.Te1t sResult Endi0 End Ne1t add one line o0 code to the btn*learM*lick routine
48
(. )&tending app#ication %e&t'#ay
ublic Sub btn*learM*lick() Dtb1+irstNa6e.Te1t P tb1+irstNa6e.Te1t Null tb1Na6e.Te1t Null D #&&E& *O&E -tb1*ountry.Te1t Null D-- To clear the checkbo1es cb1+irstNa6e./alue 9 cb1Na6e./alue 9 cb1*ountry./alue 9 D EN& #&&E& *O&E -lblResult.Te1t Null tb1Result.Te1t Null t1aResult.*lear End Ti6e to test the a44lication. it &* to run the a44lication. +ill in so6e 2alues and check i0 e2erythin7 works as e14ected.
Illustration $4: esting the ne" code
#lso try usin7 the button *lear to see i0 e2erythin7 clears u4 well. E2erythin7 should work ust 0ine i0 not check your code to see i0 you ha2enDt 6ade a 6istake so6ewhere. Now letDs 0ocus on the last reKuire6ent3 you want to be able to deter6ine what to Show when clickin7 button Show. Outco6e could be either 0irst na6e na6e or country. With current controls on the 0or6 there is no way to deter6ine what to show when clickin7 button Show. $ou will need so6e e1tra Radio'uttons to 7et that ob done. #dd three Radio'uttons behind the three *heck'o1es. Ne1t run the a44lication and select one o0 the6. What ha44ens and why? Try answerin7 this 0or yoursel0 be0ore readin7 0urther.
49
(. )&tending app#ication %e&t'#ay
$ou will 4robably ha2e noticed that when clickin7 on one o0 the newly added Radio'uttons the Radio'utton selected at lines 7rou4 7ets deselected.
Re6e6ber 4a7e <:3 *here all three Chec&oes can be selected only one 4adioutton can at any given time. ?eep that in mind.P #ll Radio'uttons act like this when bein7 in the sa6e container. #nd 0or a Radio'utton that 6akes sense as you only want one to be selected. !n this case your 0or6 is the container o0 the 4laced Radio'uttons and since all the Radio'uttons are in the sa6e container only one can be selected. So what you need is a se4arate container to hold your newly added Radio'uttons. +irst rena6e the Radio'uttons to rbtShow: rbtShow; and rbtShow8. Ne1t select the6 and do ct8l? (or ri7ht click and select cut in 6enu).
Ne1t select the Tab container in the Tool'o1 and click on +ra6e. &ra7 a 0ra6e on the 0or6 where the cut Radio'uttons used to be and na6e it 0r6Show. Set the Te1t to ShowP.
50
(. )&tending app#ication %e&t'#ay
Illustration $*: 5rame (ho" placed on form
Now with the 0ra6e selected do ct8l< (or ri7ht click and select 4aste in 6enu)
Illustration $+: 8adio6uttons pasted into 5rame (ho"
Now run the a44lication a7ain and see what ha44ens. $ou should be able to click the Show Radio'uttons inde4endently 0ro6 the one 0or lines. *ontainers *ontainers 6ake it easy to 6ana7e controls that belon7 to7ether. !0 you dra7 the 0ra6e o2er the 0or6 all controls within the 0ra6e will be dra77ed alon7 as they are in the container. When 0or e1a64le you need certain controls 2isible under certain conditions 4lace the6 in one container. Ne1t you can 6ake the6 2isible or in2isible ust by settin7 the /isible 4ro4erty o0 the container instead o0 settin7 the /isible 4ro4erty o0 each indi2idual control. # +ra6e is a container with ed7es borders and a label. "ore on +ra6e see htt43557a6basdoc.or75hel45co6457b.Kt<50ra6e?28 'ack to desi7n 4lace the Line Radio'uttons in their own 0 ra6e as well. Na6e the +ra6e 0raLines with Te1t Lines
51
(. )&tending app#ication %e&t'#ay
*onsider it 7ood 4ractice to always 4lace Radio'uttons in their own container. !n ti6e you will 0ind out this has his ad2anta7es When co64leted your result should look so6ethin7 like the illustration below.
Illustration $.: !ll 8adio6uttons placed in their 5rames
Now letDs recode the btnShow e2ent click to 6eet our reKuire6ents (code see ne1t 4a7e).
Illustration &0: 8adio6uttons sho" "or#ing as supposed
Now run and test the result. !0 all is well it should work.
52
(. )&tending app#ication %e&t'#ay
ublic Sub btnShowM*lick()
!0 rbtShow:./alue Then !0 !sNull(tb1+irstNa6e.Te1t) Then "essa7e(+irst 0ill in a 0irst na6eP) Else "essa7e(The na6e entered is tb1+irstNa6e.Te1t) Endi0 Else !0 rbtShow;./alue Then !0 !sNull( tb1Na6e.Te1t) Then "essa7e(+irst 0ill in a na6eP) Else "essa7e(The na6e entered is tb1Na6e.Te1t) Endi0 Else !0 !sNull( tb1*ountry.Te1t) Then "essa7e(+irst 0ill in a countryP) Else "essa7e(The na6e entered is tb1*ountry.Te1t) Endi0 Endi0 Endi0
End
.5 0 la"t reie* of t#e application !n this last 4art o0 this cha4ter you are 7oin7 to re2iew your a44lication and see i0 it can be si64li0ied and 6ade 6ore 0riendly in use.
$*$1 #o :ou need a button 9ow That is a 2ery 7ood Kuestion. $ou ha2e 6ade an a44lication where you 0irst need to select what to show and then click the button Show to 6ake it show. So could you not si64ly select what to show and it shows? That is 0or you to 0i7ure out. Start with deletin7 the button Show. Now think about the ne1t three Kuestions and see i0 you can answer the63 - When do you want this to ha44en? - Where do you want this to ha44en? - &o ! ha2e usable code in 6y 4roect ! can si64ly co4y54aste? The answer to the 0irst Kuestion will 7i2e you the e2ent to code the second one the obect(s) to code (read3 controls). Take a 6o6ent to think about this and try to sol2e it yoursel0 be0ore continuin7 to the answer. # 4ositi2e answer to the third Kuestion will 6ake 0or a Kuick redo to 6eet the new de6ands. Think about this as well and sol2e it yoursel0 be0ore 7oin7 to the answer.
$*$% Could iwitc9 fo8mat be mo8e clea8 #s you 7a2e seen you ha2e used the 2ariable iSwitch to be set accordin7ly to selections 6ade in the add *heck'o1es to deter6ine what needs t o be show in the Label and Te1t#rea. These are the 2alues o0 iSwitch and what they re4resent3 alue !ep8esents : 0irst na6e V : line
53
(. )&tending app#ication %e&t'#ay
:; 0irst na6e and na6e V : or ; lines ; na6e V : line ;8 na6e and country V : or ; lines :;8 0irst na6e na6e and country V : ; or 8 lines !0 you would add a ne1t 0ield the /alue o0 iSwitch would 7et 6ore co64licated. So thinkin7 about these 2alues and their 0or6at is so6ethin7 that can 6ake li0e easier in a later sta7e o0 de2elo46ent when thin7s need to be added or chan7ed. So here are so6e 6ore Kuestions to think about3 - What 0or6at could do better? - *an the current 2ariables ty4e (!nte7er) hold the i64ro2ed 0or6at? The answer to the 0irst Kuestion will deter6ine the outco6e o0 t he second. #7ain think about this as well and sol2e it yoursel0 be0ore 7oin7 to the answer.
$*$3 9ould t9e button Add be enabled if no Add is c9ec@ed #7ain 7ood Kuestion. Should it be enabled or would it be better to enable it when a #dd 7ets checked and disabled when no #dd is checked? #7ain think about the ne1t two Kuestions and see i0 you can answer the63 - When do you want this to ha44en? - Where do you want this to ha44en? The answer to the 0irst Kuestion will 7i2e you the e2ent to code the second one the obect to code. Take a 6o6ent to think about this and try to sol2e it yoursel0 be0ore continuin7 to the answer.
$*$( 9ould t9e button Clea8 be enabled if t9e8e is not9ing to clea8 The Kuestions kee4 co6in7. WouldnDt it be better to ha2e button *lear enabled when Te1t#rea and Label ha2e content and ha2e it disabled when no content 4resent? #nd a7ain think about the ne1t two Kuestions and see i0 you can answer the63 - When do you want this to ha44en? - Where do you want this to ha44en? The answer to the 0irst Kuestion will 7i2e you the e2ent to code the second one the obect to code. Take a 6o6ent to think about this and try to sol2e it yoursel0 be0ore continuin7 to the answer.
. 0n"*er" to t#e la"t reie* ere you will 0ind all answers to the Kuestions 4osed in B.=. "ake sure that you ha2e 0irst tried to sol2e the answers yoursel0 and donDt be a0raid o0 0ailure. There is no such thin7 as 0ailure only lessons to be learned. #nd disco2erin7 how to do these thin7 yoursel0 by 0ailin7 and tryin7 all o2er a7ain is the 6ethod with the bi77est learnin7 cur2e that sticks the lon7est. This is one o0 the 6ain reasons ! choose to start e1a64les with code that can be i64ro2ed and show you the i64ro2e6ents ste4 by ste4. !n this way they will 6ake 6ore sense. So a0ter you tried and succeeded or not ha2e a look at how ! did it and kee4 in 6ind that there are other 4ossibilities as well. $ou 6i7ht ha2e sol2ed it di00erently and ha2e it workin7 ust as well.
$-$1 #o :ou need a button 9ow Jud7in7 the illustration on the ne1t 4a7e ! would say you donDt.
54
(. )&tending app#ication %e&t'#ay
Illustration &1: o button (ho" but still doing the same job
The Kuestions and their answers3 7 W9en do :ou want t9is to 9appen V #s soon as one o0 the !adioBo?es in +ra6e show is clic@ed V Radio'o1es *lick e2ent 7 W9e8e do :ou want t9is to 9appen V On 8bt9ow1E 8bt9ow% and 8bt9ow3 7 #o I 9a
55
(. )&tending app#ication %e&t'#ay
ublic Sub rbtShow8M*lick() !0 !sNull( tb1*ountry .Te1t) Then "essa7e(+irst 0ill in a countryP) Else "essa7e(The country entered is tb1*ountry.Te1t) Endi0 End *ode run and test is all that is le0t.
$-$% Could iwitc9 fo8mat be mo8e clea8 !t sure can be 6ore clear. LetDs look at the Kuestions and their answers3 7 W9at fo8mat could do bette8 V E1a64le3 :99 V 0irst ite6 needs to be shown ::9 V 0irst and second ite6 need to be shown ::: V all ite6s need to be shown 99: V third ite6 needs to be shown :9: V 0irst and third ite6 needs to be shown . 7 Can t9e cu88ent
$-$3 9ould t9e button Add be enabled if no Add is c9ec@ed No it should not be enabled. #7ain letDs ha2e a look at the Kuestions and answers3 7 W9en do :ou want t9is to 9appen V W9en fo8m opens no #dd is checked so button should be disabled V +or6 o4en e2ent V When one o0 the Add gets c9ec@ed button should be enabled V *heck'o1 *lick e2ent V When all # dds get deselected the button should disable V *heck'o1 *lick e2ent 7 W9e8e do :ou want t9is to 9appen V On &main and cb?&i8st)ameE cb?)ame and cb?Count8: *ode to disable the button #dd on o4enin7 the 0or63 ublic Sub +or6MO4en() "e.*enter btn#dd.Enabled +alse End *ode to enable disable button #dd when clickin7 cb1+irstNa6e cb1Na6e or cb1*ountry3 #s you will notice in code below you can run code 0ro6 one routine (bein7 cb1+irstNa6eM*lick) 0ro6 another routine (bein7 cb1Na6eM*lick and cb1*ountryM*lick).
56
(. )&tending app#ication %e&t'#ay
ublic Sub cb1+irstNa6eM*lick() !0 (cb1+irstNa6e./alue 9) #nd (cb1Na6e./alue 9) #nd (cb1*ountry./alue 9) Then btn#dd.Enabled +alse Else btn#dd.Enabled True Endi0 End D Since code 0or other checkbo1es is sa6e use cb1+irstNa6eM*lick routine to do the ob -ublic Sub cb1Na6eM*lick() cb1+irstNa6eM*lick End ublic Sub cb1*ountryM*lick() cb1+irstNa6eM*lick End $ou will 7et to see 6ore o0 this 0urther in the 7uide. +or now ust knowin7 that it is 4ossible will do.
$-$( 9ould t9e button Clea8 be enabled if t9e8e is not9ing to clea8 No it should not be enabled. #nd a7ain letDs ha2e a look at the Kuestions and answers3 7 W9en do :ou want t9is to 9appen V W9en fo8m opens no content so button should be disabled V +or6 o4en e2ent V When Te?t is Added button should be enabled V btn#dd *lick e2ent V When Te?t is Clea8ed button should be disabled V btn*lear *lick e2ent 7 W9e8e do :ou want t9is to 9appen V On &main and btnAdd and btnClea8 *ode to disable the button *lear on o4enin7 the 0 or63 ublic Sub +or6MO4en() "e.*enter btn#dd.Enabled +alse btn*lear.Enabled +alse End #dd this code to the btn#ddM*lick e2ent3
57
(. )&tending app#ication %e&t'#ay
ublic Sub btn#ddM*lick() . .. Endi0 D #dd this line at the end o0 the routine btn*lear.Enabled True End #dd this code to the btn*learM*lick e2ent3 ublic Sub btn*learM*lick() . .. tarResult.*lear D #dd this at the end o0 the routine D $ou need to set the 0ocus to another control be0ore disablin7 btn*lear as it has the 0ocus btn#dd.Set+ocus btn*lear.Enabled +alse End Since btn*lear has the 0ocus when bein7 clicked and you canDt disable a control that has the 0ocus you need to set the 0ocus to another control be0ore disablin7 btn*lear. $ou will notice that u4on clickin7 clear the #dds are deselected by code resultin7 in the #dd button bein7 disabled as well. !n this e1a64le that is ust 4er0ect. $ou 4robably didnDt e14ect that to ha44en but you did code 0or it. 'ut it is a 7ood de6onstration o0 e2ent dri2en 4ro7ra66in7. One e2ent can tri77er a ne1t e2ent that can tri77er a ne1t e2ent. So kee4 your 6ind to it not to end u4 with unwanted side e00ects co6in7 0ro6 4arts o0 could you would ne2er look 0or. Now close the 4roect in 6enu &ile ; Fuit. This will also close %a6bas and set you at the 4oint to start 0or the ne1t cha4ter.
58
*. +our second app#ication$ %e&t'#ay%,w-o
'ou8 second application/ Te?t,la:T4w5o .1 W#at *ill $e done in t#i" c#apter "ain 0ocus will be strin7 0unctions 0or that you will built a new a44lication. This will in2ol2e O4enin7 %a6bas !&E and 6ake a new 4roect ➢ "ake a 6ain 0or6 ➢ lace 0ourteen controls on the 0or6 (: Te1t#rea = Te1t'o1es ; buttons : 0ra6e ; ➢ Radio'uttons : /alue'o1 ; labels) "ake a search and re4lace 0or6 ➢ lace 0our controls on the 0or6 (; Te1t'o1es ; buttons) ➢ Write so6e code ➢ Run the a44lication 0ro6 %a6bas !&E ➢ $ou can learn about3 Workin7 with the %a6bas !&E ➢ Workin7 with 6ore than one 0or6 ➢ ow to interact between 0or6s ➢ "eanin7 o0 Subroutines ublic and ri2ate ➢ Workin7 with nu6bers ➢ Workin7 with Strin7 0unctions to search and re4lace te1t ➢ Workin7 with Strin7 0unctions to s4lit te1t and 6ore ➢ User interaction (in4ut 0ro6 user out4ut 0ro6 a44lication) ➢
.2 +tart a ne* pro'ect and de"ign t#e fir"t form O4en %a6bas8 !&E and start a new 7ra4hical a44lication 4roect na6ed Te1tlayT(w)o. Ri7ht click +6ain and select !ename$$ in the 6enu and na6e it +r6"ain. lace 0ollowin7 cont8ols on +r6"ain with so6e 4ro4erties 4re-set3 1 Te?tA8ea 3 Na6e V t1a*ontentY Te1t V e64ty 1 .abel3 Te1t V S4lit characterY #li7n6ent V Ri7ht 1 Te?tBo?3 Na6e V tb1S4lit Y Te1t V e64ty 1 .abel3 Te1t V Nu6ber o0 s4litsY #li7n6ent V Ri7ht 1 alueBo?3 Na6e V 2b1S4lit 1 Button3 Na6e V btnS4litY Te1t V S4lit ( Te?tBo?es3 Na6e V tb1Line:...tb1Line;) shows the 0or6 in the !&E desi7ner. Second illustration (>8) shows the 0or6 when run.
59
*. +our second app#ication$ %e&t'#ay%,w-o
Illustration &2: ain form in IDE design mode
Illustration &: ain form "hen run from IDE
.3 Pla)ing *it# text t#e ot#er *a) around !n Te1tlay(w)o you are 7oin7 to start with doin7 the o44osite o0 Te1tlay. !n Te1tlay you had in4ut in Te1t'o1es and it was 4ut to7ether in a Te1t#rea. !n this e1a64le you will 7et your in4ut in the Te1t#rea (one bi7 strin7) and need to s4lit it to 0it in the Te1t'o1es (se2eral strin7s).
$3$1 ome fi8st coding to split t9e te?t $ou start with so6e code to 6ake +r6"ain centre u4on o4enin7.
60
*. +our second app#ication$ %e&t'#ay%,w-o
ublic Sub +or6MO4en() "e.*enter End Ste4 :3 +ocus on s4littin7 the te1t What you need to acco64lish is s4lit entered te1t in the Te1t#rea (t1a*ontent) into the Te1t'o1es (tb1Line: to tb1Line<). +or that you need to know on what character to s4lit. E1a64le3 t1a*ontent.Te1t V s4lit this te1t on s4aceP tb1S4lit.Te1t V (a s4ace) ressin7 S4lit should result in3 tb1Line:.Te1t V s4litP tb1Line;.Te1t V thisP tb1Line8.Te1t V te1tP tb1Line<.Te1t V onP So in4ut is the te1t entered in t1a*ontent and the character entered in tb1S4lit. The s4littin7 needs to take 4lace when btnS4lit is clicked. +or s4littin7 strin7s you can use the 0unction S4lit(). t8ingA88a: S4lit( t8ing #s Strin7 epa8ato8s #s Strin7 "scape #s Strin7 ...) S4lit enables you to s4lit a strin7 usin7 the Se4arator to deter6ine where to S4lit the strin7. t8ing is the strin7 to s4lit epa8ato8s are the characters used to s4lit the te1t. 'y de0ault the se4erators are not returned. t8ingA88a: is an array o0 strin7s into where the results o0 S4lit are returned. So it contains all the s4litted strin7 results as se4arate strin7s in one array o0 strin7s. +plit #a" more option" t#an mentioned a$oe.
S4lit is a Strin7 +unction. Strin7 0unctions are 0unctions that can be used to do work with Strin7s. "ore on S4lit see htt43557a6basdoc.or75hel45lan75s4lit?28 O2er2iew o0 Strin7 +unctions see3 htt43557a6basdoc.or75hel45cat5strin7?28 #s the s4littin7 needs to take 4lace when btnS4lit is clicked you will start with codin7 this e2ent3 ublic Sub btnS4litM*lick() &i6 sS4lit #s Strin7 DStrin7 0ollowed by and declares a strin7 array &i6 sTe1t #s Strin7 &i6 i #s !nte7er i : D Used to deter6ine what t1bLine to out4ut to sS4lit S4lit(t1a*ontent.Te1t tb1S4lit.Te1t) +or Each sTe1t !n sS4lit Select i *ase : tb1Line:.Te1t sTe1t *ase ; tb1Line;.te1t sTe1t
61
*. +our second app#ication$ %e&t'#ay%,w-o
*ase 8 tb1Line8.Te1t sTe1t *ase < tb1Line<.Te1t sTe1t End Select i i Z : Ne1t End
+or Each a8iable !n "?p8ession Ne1t Re4eats a loo4 while enu6eratin7 the obect in e14ression "?p8ession 6ust be a re0erence to a enu6erable obect like a collection or an array a8iable is the re0erence to the ite6 in the enu6erable obect in "?p8ession !n abo2e code sTe1t (2ariable) stands 0or each strin7 ite6 in the strin7 array sS4lit (e14ression) "ore on +or Each see htt43557a6basdoc.or75hel45lan750oreach?28 Now run the a44lication and3 :. in t1a*ontent ty4e3 S4lit.On.&ot.lease :. in tb1S4lit ty4e3 . 8. click btnS4lit
Illustration &4: e3t splitted on the dot <%=
!t runs as e14ected but try a7ain with so6e other te1t in t1a*ontent en S4lit on GnP (line 0eed). Run the a44lication and3 :. in t1a*ontent ty4e3 This is Line : Ne1t line aka as line ;
62
*. +our second app#ication$ %e&t'#ay%,w-o
Line 8 abo2e is e64ty and this is line < :. in tb1S4lit ty4e3 Gn 8. click btnS4lit
Illustration &$: 8esult of splitting on >n
Now this 6akes sense as +eparator" are the characters used to split the tet P So S4lit will check 0or both GP and nP to s4lit and that is e1actly what ha44ened. Ste4 ;3 !64ro2e s4littin7 the te1t What you want is it to s4lit on a line 0eed (GnP) instead. So you need to code the btnS4lit e2ent to reco7niIe the GnP and act accordin7ly. +or this you need to add an e1tra GP :=. So back to !&E 0or so6e recodin73 ublic Sub btnS4litM*lick() &i6 sS4lit #s Strin7 DStrin7 0ollowed by and declares a strin7 array &i6 sTe1t #s Strin7 &i6 i #s !nte7er i : D Used to deter6ine what t1bLine to out4ut to Select tb1S4lit.Te1t *ase GGnP D To check 0or a line 0eed tb1S4lit.Te1t *hr(:9) D To s4lit on a line 0eed (*hr(:9)) End Select sS4lit S4lit(t1a*ontent.Te1t tb1S4lit.Te1t) +or Each sTe1t !n sS4lit Select i *ase : tb1Line:.Te1t sTe1t *ase ; tb1Line;.te1t sTe1t *ase 8 tb1Line8.Te1t sTe1t
12
The extra BEC maes sure the following BEC is #reserved.
63
*. +our second app#ication$ %e&t'#ay%,w-o
*ase < tb1Line<.Te1t sTe1t End Select i i Z : Ne1t End
*hr(Code #s !nte7er) #s Strin7 Returns the character 0or the Code (with *ode bein7 the #S*!! code) Code needs to be a !nte7er 2alue 0ro6 9 to ;== !n abo2e code *hr(:9) stands 0or a line 0eed. "ore on *hr() see htt43557a6basdoc.or75hel45lan75chr?28 O2er2iew o0 Strin7 +unctions see3 htt43557a6basdoc.or75hel45cat5strin7?28 Now run the a44lication a7ain and do the sa6e test as done be0ore.
Illustration &&: 8ene"ed result of splitting on ?>n?
See6s you ha2e a 4er0ect s4lit on GnP or a line 0eed. Note that once clickin7 btnS4lit the content o0 tb1S4lit contains *hr(:9). 'ecause Te1t'o1es are sin7le line they re4resent it with this sy6bol instead o0 7oin7 to a new line.
$3$% #ete8mine t9e numbe8 of splits Now that the s4lit works as needed 0ocus is on the nu6bers o0 s4lits you want. Ste4 :3 !64le6ent to take nu6ber o0 s4lits into account #s you ha2e < lines to show the s4litted strin7s it would be 7reat i0 you entered ; in nu6ber o0 s4lits and only ; where shown or enter : and only one is shown. So you will a7ain need to recode btnS4lit e2ent to 6ake that ha44en3
64
*. +our second app#ication$ %e&t'#ay%,w-o
ublic Sub btnS4litM*lick() &i6 sS4lit #s Strin7 DStrin7 0ollowed by and declares a strin7 array &i6 sTe1t #s Strin7 &i6 i iNu6S4lits #s !nte7er i : D Used to deter6ine what t1bLine to out4ut to iNu6S4lits 2b1S4lit./alue D Used to deter6ine nu6ber o0 s4lits Select tb1S4lit.Te1t *ase GGnP D To check 0or a line 0eed tb1S4lit.Te1t *hr(:9) D To s4lit on a line 0eed (*hr(:9)) End Select sS4lit S4lit(t1a*ontent.Te1t tb1S4lit.Te1t) +or Each sTe1t !n sS4lit !0 i iNu6S4lits Then Select i *ase : tb1Line:.Te1t sTe1t *ase ; tb1Line;.te1t sTe1t *ase 8 tb1Line8.Te1t sTe1t *ase < tb1Line<.Te1t sTe1t End Select i i Z : Endi0 Ne1t End Now check i0 it works by runnin7 the a44lication
Illustration &*: 8esult of splitting on ?>n? sho"ing 2 lines
65
*. +our second app#ication$ %e&t'#ay%,w-o
$ou will notice that when showin7 < lines and ne1t tryin7 ; lines that lines 8 and < are still 0illed. The reason behind this is that the tb1Line 2ariables arenDt cleared be0ore a new s4lit. Ste4 ;3 Recode to 6ake tb1Line 2ariables clear To do this you need to code the btnS4lit e2ent a7ain3 ublic Sub btnS4litM*lick() &i6 sS4lit #s Strin7 DStrin7 0ollowed by and declares a strin7 array &i6 sTe1t #s Strin7 &i6 i iNu6S4lits #s !nte7er i : D Used to deter6ine what t1bLine to out4ut to iNu6S4lits 2b1S4lit./alue D Used to deter6ine nu6ber o0 s4lits *learLines DThis starts a subroutine Select tb1S4lit.Te1t *ase GGnP D To check 0or a line 0eed tb1S4lit.Te1t *hr(:9) D To s4lit on a line 0eed (*hr(:9)) End Select sS4lit S4lit(t1a*ontent.Te1t tb1S4lit.Te1t) +or Each sTe1t !n sS4lit !0 i iNu6S4lits Then Select i *ase : tb1Line:.Te1t sTe1t *ase ; tb1Line;.te1t sTe1t *ase 8 tb1Line8.Te1t sTe1t *ase < tb1Line<.Te1t sTe1t End Select i i Z : Endi0 Ne1t End ri2ate Sub *learLines()
End #s you can see in the code abo2e the code 0or clearin7 the lines is taken into a se4arate routine (ublic Sub *learLines()). This se4arate routine is a subroutine (indicated by the Sub). !t is a ri2ate routine (indicated by ri2ate). The 0act that the routine is a subroutine and ri2ate 6eans that it can be called anywhere within the sa6e class it resides to be e1ecuted. The class subroutine *learLines() resides in the 0or6 class o0 +r6"ain (the tab in !&E na6ed +r6"ain.*lass). So anywhere in the code o0 +r6"ain you can use subrourine *learLines().
66
*. +our second app#ication$ %e&t'#ay%,w-o
$ou call the routine si64ly by its na6e to be e1ecuted (in our code you si64ly write *learLines). U4on e1ecution the inter4reter a0ter e1ecutin7 the line DiNu6S4lits 2b1S4lit./alueD will run the subroutine *learLines and e1ecute it line by line be0ore continuin7 with the e1ecution o0 DSelect tb1S4lit.Te1tD Static [ublic \ ri2ate] [rocedure \ Sub] Identifie8 (,a8amete8 #s &ataty4e.....) Identifie8 is the na6e o0 the subroutine or 4rocedure ,a8amete8 is5are 2alues that can be 4assed to the subroutine54rocedure when callin7 it. $ou ha2e to declare a data ty4e 0or each pa8amete8 used. # subroutine or 4rocedure does not return any 2alues. Use a 0unction 0or that. "ore on Sub see htt43557a6basdoc.or75hel45lan75sub?28
,8i
"ore on ri2ate see htt43557a6basdoc.or75hel45lan754ri2ate?28
,ublic ,eyword that declares accessibility o0 a 6ethod (sub54rocedure or 0unction) 2ariable or 4ro4erty # ublic declared 6ethod 2ariable or 4ro4erty can be used outside its own class.
"ore on ri2ate see htt43557a6basdoc.or75hel45lan754ublic?28 Now run the a44lication a7ain and 0irst let it s4lit showin7 < lines
Illustration &+: (plit for 4 lines
Ne1t s4lit a7ain 0or : line and see i0 lines ; to < are cleared (see illustration on ne1t 4a7e).
67
*. +our second app#ication$ %e&t'#ay%,w-o
Illustration &.: e3t a split for 1 line
See6s that all is workin7 as e14ected.
.4 +earc# for text +or this 4art you 0irst need to 6ake a new 0or6. The new 0or6 is 7oin7 to ser2e as a window to enter the search strin7.
$($1 +a@e a new fo8m To 6ake a new 0or6 in the ,8oect B8owse8 ri7ht click ou8ces. !n the 6enu select )ew ; &o8m.
Illustration *0: !d a ne" form to the project
68
*. +our second app#ication$ %e&t'#ay%,w-o
Na6e the 0or6 +r6SearchRe4lace.
Illustration *1: Dialog to name the form
lace 0ollowin7 cont8ols on +r6SearchRe4lace with so6e 4ro4erties 4re-set3 1 Te?tBo?3 Na6e V tb1Search Y Te1t V Enter a strin7 to search... 1 Button3 Na6e V btnSearchY Te1t V Search
Now add so6e initial code to the 0or6 o4en e2ent (ri7ht click the 0or6 and select 6enu "
$($% +a@e &8mea8c9!eplace open f8om btnea8c9 on &8m+ain &ouble click &8m+ain in the ,8oect B8owse8 and ne1t select btnea8c9$ #s you 6i7ht re6e6ber btnSearch was disabled. So in btnea8c9 4ro4erties set enabled to True. Ne1t ri7ht click the selected btnea8c9 and in the 6enu select "
The +or6 class inherits the Window class. This 6eans that 4ro4erties 6ethods (routines) and e2ents o0 the Windows class (4arent class to the 0or6 class) are inherited 0ro6 the window class e2ery ti6e a 0or6 is 6ade. This also 6eans that you can use these in your 0or6s. Sub 9ow () Shows the window "ore on +or6s see htt43557a6basdoc.or75hel45co6457b.Kt<50or6?28 "ore on Show see htt43557a6basdoc.or75hel45co6457b.Kt<5window5show?28 Now run the a44lication and click the btnSearch. $ou should see so6ethin7 like the illustration on the ne1t 4a7e. Notice the window ca4tion o0 +r6SearchRe4lace statin7 Search.
70
*. +our second app#ication$ %e&t'#ay%,w-o
Illustration *: 5rm(earch8eplace opened from btn(earch
See6s you now ha2e an a44lication consistin7 o0 two 0or6s. #nd 0ro6 within one 0or6 you can o4en the other. Now the real challen7e is to enter a search strin7 in the +r6SearchRe4lace and search 0or the strin7 in t1a*ontent on +r6"ain. This will reKuire so6e interaction between the two 0or6s and brin7s us to a ne1t le2el.
$($3 ,8oect inte8meKKo/ &o8m class inte8action e?plained $ou already had a 0irst 7li64se at ri2ate and ublic declarations. With two 0or6s at hand this is 7oin7 to start 6akin7 sense. The reKuire6ent o0 ha2in7 to search a strin7 entered in a Te1t'o1 on one 0or6 to be searched in a Te1t#rea on another 0or6 and then ha2e the result selected in that Te1t#rea reKuires you to understand the di00erence in ri2ate and ublic. They are the basis o0 what will and will not be a2ailable to other +or6 classes to use in their code. 'y de0ault coded e2ents (na6ed subs 4rocedures or 6ethods) o0 controls you draw on a 0or6 are ublic. So in our 4roect what can one 0or6 use 0ro6 the other? LetDs 6ake an o2er2iew and see what is a2ailable and what is not. ,ublic in +r6SearchRe4lace are3 +et9ods +or6MO4en ,8i
71
*. +our second app#ication$ %e&t'#ay%,w-o
btnS4litM*lick() btnSearchM*lick ,8i
Illustration *4: 8unning btn(plit code from btn(earch on another form
What actually ha44ened u4on clickin7 btnSearch on the Search 0or6 is that code 0ro6 another 0or6 class 7ot called by the current 0or6 *lass and e1ecuted be0ore returnin7. So you can directly call ublic 6ethods 0ro6 another class to be e1ecuted. !ndirectly the ri2ate 6ethod *learLines 7ot called as well. With this di00erence that it was called 0ro6 a 6ethod (btnS4litM*lick()) within its own class.
72
*. +our second app#ication$ %e&t'#ay%,w-o
Illustration *$: Interaction bet"een methods in different form classes
$ou can now re6o2e the code 0ro6 btnSearch in +r6SearchRe4lace class as it was ust an e1a64le. 'ack to our 4roble6 to sol2e in this 4roect. There are two 4ossible a44roaches3 1$ Code t9e sea8c9ing on fo8m &8mea8c9!eplace %$ Code t9e sea8c9ing on fo8m &8m+ain !n both a44roaches it co6es down to 7et the strin7 that needs to be searched and 4er0or6 a search. The code 0or searchin7 will not be that di00erent. What it co6es down to is what is the best a44roach in 7ettin7 the strin7 and ne1t selectin7 it when 0ound. LetDs ha2e a look at both a44roaches and see what 0its best. 1$ Code t9e sea8c9ing on fo8m &8mea8c9!eplace So what you need to 7et is the content 0ro6 t1a*ontent.Te1t. This 6eans declarin7 a ublic 2ariable as Strin7 and 6ake sure e2ery ti6e t1a*ontent.Te1t is chan7ed the strin7 is 4assed to the 2ariable. Ne1t search can be 4er0or6ed and then so6ehow the result has to be selected in t1a*ontent while it isnDt e2en ublicXX #ll in all a tou7h road to walk that reKuires a lot o0 e1tra codin7. %$ Code t9e sea8c9ing on fo8m &8m+ain What you need here is 4ass the strin7 to be searched 0ro6 +r6SearchRe4lace to +r6"ain 4er0or6 the search and select the result in t1a*ontent. #s all takes 4lace on +r6"ain this is a 6uch easier a44roach. #ll that needs to be done is 4ass a strin7 0ro6 one 0or6 to another nothin7 6ore. What you need is a 6ethod (sub or 4rocedure) on +r6"ain that can 4ass an ar7u6ent 4er0or6 the search and select the result.
$($( Bac@ on t8ac@ to code a ,ublic met9od on &8m+ain +irst check out how 4assin7 ar7u6ents in a 6ethods works.
73
*. +our second app#ication$ %e&t'#ay%,w-o
!n +r6"ain class write 0ollowin7 code3 ublic Sub SearchStrin7(Strin7ToSearch #s Strin7) "essa7e(Strin7ToSearch) End Strin7ToSearch is a strin7 2alue that 7ets 4assed to the 6ethod u4on callin7 it. !n +r6SearchRe4lace class write 0ollowin7 code3
ublic Sub btnSearchM*lick()
+r6"ain.SearchStrin7(tb1Search.Te1t)
End Now run the a44lication click btnSearch on +r6"ain write your na6e in the search 0ield and click btnSearch. $ou should see your na6e a44ear in a 6essa7e bo1. So the Te1t 4ro4erty o0 tb1Search (althou7h not bein7 ublic) is 4assed to another class as an ar7u6ent (Strin7ToSearch) to be used in a ublic 6ethod in that other class. So 4assin7 ar7u6ents is one way o0 4assin7 ri2ate data to another *lass. ,ee4 that in 6indX
Illustration *&: (earch string passed as argument of a ublic method
! ho4e this 6akes clear how 4assin7 ar7u6ents works. Now that you ha2e seen it workin7 you know the search strin7 can reach 6ethod SearchStrin7 in +r6"ain all you need to do is code 0or it to be 0ound.
$($* .ets sea8c9 some st8ing Search a strin7 and select it in tar*ontent when 0ound is the 6ission to co64lete. +or that you need to code the 6ethod SearchStrin7 in +r6"ain class3
74
*. +our second app#ication$ %e&t'#ay%,w-o
ublic Sub SearchStrin7(Strin7ToSearch #s Strin7) &i6 i' iL #s !nte7er i' : i' !nStr(tar*ontent.Te1t !nStr(tar*ontent.Te1t Strin7T Strin7ToSearch oSearch i') D&eter6ines the start 4osition o0 strin7 0ound iL Len(Strin7T Len(Stri n7ToSearch) oSearch) D&eter6ines D&eter6in es len7th o0 the strin7 !0 iL F 9 Then tar*ontent.Select(i' - : iL) DSelect the 0ound strin7 only when len7th F 9 Endi0 End
,osition !nstr !ns tr((t8ing #s Strin7 ubst8ing #s Strin7 Strin7 ta8t #s !nte7er !nte7e r ...) ... ) !nstr enables you to search a strin7 to deter6ine its start 4osition. ,osition is the start 4osition o0 the strin7 0ound (9 when nothin7 0ound) t8ing is the strin7 2alue that will be searched ubt8ing is the strin7 2alue that you are searchin7 0or ta8t is the 4osition you want the search to be7in n"tr #a" more option" t#an mentioned a$oe.
!nstr is a Strin7 +unction. Strin7 0unctions are 0unctions that can be used to do work with Strin7s. "ore on !nstr see htt43557a6basdoc.or75hel45lan75instr?28 O2er2iew o0 Strin7 +unctions see3 htt43557a6basdoc.or75hel45cat5strin7?28
.engt9 Len( Le n( A8gs #s Strin7) Stri n7) Len enables you to deter6ine the len7th o0 a strin7 .engt9 is the len7th o0 the strin7 in ar7u6ents A8gs (9 when e64ty) A8gs is the strin7 2alue you want to deter6ine the len7th o0 eng#t i" determined $) $)te count of t#e "tring. 0+! c#aracter" take up one $)te. !#aracter" like or 6 78T&9- take up t*o $)te" and t#u" *ill count for t*o
Len is a Strin7 +unction. Strin7 0unctions are 0unctions that can be used to do work with Strin7s. "ore on Len see htt43557a6basdoc.or75hel45lan75len?28 O2er2iew o0 Strin7 +unctions see3 htt43557a6basdoc.or75hel45cat5strin7?28 Now run the a44lication and do so6e testin7. !n t1a*ontent write3 ! a6 lookin7 0or 6y na6e a2e you seen 6y na6e !t is Willy they tell 6e lease search 0or 6y na6e Ne1t click btnSearch on the 6ain 0or6 to o4en the Search 0or6. !n the Search 0or6 ty4e WillyP (without the Kuotes) and click Search. Willy should be selected in t1a*ontent on the 6ain 0or6.
75
*. +our second app#ication$ %e&t'#ay%,w-o
Illustration **: (eems li#e search performs as e3pected
#ll should work as e14ected and select the searched strin7 when 0ound select nothin7 when nothin7 is 0ound.
.5 .5 +ear +earc# c# and and repl replac ace e text text 'e0ore you can start searchin7 and re4lacin7 strin7 you will need to add so6e controls to you current +r6SearchRe4lace. +r6SearchRe4lace.
$*$1 ,8epa8e ,8epa8e t9e t9e fo8m fo8ms s fo8 fo8 ea8c9 ea8c9 and !eplace !eplace !n the !&E click +r6SearchRe4lace +r6SearchRe4lace in the ,8oect B8owse8. lace 0ollowin7 cont8ols on +r6SearchRe4lace with so6e 4ro4erties 4ro4erties 4re-set3 4re-set3 1 Te?tBo?3 Na6e Na6e V V tb1Re4lace Y Te1t Te1t V V Enter the re4lace strin7... 1 Button3 Na6e Na6e V V btnRe4laceY Te1t Te1t V V Re4lace +astest 6ethod is to select tb1Search and btnSeach co4y and 4aste the6 onto the 0or6 dra7 the6 to the t he 4ro4er location and ada4t the 4ro4erties Na6e and Te1t. Te1t.
Illustration *+: 8eplace e3t6o3 and 6utton added to 5rm(earch8eplace
Ne1t click +r6"ain in the ,8oect B8owse8 Select 0raSearch and set the 4ro4erty "nabled to True
76
*. +our second app#ication$ %e&t'#ay%,w-o
Illustration *.: 5rm(earch propert) Enabled set to true on 5rmain
With the 0ra6e 0or 0 or choosin7 between Search and Search5Re4lace enabled enabled and +r6SearchRe4lace 4re4ared 4re4ared 0or Re4lacin7 Re4lacin7 you are set 0or the ne1t ste4.
$*$% +a@ing &8mea8c9 &8mea8c9!epla !eplace ce appea8 appea8 dependin depending g on selection selection in &8m+a &8m+ain in When clickin7 button Search on +r6"ain you want +r6SearchRe4lace to a44ear di00erent de4endin7 on what was selected in the 0raSearch (a +ra6e). When Search is selected +r6SearchRe4lace +r6SearchRe4lace has to3 t o3 - ha2e ca4tion Search - show search 0ield - show search button When Search5Re4lace is selected +r6SearchRe4lace has to3 - ha2e ca4tion Search and Re4lace - show search and re4lace 0ields - show search button - show re4lace button $ou will start with +r6"ain to 7et the 0raSearch and Radio'uttons to work3 D -- Used to deter6ine how +r6SearchRe4lace is shown :search ;re4lace -ublic SearchRe4lace #s 'yte ublic Sub +or6MO4en() "e.*enter rbtSearch./alue rbtSearc h./alue True rbtSearchM*lick End $ou use ublic to declare a 2ariable SearchRe4lace to 4ass a 2alue to +r6SearchRe4lace to deter6ine whether to search or to search and re4lace. rbtSearch is set to True so it is the de0ault selection u4on o4enin7 +r6"ain. Ne1t you e1 ecute the rbtSearch click e2ent. This is the ne1t ste4 you need to code. *lickin7 on one o0 the Radio'uttons has to set SearchRe4lace to 2alue : or ; that is all.
77
*. +our second app#ication$ %e&t'#ay%,w-o
#atat:pes &ataty4es deter6ine the ty4e o0 data a 2ariable can contain when declared. The dataty4e also deter6ines the len7th o0 the s4ace used by the data.