Вы находитесь на странице: 1из 177

How to Gambas 3

Building GUI Applications


version 0.0.1 Pre-release 1

Main Author: Willy Raets (The Netherlands) <willy@development.earthshipbelgium.be> Version: 0.0. !re"release (#0 $"0%"#%)

Contributing author: " Illustrations: &hol'y ((nited &tated)) *llustrations +$ Willy Raets (The Netherlands)) *llustrations " +#) +," +Proofreaders: .lain /. 0audre' (0elgium) .mmar .li 1urd (2emen) /im 3oo4 ((nited &tates) Neti'en %%$ ((nited &tates) Instruction and example testing .lain /. 0audre' (0elgium) .mmar .li 1urd (2emen) /im 3oo4 ((nited &tates) Neti'en %%$ ((nited &tates) Advisors: .lain /. 0audre' (0elgium) *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 8or a detailed e:planation. ;or 5onditions o8 use prohibited by this li5ense) 5onta5t the li5ensor. T<*& W=R1 *& !R=>*6?6 (N6?R T<? T?R@& =; T<*& 3R?.T*>? 3=@@=N& !(07*3 7*3?N&? (A33!7A =R A7*3?N&?A). T<*& W=R1 *& !R=T?3T?6 02 3=!2R*B<T .N6C=R =T<?R .!!7*3.07? 7.W. .N2 (&? =; T<*& W=R1 =T<?R T<.N .& .(T<=R*D?6 (N6?R T<*& 7*3?N&? =R 3=!2R*B<T 7.W *& !R=<*0" *T?6. UNLESS OT ERW!SE "UTU#LL$ #%REE& '$ T E (#RT!ES !N WR!T!N%) L!*ENSOR O++ERS T !S WOR, #S-!S #N& "#,ES NO RE(RESENT#T!ONS OR W#RR#NT!ES O+ #N$ ,!N& *ON*ERN!N% T !S WOR,) E.(RESS) !"(L!E&) ST#TUTOR$ OR OT ERW!SE) !N*LU&!N%) W!T OUT L!"!T#T!ON) W#RR#NT!ES O+ T!TLE) "ER* #NT!'!L!T$) +!TNESS +OR # (#RT!*UL#R (UR(OSE) NON!N+R!N%E"ENT) OR T E #'SEN*E O+ L#TENT OR OT ER &E+E*TS) #**UR#*$) OR T E (RESEN*E O+ #'SEN*E O+ ERRORS) W ET ER OR NOT &!S*O/ER#'LE. SO"E JUR!S&!*T!ONS &O NOT #LLOW T E 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,

Table of Contents
1 HOW THI GOT TA!T"#$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$1 % &O!"WO!#$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$% 3 B"&O!" 'OU G"T TA!T"#$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$3 8.: '#*,%ROUN& !N+OR"#T!ON......................................................................................................8 8.; E.#"(LE *O&E.....................................................................................................................8 8.8 %OO& (R#*T!*E...................................................................................................................8 8.< TER"!N#L.............................................................................................................................8 8.= W#RN!N%.............................................................................................................................< 8.> *ONTROLS #N& "ENUS...........................................................................................................< ( I)T!O#UCTIO)$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$* <.: W #T !S %#"'#S?................................................................................................................= * O+" GOO# ,!ACTIC" $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$=.: +OL&ER OR%#N!S#T!ON............................................................................................................> 5.1.1 Development folder........................................................................................................ 6 5.1.2 Distributions folder......................................................................................................... 7 =.; (ROJE*T OR%#N!S#T!ON..........................................................................................................@ =.8 OR%#N!S!N% $OUR *O&E.........................................................................................................@ 5.3.1 Commenting your code................................................................................................ 10 5.3.2 Clear choices for names.............................................................................................. 11 5.3.3 ndenting code............................................................................................................. 12 5.3.! "liminate repeating code.............................................................................................. 13

- A &I! T GUI A,,.ICATIO)/ T"0T,.A'$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$1( >.: W #T W!LL 'E &ONE !N T !S * #(TER......................................................................................:< >.; ST#RT!N% # NEW (ROJE*T.....................................................................................................:< >.8 *RE#T!N% # +OR" #N& *ONTROLS...........................................................................................:A
6.3.1 #lacing controls on a form............................................................................................ 1$ 6.3.2 % closer loo& at the controls......................................................................................... 22

>.< *O&!N%) TEST!N%) *O&!N%) TEST!N%.........................................................................................;< 6.!.1 'b(ects) properties) events and methods.....................................................................2! 6.!.2 *rite some code and test +round 1,.............................................................................2! 6.!.3 Code some improvements and test +round 2,..............................................................27

1 "0T")#I)G A,,.ICATIO) T"0T,.A'$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$32 B.: W #T W!LL 'E &ONE !N T !S * #(TER......................................................................................89 B.; O(EN #N E.!ST!N% (ROJE*T...................................................................................................89 B.8 "O&!+$ "#!N +OR" #N& #&& *ONTROLS....................................................................................8:
7.3.1 Copy controls on the form............................................................................................ 33 7.3.2 % closer loo& at the controls......................................................................................... 3!

B.< *O&!N%) TEST!N% #N& #&#(T!N%.............................................................................................8B 7.!.1 Coding and testing +round 1,........................................................................................ 37 7.!.2 %dapting) coding and testing +round 2,.........................................................................37.!.3 %dding ne. controls) code and test +round 3,..............................................................!1 7.!.! %dd some last controls) code and test +round !,..........................................................!6 B.= # L#ST RE/!EW O+ T E #((L!*#T!ON.........................................................................................=8 7.5.1 Do you need a button /ho.0.......................................................................................53 7.5.2 Could i/.itch format be more clear0...........................................................................53 7.5.3 /hould the button %dd be enabled if no %dd is chec&ed0............................................5!

7.5.! /hould the button Clear be enabled if there is nothing to clear0..................................5! B.> #NSWERS TO T E L#ST RE/!EW ...............................................................................................=< 7.6.1 Do you need a button /ho.0.......................................................................................5! 7.6.2 Could i/.itch format be more clear0...........................................................................56 7.6.3 /hould the button %dd be enabled if no %dd is chec&ed0............................................56 7.6.! /hould the button Clear be enabled if there is nothing to clear0..................................57

3 '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 T E +!RST +OR"...................................................................=@ A.8 (L#$!N% W!T TE.T T E OT ER W#$ #ROUN&.............................................................................>9
-.3.1 /ome first coding to split the te1t.................................................................................60 -.3.2 Determine the number of splits....................................................................................6!

A.< SE#R* +OR TE.T...............................................................................................................>A -.!.1 2a&e a ne. form......................................................................................................... 6-.!.2 2a&e 3rm/earch4eplace open from btn/earch on 3rm2ain......................................70 -.!.3 #ro(ect interme55o6 3orm class interaction e1plained..................................................71 -.!.! 7ac& on trac& to code a #ublic method on 3rm2ain....................................................73 -.!.5 8ets search some string............................................................................................... 7! A.= SE#R* #N& RE(L#*E TE.T...................................................................................................B> -.5.1 #repare the forms for /earch and 4eplace..................................................................76 -.5.2 2a&ing 3rm/earch4eplace appear depending on selection in 3rm2ain.....................77 -.5.3 4eplace searched strings............................................................................................ -0

6 'OU! THI!# A,,.ICATIO)/ )U+,.A'$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$3% @.: W #T W!LL 'E &ONE !N T !S * #(TER......................................................................................A; @.; ST#RT # NEW (ROJE*T #N& &ES!%N # +OR"...............................................................................A; @.8 &O!N% T E "#T S................................................................................................................A8
$.3.1 %nalyse the needs and determine a strategy...............................................................-! $.3.2 mplement error handling............................................................................................. --

@.< WOR,!N% W!T NU"'ERS !N TE.T /#R!#'LES #N& TE.T *ONTROLS..................................................@9 $.!.1 /imple beginnings........................................................................................................ $1 $.!.2 9e1t challenge............................................................................................................. $2 @.= OW #'OUT &#TE #N& T!"E...................................................................................................@= $.5.1 %dding some code....................................................................................................... $6 $.5.2 2ore date and time...................................................................................................... $7 @.> #NOT ER #((RO#* ) ENU"ER#TE............................................................................................@@ $.6.1 i/.itch enumerated..................................................................................................... $$

12 'OU! &OU!TH A,,.ICATIO)/ I+AG",.A'$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$12% :9.: W #T W!LL 'E &ONE !N T !S * #(TER..................................................................................:9; :9.; ST#RT # NEW (ROJE*T #N& &ES!%N # +OR"..........................................................................:9; :9.8 LET "E SEE SO"E !"#%ES.................................................................................................:98
10.3.1 2a&ing some images............................................................................................... 103 10.3.2 2a&e the images appear.......................................................................................... 10! 10.3.3 Code the labels........................................................................................................ 107

:9.< LO#&!N% !"#%ES US!N% *O&E.............................................................................................::: 10.!.1 :he mission............................................................................................................. 112 10.!.2 2a&e the labels .or& again......................................................................................113

11 'OU! &I&TH A,,.ICATIO)/ I+,."T"0T"#ITO!$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$11* ::.: W #T W!LL 'E &ONE !N T !S * #(TER..................................................................................::= ::.; ST#RT # NEW (ROJE*T #N& &ES!%N # +OR"..........................................................................::= ::.8 "#,!N% # "ENU..............................................................................................................::>
11.3.1 Coding the menu entry 9e......................................................................................1111.3.2 %dding con and &eyboard shortcut to menu entry 9e............................................11$

::.< S#/!N% $OUR +!RST TE.T +!LE.............................................................................................:;9 11.!.1 'n a mission............................................................................................................ 120 11.!.2 8ets save some te1t................................................................................................. 121 11.!.3 ;et name and path to save to..................................................................................123 ::.= O(EN!N% $OUR +!RST TE.T +!LE..........................................................................................:;> 11.5.1 Code for opening..................................................................................................... 126 11.5.2 2a&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 ::.A (UTT!N% T E &OTS ON T E !...............................................................................................:8< 11.-.1 'ffer to save .hen closed after editing te1t file........................................................135 11.-.2 7ug<< *hat bug0<..................................................................................................... 1311.-.3 4esi5ing the application .indo...............................................................................1!1 11.-.! %dding a :ool7ar...................................................................................................... 1!3 11.-.5 4eordering menu items............................................................................................ 1!7 11.-.6 %dding ne. menus................................................................................................... 1!11.-.7 %pplication caption................................................................................................... 150 ::.@ # L#ST NOTE ON *O&E OR%#N!S#T!ON...................................................................................:=8

1% 13 1(

")# )OT" &O! ,!"7!"."A " 1$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$1** A,,")#IC" $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ I BIB.IOG!A,H'$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$0II

.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 4roEect 0older in the %a6bas !&E.....................................@ !llustration =3 %a6bas 8.<.: o4enin7 screen...................................................................:< !llustration >3 DNew 4roEectD screen ste4 :. Select the ty4e o0 a44lication........................:= !llustration B3 DNew 4roEectD screen ste4 ;. Select the 0older 0or the new 4roEect..............:= !llustration A3 *reate a new 0older 0or Learnin7 4roEects..................................................:> !llustration @3 Select the 0older D%a6bas8-Learnin7D........................................................:> !llustration :93 DNew 4roEectD screen ste4 8. Na6e the 4roEect 0older and 4roEect ...........:B !llustration ::3 "essa7e when 4roEect has been created success0ully............................:B !llustration :;3 New 4roEect DTe1t(layD o4ened in %a6bas 8 !&E.....................................:A !llustration :83 +"ain in Sources 0older o0 the 4roEect.....................................................: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 4roEectsD.............................................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 <B3 Set selected controls NoTab+ocus 4ro4erty to True................................8@ !llustration <A3 *lickin7 #dd without 0illin7 in a na6e.......................................................<9 !llustration <@3 Three *heck'o1es and three Radio'uttons............................................<: !llustration =93 #ll *heck'o1es selected) only one Radio'utton......................................<: !llustration =:3 Result o0 ; lines selected.........................................................................<8 !llustration =;3 *licked button #dd with only Na6e selected...........................................<= !llustration =83 New controls added and ada4ted............................................................<> !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 Eob............................................== !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 4roEect....................................................................>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 Eob 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 obEect can be 0ound in the toolbo1 on tab DS4ecialD.........................@A !llustration @@3 Ti6er obEect 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 4roEect &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 !6a7e(lay 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 (roEect 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 :<B3 The Te1t#rea now does resiIe with the window...................................:<; !llustration :<A3 #rran7e6ent set to +ill does the Eob.....................................................:<; !llustration :<@3 'o1 is 0ound on the Tab *ontainer.....................................................:<8 !llustration :=93 #ll new controls in 4lace.......................................................................:<8 !llustration :=:3 # stran7e lookin7 0or6) but New button works.....................................:<< !llustration :=;3 Set t1a"ain 4ro4erty E14and to True...................................................:<< !llustration :=83 "ake sure all Tool'uttons are inside the 'o1....................................:<= !llustration :=<3 # runnin7 resiIeable Si64eTe1tEditor with Toolbar..............................:<> !llustration :==3 Toolbar with 4ro4erty S4acin7 set to +alse..........................................:<B !llustration :=>3 Toolbar with 4ro4erty S4acin7 set to True............................................:<B !llustration :=B3 # click on "o2e U4 button brin7s the ite6 hi7her................................:<B !llustration :=A3 #dded 6enus with Tool'ar 6enu ite6 set checked.............................:<A !llustration :=@3 # new 6enu /iew Toolbar....................................................................:<A !llustration :>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 J +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 Eoin)................../! !llustration :B<3 Search) (ost since last 2isit and To4ics with unread 4osts..................../!! !llustration :B=3 %a6bas based 4roEects with sub 0oru6 D ow to %a6bas 8D................/!!! !llustration :B>3 Sub 0oru6s and to4ics o0 D ow 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 D ow to %a6bas 8D 7uides......................................./!!! #44endi1 /!3 #44lication %a6bas Learnin7 L a learnin7 aid 0or %a6bas.......................... #44endi1 /!!3 O2er2iew o0 the D ow 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 Eob 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 Eust 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. EnEoy... 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 D ow 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%(

2. Foreword

% &o8ewo8d
This 7uide is the second in the series D ow 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! 4roEects 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 e14lained 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) obEects) 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 4roEects) %a6bas co64onents and their 0unction and 6aybe e2en 6ore will be e14lained. +or now enEoy 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 0a6iliar 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.

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. D ow to %a6bas 8 L !nstallin7 %a6basD co2ers the installation o0 %a6bas in detail and 0or di00erent distributions. $ou will also need to know so6e con2entions used in this 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 N OSa64le 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 li-ely 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. 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. Introduction

( Int8oduction
4.1 W#at i" Gam$a"%
#s the %a6bas website states3 D%a6bas al6ost 6eans 'asicD. With the DGD 0or %a6bas) the DaD 0or al6ost) the DmD 0or 6eans and the DbasD 0or 'asic. # nice little 4layin7 on words. #ccordin7 to the o00icial website DGam$a" is a 0ree de2elo46ent en2iron6ent based on a 7a= sic inter4reter with obEect e1tensionsD. !t is co64arable to a 4o4ular counter4art on an 6ainstrea6 4lat0or6. The 4erson behind the 4roEect 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 0unctionality 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 4roEect so you can easily browse 0ro6 one 0or6 desi7n to another and see other 0iles like icons contained in the 4roEect. 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 4roEect 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. ObEect-oriented 4ro7ra66in7 (OO() is a 4ro7ra66in7 4aradi76 usin7 HobEectsH L data structures consistin7 o0 data 0ields and 6ethods to7ether with their interactions L 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.or75wiki5ObEect-orientedM4ro7ra66in7

htt#1//gambas.sourceforge.net

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 4roEects) 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 4roEects) code and 6ore) it is Eust 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 4roEects 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<elopment folde8


!n 6y case in the D&e2elo46entD 0older ! create a D%a6bas;D and a D%a6bas8D 0older. Within the D%a6bas8D 0older ! create 0ollowin7 0olders3 #44lication docu6entation %a6bas8-#rchi2ed#44s %a6bas8-%a6eCone %a6bas8-(roEects %a6bas8-SourceOthers %a6bas8-TestCone %a6bas8-Tools

Illustration 1: Organisation of 'Development' folder

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 Eust 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 4roEects ($ %a6bas8-(roEects The 0older ! use 0or all 6y distributed a44lication 4roEects. Each 4roEect will 7et his own 0older na6ed a0ter the 4roEect. This will be done when creatin7 a new 4roEect in the %a6bas !&E. *$ %a6bas8-SourceOthers !n this 0older ! un4ack 4roEect source code 0ro6 other de2elo4ers they shared. ! can then o4en the6 as nor6al 4roEects 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 4roEect 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 4roEects) but e1actly what the 0older says) test Ione a44lications. They o0ten 7et deleted instead or archi2ed. 1$ %a6bas8-Tools The 0older ! use 0or a44lications (libraries) ! de2elo4 0or use in 6y distributed 4roEects) or 0or tools ! use 6ysel0. $ou can 6ake 6ore or less 0olders to cate7orise and or7anise your 4roEects on your syste6. $ou can add new 0olders later and re4lace 4roEects. +or the 4roEects 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-(roEects Cone-Testin7

1$ #rchi2ed 0older
The D#rchi2edD 0older contains distribution 4acka7es o0 a44lications no lon7er 6aintained. %$ Website 0older

5. Some good practices

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

Illustration 2: Organisation of 'Distributions' folder

1$ Source 4acka7e o0 that 2ersion o0 the a44lication


The source 4acka7es will enable you to re2ert back to 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.

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 Eust 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.

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 4roEects. 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 4roEects where 6ore that one 4ersons works on the sa6e 4roEect. This way all de2elo4ers in2ol2ed will ha2e an idea on what has chan7ed. 'ut e2en in your own 4ersonal 4roEects you 6i7ht be ha44y so6eday when lookin7 back at R 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 4roEects (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.

10

5. Some good practices Static (ublic Sub Read&eskto4!n0o() &i6 sWin"an) sWin"an#id) s&esk#id #s Strin7 &i6 i(osition) i(os') i(osE) iSwitch #s !nte7er =>>>>>>>> &ind ? window manage8 >>>>>>>>>>>>>> Try E1ec SHkwinH) H--2ersionHT To sWin"an ...(rest o0 code) Else *o64Window"ana7er N Tri6(sWin"an) Endi0 = >>> #ete8mine des@top based on window manage8 >>> Select sWin"an#id *ase H6etH =7777 metacit: A Gnome % U (rest o0 code) *ase H6utH =7777 mutte8 A Gnome 3 U (rest o0 code) End

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 trouble 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) s(ost%reSKl #s Strin7 &i6 i(os') i(osE #s !nte7er = table on most 8etu8ned dist8os 7 .0#" a bit t8oublesome =TO#O/ sBlite3 needs testing 8esults 8etu8ned fo8 anal:sing =TO#O/ figu8e out sBlite 8et8ie<al on an .0#" based des@top Try E1ec SH6ysKlH) H-/HT To s"ySKl =>>c9ec@ fo8 +:sBl >> U (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 adEust6ent 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 N Te1t'o1:.Te1t !0 Len(sWin"an) N 9 Then sWin"an N OUnable to traceP Else sWin"an N 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 N Te1t'o1:.Te1t !0 Len(sWin"an) N 9 Then sWin"an N OUnable to traceP Else sWin"an N 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 likewise tasks) try to abstract it and 4lace it in a 6odule. This way it can be used within the entire 4roEect. This also 6akes 0or easier 6aintenance o0 the code as you only need to chan7e it at one 4lace in the 4roEect instead on se2eral 0or6s classes. "odules are actually e14orted classes) that is why they work 0or the entire 4roEect. $ou will 7et to see so6e e1a64les on usin7 6odules in this 7uide. 3$ *lasses *lasses can be considered as the te64lates 0or obEects. When ha2in7 likewise code in 6odules in di00erent a44lications you should consider 6akin7 a new 4roEect 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 4roEects 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 4roEect (the library) instead o0 in each 4roEect 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 4roEect "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 4roEect in %a6bas8 Workin7 with the %a6bas !&E 'utton) Te1t'o1 and Label controls E2ent dri2en 4ro7ra66in7) obEects) 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

"ee 34ow To Gambas % * 0nstalling Gambas3

14

6.

first !"I app#ication$ %e&t'#ay

Now click on )ew ,8oCect$$$ to 6ake a new 4roEect. !n the DNew 4roEectD screen you select the ty4e o0 a44lication you wish to build.

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 *: ''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 4roEectD screen ste4 8 you can na6e both the 0older where the 4roEect will be located and the na6e the actual a44lication will ha2e. They can be di00erent i0 you like.

Illustration 10: ''e" project' screen step project folder and project

% 'ame the

+or both D(roEect na6eDB and D(roEect titleDA ty4e Te?t,la:. Ne1t click the button OD and you 7et a 6essa7e a new 4roEect has been created.

Illustration 11: 1essage "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 4roEect na6ed Te1t(lay.

Illustration 12: 'e" project '2e3t4la)' opened in Gambas

IDE

$ou ha2e Eust created a new 4roEect in %a6bas 0or buildin7 a 0irst %U! a44lication. Each 4roEect o4ened in !&E has < 6aEor Iones3 Cone :3 "enu and toolbar Ione Cone ;3 (roEect 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 this 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 (roEect browser (Ione ;) in the 0older DSourcesD.

Illustration 1 : 51ain 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 the ri7ht o0 it a bo1 with D(ro4erties5 ierarchyD. Underneath the D(ro4erties5 ierarchyD you will 0ind a Tool'o1 to build the controls. The D (ro4erties5 ierarchyD is only 2isible when 0or6s are in the DWorkin7 #reaD. !0 code is 2iewed in the DWorkin7 #reaD there will be no D (ro4erties5 ierarchyD 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 2ool6o3

*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*: 2e3t6o3 icon in 2ool6o3

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+: 2e3t6o3 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 2ool6o3

*lick on the 'utton icon and dra7 two rectan7les on the 0or6.

Illustration 20: 2"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 D(ro4erties5 ierarchyD '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 D(ro4erties5 ierarchyD '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. This will ali7n the te1t o0 the label to the ri7ht.

first !"I app#ication$ %e&t'#ay

Illustration 22: -abel1 aligned right

Ne1t 7o to the DTe1tD 4ro4erty and click on .abel1 and ty4e "nte8 :ou8 name/

Illustration 2 : -abel1 2e3t 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 ObCectsE p8ope8tiesE e<ents and met9ods


$ou will 0irst need to understand so6e basics be0ore continuin7. The 0or6 and all the controls you 6ade are all obCects. There are di00erent kinds o0 obEects. Like the 0or6 is an obEect) each control is an obEect) classes are obEects. So6e obEects are creatable:9) others are not. #s you ha2e seen obEects ha2e 4ro4erties) but obEects can also ha2e e2ents and 6ethods. ,8ope8ties a8e about t9e obCect$ +or e1a64le a 'utton control can ha2e a 4ro4erty Te1t that contains the te1t shown on the 'utton. "<ents can 9appen to an obCect$ +or e1a64le a 'utton control can ha2e the e2ent click. This 6eans that when this e2ent ha44ens to the obEect you can 6ake so6ethin7 ha44en (in other word you need to code what needs to ha44en). +et9ods can be done b: an obCect. +or e1a64le a 'utton control has the 6ethod Show. $ou can use this 6ethod to show the 'utton or hide it. This is a 2ery basic e14lanation) but enou7h to co64rehend what you will be doin7 ne1t.

-$($% 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 obEect). 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 thin7s 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. Ri7ht click on button 9ow) select "<ent ; Clic@.

first !"I app#ication$ %e&t'#ay

Illustration 2$: (elect Event ,lic# for button control

This will o4en the +"ain.*lass in a new tab in the DWorkin7 #reaD.

Illustration 2&: 51ain%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 obEect '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 obEect 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 4roEect browser (or click tab &+ain$fo8m) to 7o back to the 0or6. Ri7ht click button *lear and select "<ent ; Clic@. Write this code 0or (ublic Sub 'utton;M*lick()3 (ublic Sub 'utton;M*lick() Te1t'o1:.Te1t N OP End The N OP (; ti6es double Kuote) sets an e64ty strin7 2alue (or Null 2alue) to the Te1t'o1:.Te1t 4ro4erty. Strin7s are anythin7 in between double Kuotes. Note that co4yin7 the code with Kuotes into !&E will 7enerate errors. $ou need to ty4e the Kuotes in !&E. Ne1t ste4 is to test this little a44lication. +or this you 7o to 6enu #ebug ; !un (or hit &* on your keyboard).

Illustration 2*: 1enu Debug /7 8un to test the application

11

<ou will notice when tying code in Gambas 0=, you will get some hel# along the way. i-e when ty#ing the name of a control followed with a dot you will see all methods& functions& constants and #ro#erties of that control.

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.

Illustration 2+: !pplication running "ith message sho"n

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<ements and test 48ound %5


#s you ha2e seen the 6essa7e bo1 centres on the screen) the a44lication is on the to4-le0t o0 the screen. !t would look nicer i0 the a44lication was centred on the screen as well. #nother thin7 that would be nice is that) in case no te1t is entered and the show button would be clicked the 6essa7e would ask you to enter so6e te1t 0irst. So these added reKuire6ents will 6ean you need to code so6e e2ent belon7in7 to +6ain to 6ake it a44ear centred on the screen. To 0i7ure out what e2ent to code 0or +6ain si64ly select +6ain (in case a control is selected si64ly click on an e64ty s4ace on the 0or6 to select it)) ri7ht click and select 6enu "<ent ; Open. $ou will need the e2ent O4en because you want the 0or6 to centre when it o4ens:;. Write this code 0or (ublic Sub +or6MO4en()3 (ublic Sub +or6MO4en() "e.*enter End "e re0ers to +6ain. *enter is the 6ethod that will center +"ain.
1$

)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(O+irst 0ill in a na6eP) Else "essa7e(OThe na6e entered is O J 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+:e1t7o11, :hen is the sa6e as3 f s9ull+:e1t7o11.:e1t, > :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 obEect re0erence - # Iero len7th strin7 - # null date - #n uninitialiIed 2ariant "ore on !sNull() see htt43557a6basdoc.or75hel45lan75isnull?EaJ28

!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 J. 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 loo-s li-e B B and has a length of 1 instead of loo-ing li-e this BC with a length of '(.

28

6.

first !"I app#ication$ %e&t'#ay

Illustration 2.: 1essage "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: 1essage "hen name entered

$ou see this results in a nicer result than in the 0irst round o0 testin7. *on7ratulations) you ha2e Eust build a 0irst %U! a44lication in %a6bas 8. Now close the 4roEect 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

1 "?tending application Te?t,la:


..1 W#at *ill $e done in t#i" c#apter
"ain 0ocus will be on addin7 so6e 6ore controls and code to 0urther e14lore %a6bas and run the a44lication. This will in2ol2e3 O4en an e1istin7 4roEect 0or chan7es #dd new controls (Radio 'utton) *heck'o1) 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 (pen an exi"ting pro'ect


+irst you start %a6bas and it should o4en on the !ecent ,8oCects tab. !0 not click on it.

Illustration

1: Gambas "elcome screen '8ecent projects'

The 4roEect 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 /odif) main form and add control"


Ne1t ste4 will brin7 us back to +6ain to redesi7n the 0or6 0or so6e additional controls to be added later. To 7et 7et there Eust double click &main in the 4roEect browser. Once you ha2e the +"ain in the DWorkin7 #reaD 6o2e your 6ouse to 6iddle o0 the ri7ht border o0 the 0or6.

Illustration

2: 1ouse 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

: 5orm dragged "ider and -abel and 2e3t!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

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

$: 8esult after adapting the form

$our 0or6 should look like in the illustration abo2e.

32

(. )&tending app#ication %e&t'#ay

1$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 ct8lGc (to co4y the controls). Ne1t 4ress ct8lG< (to 4aste the controls on the 0or6).

Illustration

&: 4asted 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

1$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 D(ro4erties5 ierarchyD '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 *o4y5(aste 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 2e3t 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 2e3t6o3es 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: 1ain 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 4roEect 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 : 1ain 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$($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 "<ent V Clic@ Ty4e 0ollowin7 code 0or (ublic Sub btn#ddM*lick() (ublic Sub bnt#ddM*lick() &i6 sResult #s Strin7 sResult N tb1+irstNa6e.Te1t J O O J tb1Na6e.Te1t lblResult.Te1t N sResult tb1Result.Te1t N sResult t1aResult.Te1t N sResult End &i6 (short 0or &i6ension) is to declare local 2ariables and their ty4es. !n %a6bas you always need to declare your 2ariables be0ore you can use the6 in your code. The J is used to add di00erent strin7s to7ether to one strin7. !n this case the strin7 in tb1+irstNa6e.Te1t 0ollowed by a s4ace (O O) and the strin7 in tb1Na6e.Te1t. The s4ace is to se4arate the 0irst na6e 0ro6 the na6e. Now run the 4roEect ty4e in your 0irst na6e and na6e and click Add to see what ha44ens.

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 Eu64s 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.

1$($% 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 D(ro4erties5 ierarchyD '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 'o2ab5ocus propert) to 2rue

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 N OP tb1+irstNa6e.Te1t N Null tb1Na6e.Te1t N Null lblResult.Te1t N Null tb1Result.Te1t N Null t1aResult.*lear End The D be0ore tb1+irstNa6e.Te1t N OP 6akes the line a co66ent. So it will not be e1ecuted. ere you Eust 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 N 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 0ocus 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(O+ill in 0irst na6eP) tb1+irstNa6e.Set+ocus Else !0 !sNull(tb1Na6e.Te1t) Then "essa7e(O+ill in na6eP) tb1Na6e.Set+ocus Else sResult N tb1+irstNa6e.Te1t J O O J tb1Na6e.Te1t lblResult.Te1t N sResult tb1Result.Te1t N sResult t1aResult.Te1t N 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.

1$($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.: 2hree ,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(O+ill in 0irst na6eP) tb1+irstNa6e.Set+ocus Else !0 !sNull(tb1Na6e.Te1t) Then "essa7e(O+ill in na6eP) tb1Na6e.Set+ocus Else !0 rbtLine:./alue Then sResult N tb1+irstNa6e.Te1t J O O J tb1Na6e.Te1t Else sResult N tb1+irstNa6e.Te1t J OGnO J tb1Na6e.Te1t Endi0 lblResult.Te1t N sResult tb1Result.Te1t N sResult t1aResult.Te1t N sResult Endi0 Endi0 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 Eob. Just see what ha44ens and 4ay s4ecial attention to the di00erent result out4uts when ; Lines is selected.

D: line selected D; lines selected DGn 7enerates a new line

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 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 (OGnP). Ne1t lets add the code 0or the *heck'o1es and re6o2e the 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 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() to3 (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 N 9 D DTest i0 0irst na6e needs to be added be0ore checkin7 i0 0ield is e64ty !0 cb1+irstNa6e./alue N -: Then !0 !sNull(tb1+irstNa6e.Te1t) Then "essa7e(O+ill in 0irst na6eP) tb1+irstNa6e.Set+ocus iSwitch N -: Else iSwitch N : Endi0 Endi0

43

(. )&tending app#ication %e&t'#ay

DTest i0 Na6e needs to be added be0ore checkin7 i0 0ield is e64ty !0 iSwitch WF - : Then !0 cb1Na6e./alue N -: Then !0 !sNull(tb1Na6e.Te1t) Then "essa7e(O+ill in na6eP) tb1Na6e.Set+ocus iSwitch N -: Else !0 iSwitch N : Then iSwitch N :; Else iSwitch N ; Endi0 Endi0 Endi0 Endi0 D D*heck iSwitch what needs to be done !0 iSwitch WF -: Then Select iSwitch *ase : DShow 0irst na6e sResult N tb1+irstNa6e.Te1t *ase ; DShow na6e sResult N tb1Na6e.Te1t *ase :; DShow 0irst na6e and na6e !0 rbtLine:./alue Then D*heck 0or : line sResult N tb1+irstNa6e.Te1t J O O J tb1Na6e.Te1t Else sResult N tb1+irstNa6e.Te1t J OGnO J tb1Na6e.Te1t DGn 7enerates a new line Endi0 End Select lblResult.Te1t N sResult t1aResult.Te1t N 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 Each Case can be tested against t9e e?p8ession be9ind elect 4as long as t:pes matc95 . $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 W b V a s6aller than b a N b V a eKuals b a WN b V a s6aller than or eKual to b a F b V a bi77er than b a FN V a eKual to or bi77er than b a WF b V a di00erent 0ro6 b $ou can co64are both nu6ber 2alues and strin7s. +or strin7s al4habetic order is used to deter6ine outco6e. :9 N :: V returns 0alse :9 W :: V returns true OWordP N ONoneP V returns 0alse OWordP N OWordP V returns true OWordP W ONoneP V returns 0alse OWordP FN ONoneP 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 4roEect. !0 you 7et errors you 4robably 0or7ot to re6o2e tb1Result so6ewhere in the code. +i1 it. 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 na6e. *lick #dd and see what ha44ens. Now select both and click #dd to see what ha44ens.

Illustration $2: ,lic#ed button !dd "ith onl) 'ame selected

!t should all work as e14ected.

45

(. )&tending app#ication %e&t'#ay

1$($( 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 0or6X

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 N 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 N -: Then !0 !sNull(tb1+irstNa6e.Te1t) Then "essa7e(O+ill in 0irst na6eP) tb1+irstNa6e.Set+ocus iSwitch N -: Else iSwitch N : Endi0 Endi0 D DTest i0 Na6e needs to be added be0ore checkin7 i0 0ield is e64ty !0 iSwitch WF - : Then !0 cb1Na6e./alue N -: Then !0 !sNull(tb1Na6e.Te1t) Then "essa7e(O+ill in na6eP) tb1Na6e.Set+ocus iSwitch N -: Else !0 iSwitch N : Then iSwitch N :; Else iSwitch N ; Endi0 Endi0 Endi0 Endi0 D--------- #&&E& *O&E -----DTest i0 *ountry needs to be added be0ore checkin7 i0 0ield is e64ty !0 iSwitch WF - : Then !0 cb1*ountry./alue N -: Then !0 !sNull(tb1*ountry.Te1t) Then "essa7e(O+ill in a countryP) tb1*ountry.Set+ocus iSwitch N -: Else !0 iSwitch N : Then iSwitch N :8 Else !0 iSwitch N ; Then iSwitch N ;8 Else !0 iSwitch N :; Then iSwitch N :;8 Else iSwitch N 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 WF -: Then Select iSwitch *ase : DShow 0irst na6e sResult N tb1+irstNa6e.Te1t *ase ; DShow na6e sResult N tb1Na6e.Te1t *ase :; DShow 0irst na6e and na6e rbtLine:./alue Then D*heck 0or : line sResult N tb1+irstNa6e.Te1t J O O J tb1Na6e.Te1t Else sResult N tb1+irstNa6e.Te1t J OGnO J tb1Na6e.Te1t DGn 7enerates a new line Endi0 D----------- #&&E& *O&E -----------------*ase 8 DShow country sResult N tb1*ountry.Te1t *ase :8 DShow 0irst na6e and country !0 rbtLine:./alue Then D*heck 0or : line sResult N tb1+irstNa6e.Te1t J O O J tb1*ountry.Te1t Else sResult N tb1+irstNa6e.Te1t J OGnO J tb1*ountry.Te1t Endi0 *ase ;8 DShow na6e and country !0 rbtLine:./alue Then D*heck 0or : line sResult N tb1Na6e.Te1t J O O J tb1*ountry.Te1t Else sResult N tb1Na6e.Te1t J OGnO J tb1*ountry.Te1t Endi0 *ase :;8 DShow 0irst na6e) na6e and country !0 rbtLine:./alue Then D*heck 0or : line sResult N tb1+irstNa6e.Te1t J O O J tb1Na6e.Te1t J O O J tb1*ountry.Te1t Else !0 rbtLine;./alue Then D*heck 0or ; lines sResult N tb1+irstNa6e.Te1t J O O J tb1Na6e.Te1t J OGn O J tb1*ountry.Te1t Else sResult N t1b+irstNa6e.Te1t J OGnO J t1bNa6e.Te1t J OGn O J t1b*ountry.Te1t Endi0 Endi0 D----------- EN& #&&E& *O&E -----------------End Select lblResult.Te1t N sResult t1aResult.Te1t N 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 N OP tb1+irstNa6e.Te1t N Null tb1Na6e.Te1t N Null DL #&&E& *O&E -tb1*ountry.Te1t N Null D-- To clear the checkbo1es cb1+irstNa6e./alue N 9 cb1Na6e./alue N 9 cb1*ountry./alue N 9 DL EN& #&&E& *O&E -lblResult.Te1t N Null tb1Result.Te1t N 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: 2esting the ne" code

#lso try usin7 the button *lear to see i0 e2erythin7 clears u4 well. E2erythin7 should work Eust 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 Eob 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.

Illustration $$: (electing 8adio6utton '(ho"' deselects 8adio6utton '1 line'

Re6e6ber 4a7e <:3 O*here all three Chec&7o1es can be selected only one 4adio7utton 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 ct8lG? (or ri7ht click and select cut in 6enu).

Illustration $&: 2ool6o3 tab ,ontainer sho"ing 5rame

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 OShowP.

50

(. )&tending app#ication %e&t'#ay

Illustration $*: 5rame (ho" placed on form

Now with the 0ra6e selected do ct8lG< (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 Eust 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 0ra6e 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(O+irst 0ill in a 0irst na6eP) Else "essa7e(OThe na6e entered is O J tb1+irstNa6e.Te1t) Endi0 Else !0 rbtShow;./alue Then !0 !sNull( tb1Na6e.Te1t) Then "essa7e(O+irst 0ill in a na6eP) Else "essa7e(OThe na6e entered is O J tb1Na6e.Te1t) Endi0 Else !0 !sNull( tb1*ountry.Te1t) Then "essa7e(O+irst 0ill in a countryP) Else "essa7e(OThe na6e entered is O J tb1*ountry.Te1t) Endi0 Endi0 Endi0 End

..5 0 la"t re1ie* 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$*$1 #o :ou need a button 9owH


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 4roEect ! can si64ly co4y54aste? The answer to the 0irst Kuestion will 7i2e you the e2ent to code) the second one the obEect(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.

1$*$% Could i witc9 fo8mat be mo8e clea8H


#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 to be show in the Label and Te1t#rea. These are the 2alues o0 iSwitch and what they re4resent3 Ialue !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 the second. #7ain) think about this as well and sol2e it yoursel0) be0ore 7oin7 to the answer.

1$*$3

9ould t9e button Add be enabled if no Add is c9ec@edH

#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 obEect to code. Take a 6o6ent to think about this and try to sol2e it yoursel0) be0ore continuin7 to the answer.

1$*$(

9ould t9e button Clea8 be enabled if t9e8e is not9ing to clea8H

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 obEect 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 re1ie*

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 Eust as well.

1$-$1 #o :ou need a button 9owH


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 9appenH 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 9appenH V On 8bt 9ow1E 8bt 9ow% and 8bt 9ow3 7 #o I 9a<e usable code in m: p8oCect I can simpl: cop:JpasteH V 4arts o0 the btnShow code are usable *ode e1a64le3 (ublic Sub rbtShow:M*lick() !0 !sNull(tb1+irstNa6e.Te1t) Then "essa7e(O+irst 0ill in a 0irst na6eP) Else "essa7e(OThe 0irst na6e entered is O J tb1+irstNa6e.Te1t) Endi0 End (ublic Sub rbtShow;M*lick() !0 !sNull(tb1Na6e .Te1t) Then "essa7e(O+irst 0ill in a na6eP) Else "essa7e(OThe na6e entered is O J tb1Na6e.Te1t) Endi0 End

55

(. )&tending app#ication %e&t'#ay

(ublic Sub rbtShow8M*lick() !0 !sNull( tb1*ountry .Te1t) Then "essa7e(O+irst 0ill in a countryP) Else "essa7e(OThe country entered is O J tb1*ountry.Te1t) Endi0 End *ode) run and test is all that is le0t.

1$-$% Could i witc9 fo8mat be mo8e clea8H


!t sure can be 6ore clear. LetDs look at the Kuestions and their answers3 7 W9at fo8mat could do bette8H 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 U. 7 Can t9e cu88ent <a8iables t:pe 4Intege85 9old t9e imp8o<ed fo8matH V No it canDt) you would need a Strin7) because in !nte7er 99: would beco6e :. $ou want it to re6ain 99:) hence a Strin7 and in code Kuote O99:PXX +eel 0ree to ada4t your code to 6eet the new reKuire6ents. "ake sure to chan7e iSwitch into Strin7 and ada4t all :) :;) ;) ;8) :8 and :;8 2alues accordin7ly. *han7e the iSwitch -: 2alue to O999P) indicatin7 nothin7 to be shown.

1$-$3

9ould t9e button Add be enabled if no Add is c9ec@edH

No it should not be enabled. #7ain letDs ha2e a look at the Kuestions and answers3 7 W9en do :ou want t9is to 9appenH 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 9appenH 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 N +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 N 9) #nd (cb1Na6e./alue N 9) #nd (cb1*ountry./alue N 9) Then btn#dd.Enabled N +alse Else btn#dd.Enabled N True Endi0 End DL Since code 0or other checkbo1es is sa6e use cb1+irstNa6eM*lick routine to do the Eob -(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 Eust knowin7 that it is 4ossible will do.

1$-$(

9ould t9e button Clea8 be enabled if t9e8e is not9ing to clea8H

No it should not be enabled. #nd a7ain letDs ha2e a look at the Kuestions and answers3 7 W9en do :ou want t9is to 9appenH 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 9appenH V On &main and btnAdd and btnClea8 *ode to disable the button *lear on o4enin7 the 0or63 (ublic Sub +or6MO4en() "e.*enter btn#dd.Enabled N +alse btn*lear.Enabled N +alse End #dd this code to the btn#ddM*lick e2ent3

57

(. )&tending app#ication %e&t'#ay (ublic Sub btn#ddM*lick() U. U.. Endi0 DL #dd this line at the end o0 the routine btn*lear.Enabled N True End #dd this code to the btn*learM*lick e2ent3 (ublic Sub btn*learM*lick() U. U.. tarResult.*lear DL #dd this at the end o0 the routine DL $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 N +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 Eust 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 4roEect 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

3 'ou8 second application/ Te?t,la:T4w5o


2.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 4roEect "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.2 +tart a ne* pro'ect and de"ign t#e fir"t form


O4en %a6bas8 !&E and start a new 7ra4hical a44lication 4roEect na6ed Te1t(layT(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?tA8ea3 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 IalueBo?3 Na6e V 2b1S4lit 1 Button3 Na6e V btnS4litY Te1t V S4lit ( Te?tBo?es3 Na6e V tb1Line:...tb1Line<Y Te1t V e64tyY ReadOnly V True 1 &8ame3 Na6e V 0raSearchY Te1t V Search or Search5Re4laceY Enabled V +alse 1 !adioButton (in 0raSearch)3 Na6e V rbtSearchY Te1t V Search 1 !adioButton (in 0raSearch)3 Na6e V rbtRe4laceY Te1t V Search5Re4lace 1 Button3 Na6e V btnSearchY Te1t V SearchY Enabled V +alse When 0inished with 4lacin7 all controls and settin7 all 4ro4erties accordin7ly you should ha2e so6ethin7 like in the illustrations on the ne1t 4a7e. +irst illustration (>;) 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: 1ain form in IDE design mode

Illustration & : 1ain form "hen run from IDE

2.3 Pla)ing *it# text t#e ot#er *a) around


!n Te1t(lay(w)o you are 7oin7 to start with doin7 the o44osite o0 Te1t(lay. !n Te1t(lay 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$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 Os4lit this te1t on s4aceP tb1S4lit.Te1t V O O (a s4ace) (ressin7 S4lit should result in3 tb1Line:.Te1t V Os4litP tb1Line;.Te1t V OthisP tb1Line8.Te1t V Ote1tP tb1Line<.Te1t V OonP 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: N S4lit( t8ing #s Strin7 S) epa8ato8s #s Strin7) "scape #s Strin7) ...T) 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$o1e. 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 Strin7ST DStrin7 0ollowed by S and T declares a strin7 array &i6 sTe1t #s Strin7 &i6 i. #s !nte7er i. N : D Used to deter6ine what t1bLine to out4ut to sS4lit N S4lit(t1a*ontent.Te1t) tb1S4lit.Te1t) +or Each sTe1t !n sS4lit Select i. *ase : tb1Line:.Te1t N sTe1t *ase ; tb1Line;.te1t N sTe1t

61

*. +our second app#ication$ %e&t'#ay%,w-o *ase 8 tb1Line8.Te1t N sTe1t *ase < tb1Line<.Te1t N sTe1t End Select i. N i. Z : Ne1t End

+or Each Ia8iable !n "?p8ession Ne1t Re4eats a loo4 while enu6eratin7 the obEect in e14ression "?p8ession 6ust be a re0erence to a enu6erable obEect like a collection or an array Ia8iable is the re0erence to the ite6 in the enu6erable obEect 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: 2e3t splitted on the dot <%=

!t runs as e14ected) but try a7ain with so6e other te1t in t1a*ontent en S4lit on OGnP (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 O +eparator" are the characters used to split the te1tP So S4lit will check 0or both OGP and OnP 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 (OGnP) instead. So you need to code the btnS4lit e2ent to reco7niIe the OGnP and act accordin7ly. +or this you need to add an e1tra OGP:=. So back to !&E 0or so6e recodin73 (ublic Sub btnS4litM*lick() &i6 sS4lit #s Strin7ST DStrin7 0ollowed by S and T declares a strin7 array &i6 sTe1t #s Strin7 &i6 i. #s !nte7er i. N : D Used to deter6ine what t1bLine to out4ut to Select tb1S4lit.Te1t *ase OGGnP D To check 0or a line 0eed tb1S4lit.Te1t N *hr(:9) D To s4lit on a line 0eed (N*hr(:9)) End Select sS4lit N S4lit(t1a*ontent.Te1t) tb1S4lit.Te1t) +or Each sTe1t !n sS4lit Select i. *ase : tb1Line:.Te1t N sTe1t *ase ; tb1Line;.te1t N sTe1t *ase 8 tb1Line8.Te1t N sTe1t
12

The extra BEC ma-es sure the following BEC is #reserved.

63

*. +our second app#ication$ %e&t'#ay%,w-o

*ase < tb1Line<.Te1t N sTe1t End Select i. N 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 OGnP 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$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 Strin7ST DStrin7 0ollowed by S and T declares a strin7 array &i6 sTe1t #s Strin7 &i6 i.) iNu6S4lits #s !nte7er i. N : D Used to deter6ine what t1bLine to out4ut to iNu6S4lits N 2b1S4lit./alue D Used to deter6ine nu6ber o0 s4lits Select tb1S4lit.Te1t *ase OGGnP D To check 0or a line 0eed tb1S4lit.Te1t N *hr(:9) D To s4lit on a line 0eed (N*hr(:9)) End Select sS4lit N S4lit(t1a*ontent.Te1t) tb1S4lit.Te1t) +or Each sTe1t !n sS4lit !0 i. WN iNu6S4lits Then Select i. *ase : tb1Line:.Te1t N sTe1t *ase ; tb1Line;.te1t N sTe1t *ase 8 tb1Line8.Te1t N sTe1t *ase < tb1Line<.Te1t N sTe1t End Select i. N 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 Strin7ST DStrin7 0ollowed by S and T declares a strin7 array &i6 sTe1t #s Strin7 &i6 i.) iNu6S4lits #s !nte7er i. N : D Used to deter6ine what t1bLine to out4ut to iNu6S4lits N 2b1S4lit./alue D Used to deter6ine nu6ber o0 s4lits *learLines DThis starts a subroutine Select tb1S4lit.Te1t *ase OGGnP D To check 0or a line 0eed tb1S4lit.Te1t N *hr(:9) D To s4lit on a line 0eed (N*hr(:9)) End Select sS4lit N S4lit(t1a*ontent.Te1t) tb1S4lit.Te1t) +or Each sTe1t !n sS4lit !0 i. WN iNu6S4lits Then Select i. *ase : tb1Line:.Te1t N sTe1t *ase ; tb1Line;.te1t N sTe1t *ase 8 tb1Line8.Te1t N sTe1t *ase < tb1Line<.Te1t N sTe1t End Select i. N i. Z : Endi0 Ne1t End (ri2ate Sub *learLines() tb1Line:.*lear tb1Line;.*lear tb1Line8.*lear tb1Line<.*ear 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 N 2b1S4lit./alueD will run the subroutine *learLines and e1ecute it line by line be0ore continuin7 with the e1ecution o0 DSelect tb1S4lit.Te1tD SStaticT [(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<ate ,eyword that declares accessibility o0 a 6ethod (sub54rocedure or 0unction)) 2ariable or 4ro4erty # (ri2ate declared 6ethod) 2ariable or 4ro4erty can NOT be used outside its own class. "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 <other lines are no" cleared=

See6s that all is workin7 as e14ected.

2.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.

3$($1 +a@e a new fo8m


To 6ake a new 0or6 in the ,8oCect 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

Illustration *2: 5rm(earch8eplace "ith controls placed

69

*. +our second app#ication$ %e&t'#ay%,w-o Now add so6e initial code to the 0or6 o4en e2ent (ri7ht click the 0or6 and select 6enu "<ent ; Open) (ublic Sub +or6MO4en() "e.*a4tion N OSearchP End This will 7i2e the +r6SearchRe4lace the ca4tion OSearchP. #s you notice +r6SearchRe4lace is not centered) +r6"ain is. !dea is that +r6SearchRe4lace 7ets o4ened when clickin7 the btnSearch in +r6"ain. To 6ake sure that +r6SearchRe4lace is not in 0ront o0 t1a*ontent) you lea2e all de0ault) 6eanin7 in to4 le0t o0 your screen

3$($% +a@e &8m ea8c9!eplace open f8om btn ea8c9 on &8m+ain


&ouble click &8m+ain in the ,8oCect B8owse8 and ne1t select btn ea8c9$ #s you 6i7ht re6e6ber btnSearch was disabled. So in btn ea8c9 4ro4erties set enabled to True. Ne1t ri7ht click the selected btn ea8c9 and in the 6enu select "<ent ; Clic@$ This will o4en the Tab +r6"ain.class ready to code the click e2ent o0 btnSearch. (ublic Sub btnSearchM*lick() +r6SearchRe4lace.Show End What you do here is call the 6ethod Show o0 +r6SearchRe4lace. So you tell the +r6SearchRe4lace to show itsel0. +or6s inherit the Window class which 6eans 4ro4erties) 6ethods and e2ents are inherited 0ro6 the window class e2ery ti6e a 0or6 is 6ade. &o8ms 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$($3 ,8oCect 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 4roEect 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<ate in +r6SearchRe4lace are3 +et9ods ,ublic in +r6"ain are3 +et9ods +or6MO4en

71

*. +our second app#ication$ %e&t'#ay%,w-o btnS4litM*lick() btnSearchM*lick ,8i<ate in +r6"ain are3 +et9ods *learLines() So (ublic 6eans a2ailable to other 0or6s) (ri2ate 6eans NOT a2ailable to other 0or6s. LetDs 7i2e this a 7o in the !&E. &ouble click &8m ea8c9!eplace in the ,8oCect B8owse8. Now double click btnSearch. This will o4en a btnSearchM*lick() 6ethod in the +r6SeachRe4lace.class (N all the code 0or the 0or6). Write the 0ollowin7 code to the btnSeach click e2ent3 (ublic Sub btnSearchM*lick() +r6"ain.btnS4litM*lick End Ne1t run the a44lication. !n t1a*ontent ty4e3 To be or not !n tb1S4lit ty4e3 O O (a s4ace) !n 2b1S4lit ty4e3 < Ne1t click btnSearch to o4en +r6SearchRe4lace. !n +r6SearchRe4lace click btnSearch and see what ha44ens in +r6"ain.

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 Eust an e1a64le. 'ack to our 4roble6 to sol2e in this 4roEect. There are two 4ossible a44roaches3 1$ Code t9e sea8c9ing on fo8m &8m ea8c9!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 &8m ea8c9!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.

3$($( 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 4ublic 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.

3$($* .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' N : i' N !nStr(tar*ontent.Te1t) Strin7ToSearch) i') D&eter6ines the start 4osition o0 strin7 0ound iL N Len(Strin7ToSearch) D&eter6ines 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 N !nstr( t8ing #s Strin7 ) ubst8ing #s Strin7 S) ta8t #s !nte7er) ...T) !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 ub t8ing is the strin7 2alue that you are searchin7 0or ta8t is the 4osition you want the search to be7in 3n"tr #a" more option" t#an mentioned a$o1e. !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 N Len(A8gs #s Strin7) 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 4eng#t i" determined $) $)te count of t#e "tring. 0+!33 c#aracter" take up one $)te. !#aracter" like 5 or 6 78T&92- 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 OWillyP (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

#ll should work as e14ected and select the searched strin7 when 0ound) select nothin7 when nothin7 is 0ound.

Illustration **: (eems li#e search performs as e3pected

2.5 +earc# and replace text


'e0ore you can start searchin7 and re4lacin7 strin7 you will need to add so6e controls to you current +r6SearchRe4lace.

3$*$1 ,8epa8e t9e fo8ms fo8 ea8c9 and !eplace


!n the !&E click +r6SearchRe4lace in the ,8oCect B8owse8. (lace 0ollowin7 cont8ols on +r6SearchRe4lace with so6e 4ro4erties 4re-set3 1 Te?tBo?3 Na6e V tb1Re4lace Y Te1t V Enter the re4lace strin7... 1 Button3 Na6e V btnRe4laceY Te1t V Re4lace +astest 6ethod is to select tb1Search and btnSeach) co4y and 4aste the6 onto the 0or6) dra7 the6 to the 4ro4er location and ada4t the 4ro4erties Na6e and Te1t.

Illustration *+: 8eplace 2e3t6o3 and 6utton added to 5rm(earch8eplace

Ne1t click +r6"ain in the ,8oCect 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 5rm1ain

With the 0ra6e 0or choosin7 between Search and Search5Re4lace enabled and +r6SearchRe4lace 4re4ared 0or Re4lacin7 you are set 0or the ne1t ste4.

3$*$% +a@ing &8m ea8c9!eplace appea8 depending on selection in &8m+ain


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 has to3 - 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 :Nsearch) ;Nre4lace -(ublic SearchRe4lace #s 'yte (ublic Sub +or6MO4en() "e.*enter rbtSearch./alue N 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 e1ecute 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. %a6bas nati2e dataty4es are3 T:pe Boolean B:te 9o8t Intege8 .ong ingle &loat #ate t8ing Ia8iant ObCect ,ointe8 #esc8iption True or +alse 9 to ;== -8;.B>A to 8;.B>B -;.:<B.<A8.><A to ;.:<B.<8A.><B -@.;;8.8B;.98>.A=<.BB=.A9A to @.;;8.8B;.98>.A=<.BB=.A9B sin7le 4recision 0loatin7-4oint ty4e double 4recision 0loatin7-4oint ty4e &ate and ti6e each stored in an !nte7er # 2ariable len7th strin7 o0 characters #ny dataty4e #nony6ous re0erence to any obEect # 6e6ory address #efault <alue +alse 9 9 9 9 9.9 9.9 Null Null Null Null 9

"ore on dataty4es see htt43557a6basdoc.or75hel45cat5dataty4es?28 "ore on +loatin7 (oint Nu6bers3 htt43557a6basdoc.or75hel45cat50loat?28 "ore on sin7le 4recision 0loatin7-4oint 0or6at3 htt4s355en.wiki4edia.or75wiki5Sin7leM4recisionM0loatin7-4ointM0or6at "ore on double 4recision 0loatin7 4oint 0or6at3 htt4s355en.wiki4edia.or75wiki5&oubleM4recisionM0loatin7-4ointM0or6at *ode 0or e2ent click o0 rbtSearch3 (ublic Sub rbtSearchM*lick() SearchRe4lace N : Dsearch End *ode 0or e2ent click o0 rbtRe4lace3 (ublic Sub rbtRe4laceM*lick() SearchRe4lace N ; Dre4lace End With this code in 4lace you ha2e your 6eans to know what +r6SearchRe4lace has to show u4on o4enin7. !0 +r6"ain (ublic 2ariable SearchRe4lace is : itDs a search) when ; itDs a re4lace. Now letDs 6ake that ha44en. +or this you need to code the O4en e2ent o0 +r6SearchRe4lace3 (ublic Sub +or6MO4en() Select +r6"ain.SearchRe4lace *ase :

78

*. +our second app#ication$ %e&t'#ay%,w-o "e.*a4tion N HSearchH btnRe4lace./isible N +alse tb1Re4lace./isible N +alse *ase ; "e.*a4tion N HSearch and Re4laceH btnRe4lace./isible N True tb1Re4lace./isible N True End Select End Now run the a44lication and see i0 all reacts as wanted

Illustration +0: (earch sho"n "hen search selected

Illustration +1: (earch and 8eplace sho"n "hen search9replace selected

79

*. +our second app#ication$ %e&t'#ay%,w-o

3$*$3 !eplace sea8c9ed st8ings


To re4lace a strin7 you a7ain will write a (ublic 6ethod 0or +r6"ain that does all the re4lacin7. !n +r6"ain write 0ollowin7 code3 (ublic Sub Re4laceStrin7(Strin7ToRe4lace #s Strin7) Strin7ToRe4laceWith #s Strin7) t1a*ontent.Te1t N Re4lace(t1a*ontent.Te1t) Strin7ToRe4lace) Strin7ToRe4laceWith) End Now all that you need to do is run abo2e code when button Re4lace is clicked. !n +r6SearchRe4lace write 0ollowin7 code3 (ublic Sub btnRe4laceM*lick +r6"ain.Re4laceStrin7(tb1Search.Te1t) tb1Re4lace.Te1t) End #7ain all you do is 4ass so6e ar7u6ents (bein7 the search and re4lace strin7s) to the 6ethod Re4laceStrin7 to be dealt with accordin7ly. !esult N Re4lace( t8ing #s Strin7) ,atte8n #s Strin7) !eplace t8ing #s Strin7S) ...T) Re4lace enables you to search a strin7 and re4lace it with another strin7. !esult is the outco6e o0 t8ing with ,atte8n re4laced by !eplace t8ing t8ing is the strin7 2alue that will be searched ,atte8n is the strin7 2alue that you are searchin7 0or !eplace t8ing is the strin7 2alue that ,atte8n will be re4laced by ;eplace #a" more option" t#an mentioned a$o1e. Re4lace is a Strin7 +unction. Strin7 0unctions are 0unctions that can be used to do work with Strin7s. "ore on Re4lace see htt43557a6basdoc.or75hel45lan75re4lace?28 O2er2iew o0 Strin7 +unctions see3 htt43557a6basdoc.or75hel45cat5strin7?28 Now run the a44lication 0or so6e testin7 !n t1a*ontent write3 Try to re4lace Willy with silly i0 you can +irst search 0or it) ne1t click Re4lace So 7o ahead ty4e abo2e lines) ne1t ty4e Willy in Search 0ield and click button Search. !0 that works ty4e silly in the Re4lace 0ield and click button Re4lace. !0 all is well Willy should now be silly (see illustrations on the ne1t 4a7e). (lay with it 0or a while and see what can and canDt be done. The Radio'uttons in this e1a64le are 4laced on +r6"ain to e14lain interaction between 0or6s and (ublic 2ariables and how to deal with these situations. !t would be better 4ractice to 4lace the6 on +r6SearchRe4lace.

80

*. +our second app#ication$ %e&t'#ay%,w-o

Illustration +2: (earch still "or#s "ell

Illustration + : 8eplace does a good job as "ell

!n this cha4ter you ha2e worked with se2eral Strin7 0unctions. $ou ha2e noticed by now that a lot is 4ossible usin7 Strin7 0unctions. $ou will co6e to learn that they can be use0ul in a lot o0 situations) like i64ortin7 a co66a deli6ited 0ile) e1tractin7 returned in0or6ation 0ro6 a linu1 co66and run or buildin7 a te1t editor Eust to 6ention a 0ew e1a64les. They are 2ery 4ower0ul tools to use and so6e o0 the6 you will be usin7 a lot when workin7 with te1t based in0or6ation) so 7et 0a6iliar with the6. Now in %a6bas !&E click 6enu &ile ; Fuit. This 7ets you ready 0or the ne1t cha4ter.

81

.. +our third app#ication$ /um'#ay

6 'ou8 t9i8d application/ )um,la:


<.1 W#at *ill $e done in t#i" c#apter
"ain 0ocus will be nu6erical data) 0or that you will built a new a44lication. This will in2ol2e O4enin7 %a6bas !&E and 6ake a new 4roEect "ake a 6ain 0or6 (lace a nu6ber o0 controls on the 0or6 Write so6e code Run the a44lication 0ro6 %a6bas !&E $ou can learn about3 Workin7 with the %a6bas !&E Workin7 with nu6eric data) date and ti6e Error handlin7 Workin7 with #rith6etical 0unctions and &ate J Ti6e 0unctions Workin7 with LocaliIation and Translation 0unctions Trans0er nu6eric data in strin7s to nu6erical data ty4es User interaction (in4ut 0ro6 user) out4ut 0ro6 a44lication) Enu6eration

<.2 +tart a ne* pro'ect and de"ign a form


O4en %a6bas8 !&E and start a new 7ra4hical a44lication 4roEect na6ed Nu6(lay. Rena6e 0or6 +"ain to +r6"ain. &o this by ri7ht clickin7 &+ain in the (roEect 'rowser and ne1t click !ename. (lace 0ollowin7 cont8ols on +r6"ain with so6e 4ro4erties 4re-set3 1 IalueBo?3 Na6e V 2b1Nu6: 1 IalueBo?3 Na6e V 2b1Nu6; 1 .abel3 Na6e V lblO4erator Y Te1t V e64tyY #li7n6ent V Ri7ht 1 epa8ato8 1 IalueBo?3 Na6e V 2b1Result 1 .abel3 Na6e V lblResult Y Te1t V e64tyY #li7n6ent V Ri7ht 1 Button3 Na6e V btn*alcY Te1t V *alculate 1 &8ame3 Na6e V 0raO4eratorY Te1t V O4erator 1 !adioButton (in 0raO4erator)3 Na6e V rbt#ddY Te1t V Z 1 !adioButton (in 0raO4erator)3 Na6e V rbtSubtractY Te1t V 1 !adioButton (in 0raO4erator)3 Na6e V rbt"ulti4lyY Te1t V ^ 1 !adioButton (in 0raO4erator)3 Na6e V rbt&i2ide Te1t V 5 1 !adioButton (in 0raO4erator)3 Na6e V rbt(ower Te1t V _ When 0inished with 4lacin7 all controls and settin7 all 4ro4erties accordin7ly you should ha2e so6ethin7 like in the illustrations on the ne1t 4a7e. +irst illustration (A<) shows the 0or6 in the !&E desi7ner. Second illustration (A=) shows the 0or6 when run.

82

.. +our third app#ication$ /um'#ay

Illustration +4: 5rm1ain after placing all controls

Illustration +$: 5rm1ain "hen run

<.3 =oing t#e mat#"


!n Nu6(lay you are 7oin7 to work with nu6bers and calculations. #lon7 the road youDll also ha2e a look at nu6erical data shown in te1t based controls and in usin7 in4ut 0ro6 te1t based controls 0or calculations.

83

.. +our third app#ication$ /um'#ay

6$3$1 Anal:se t9e needs and dete8mine a st8ateg:


'ased on the o4erator clicked you need to do so6e thin7s. To be7in with lblO4erator needs to show the calculation o4eration to be done when clickin7 btn*alc. Lets start by doin7 this 0irst. $ou know that each Radio'uttons te1t 4ro4erty holds the o4eration. This only needs to 4ut 4laced in the lblO4erator te1t 4ro4erty. +or this you need to code the *lick e2ent o0 each Radio'utton3 (ublic Sub +or6MO4en() "e.*enter rbt#ddM*lick End (ublic Sub rbt#ddM*lick() lblO4erator.Te1t N rbt#dd.Te1t End (ublic Sub rbtSubtractM*lick() lblO4erator.Te1t N rbtSubtract.Te1t End (ublic Sub rbt"ulti4lyM*lick() lblO4erator.Te1t N rbt"ulti4ly.Te1t End (ublic Sub rbt&i2ideM*lick() lblO4erator.Te1t N rbt&i2ide.Te1t End (ublic Sub rbt(owerM*lick() lblO4erator.Te1t N rbt(ower.Te1t End Now run the a44lication and see what ha44ens when clickin7 the Radio'uttons. DTo ensure lblO4erator shows Z

Illustration +&: lblOperator sho"s selected Operator

84

.. +our third app#ication$ /um'#ay Ne1t challen7e is to 6ake the calculations based on the selected o4erator. So when clickin7 btn*alc you need to 4er0or6 the selected o4eration on the 2alues entered in 2b1Nu6: and 2b1Nu6; and return the result in 2b1Result. *learly the O4eration selection is centrally crucial in all that needs to ha44en. So6ehow when clickin7 btn*alc you need to deter6ine what Radio'utton is selected and act accordin7ly. There are se2eral 4ossible a44roaches) one o0 the6 is usin7 a switch that is set when a Radio'utton is clicked. This is how the switch 2alue could re4resent the O4erator O4erator3 Z ^ 5 _ Switch 2alue3 9 : ; 8 < To i64le6ent this so6e code needs to be added3 (ri2ate iSwitch #s !nte7er (ublic Sub +or6MO4en() "e.*enter rbt#ddM*lick End (ublic Sub rbt#ddM*lick() lblO4erator.Te1t N rbt#dd.Te1t iSwitch N 9 End (ublic Sub rbtSubtractM*lick() lblO4erator.Te1t N rbtSubtract.Te1t iSwitch N : End (ublic Sub rbt"ulti4lyM*lick() lblO4erator.Te1t N rbt"ulti4ly.Te1t iSwitch N ; End (ublic Sub rbt&i2ideM*lick() lblO4erator.Te1t N rbt&i2ide.Te1t iSwitch N 8 End (ublic Sub rbt(owerM*lick() lblO4erator.Te1t N rbt(ower.Te1t iSwitch N < End (ublic Sub btn*alcM*lick() DTo ensure lblO4erator shows Z

85

.. +our third app#ication$ /um'#ay

(rint iSwitch End (rint iSwitch will be e1ecuted when clickin7 on btn*alc 7i2in7 us the current 2alue o0 iSwitch durin7 run ti6e. This will hel4 you check i0 all works as e14ected. Run the a44lication) select subtract and ne1t click btn*alc. !0 you 4ay attention in the console out4ut o0 the !&E you will see : a44ear when clickin7 btn*alc

Illustration +*: ,onsole output in IDE

Usin7 the (rint instruction will out4ut to the !&E console durin7 runti6e. !t is a way o0 checkin7 what is ha44enin7 durin7 runti6e and co6es in handy when so6ethin7 is not workin7 as e14ected to hel4 you in2esti7ate what is actually ha44enin7. ,8int instruction is ty4ically used to out4ut to the user in a co66and line %a6bas a44lication. # si64le (rint 0ollowed by a strin7 will out4ut to the standard out4ut (in !&E bein7 the console) in a co66and line a44lication bein7 the ter6inal it runs in). ,8int can also used to out4ut to t8eams. t8eams are internal obEects that %a6bas creates 0or 6ani4ulatin7 0iles) 4rocesses) sockets) and 6any other obEects that can be used as 0iles. "ore on (rint see htt43557a6basdoc.or75hel45lan754rint?28 *lick another o4erator and ne1t on btn*alc to see i0 all the Switch 2alues 6eet your reKuire6ents. Now that you ha2e ensured yoursel0 that iSwitch holds the 4ro4er 2alue ne1t ste4 is to code 0or the calculations based on iSwitch. +or this you need to code the *lick e2ent o0 btn*alc3

86

.. +our third app#ication$ /um'#ay (ublic Sub btn*alcM*lick() Select iSwitch *ase 9 2b1Result./alue N 2b1Nu6:./alue Z 2b1Nu6;./alue *ase : 2b1Result./alue N 2b1Nu6:./alue L 2b1Nu6;./alue *ase ; 2b1Result./alue N 2b1Nu6:./alue ^ 2b1Nu6;./alue *ase 8 2b1Result./alue N 2b1Nu6:./alue 5 2b1Nu6;./alue *ase < 2b1Result./alue N 2b1Nu6:./alue _ 2b1Nu6;./alue End Select End Now run the a44lication) enter :9 and ; 0or 2alues and try all calculations.

Illustration ++: ,alculating values 10 and 2

#s you will notice the calculations run well) but now try with 2alues :9 and 9. #s soon as you try di2idin7 you will 7et an error as you are not allowed to di2ide by Iero.

Illustration +.: Division b) @ero error

87

.. +our third app#ication$ /um'#ay #s you will also notice you a44lication is not reactin7. To sto4 it click the Sto4 button in the !&E toolbar (located below the 6enu bar).

6$3$% Implement e88o8 9andling


This is a 7ood ti6e to introduce so6e error handlin7. What you would like to know is the error code. This is a nu6ber that can be used to indicate what went wron7. #dd so6e code to the btn*alc click e2ent3 (ublic Sub btn*alcM*lick() Select iSwitch *ase 9 2b1Result./alue N 2b1Nu6:./alue Z 2b1Nu6;./alue *ase : 2b1Result./alue N 2b1Nu6:./alue L 2b1Nu6;./alue *ase ; 2b1Result./alue N 2b1Nu6:./alue ^ 2b1Nu6;./alue *ase 8 2b1Result./alue N 2b1Nu6:./alue 5 2b1Nu6;./alue *ase < 2b1Result./alue N 2b1Nu6:./alue _ 2b1Nu6;./alue End Select *atch (rint Error.*ode Error.*lear End The keyword *atch will e1ecute whene2er an error in btn*alcM*lick occurs. Error.*ode will return the error nu6ber. Error.*lear will clear the error so the a44lication doesnDt han7. Catc9 This instruction indicates the be7innin7 o0 the error 6ana7e6ent 4art o0 a 0unction or a 4rocedure. The catch 4art is e1ecuted when an error is raised between the be7innin7 o0 the 0unction e1ecution and its end. :his error can be raised by the function itself) or by any other function called during its e1ecution) provided that this deeper function has no catch part itself6 the deeper the catch part is) the more priority it has. "ore on *atch see htt43557a6basdoc.or75hel45lan75catch?28 "ore on Error 6ana7e6ent see htt43557a6basdoc.or75hel45cat5error?28 "88o8 Use this class 0or 6ana7in7 errors raised while the inter4reter is runnin7. &o not 6i1 u4 with ERROR (with *a4ital letters)XX "ore on Error see htt43557a6basdoc.or75hel45co6457b5error?28 "ore on Error 6ana7e6ent see htt43557a6basdoc.or75hel45cat5error?28 Run the a44lication and enter :9 and 9 and do a di2ision. The console will out4ut ;>

88

.. +our third app#ication$ /um'#ay

Illustration .0: Error 2& returned

Now that you know that error ;> occurs when tryin7 to di2ide by Iero you can *atch the error and code it to do what you would like to ha44en. !n this case you would like to noti0y the user that it is not allowed to di2ide by Iero and 6aybe e2en set the 0ocus to the second 2alue. #da4t the code 0or *atch accordin7ly (ublic Sub btn*alcM*lick() Select iSwitch *ase 9 2b1Result./alue N 2b1Nu6:./alue Z 2b1Nu6;./alue *ase : 2b1Result./alue N 2b1Nu6:./alue L 2b1Nu6;./alue *ase ; U. End Select *atch Select Error.*ode *ase ;> "essa7e.Warnin7(O$ou are not allowed to di2ide by IeroXXP) 2b1Nu6;.Set+ocus *ase Else (rint Error.*ode End Select Error.*lear End Error.*ode holds !nte7er 2alues. #nd since this 6i7ht be the 0irst in 6ore 4ossible errors a Select *ase is used instead o0 an !0 Then 0or 4ossible 0urther errors to occur. +or now i0 an other error than ;> occurs you will see its nu6ber in the *onsole o0 the !&E. Now run the a44lication a7ain and see what ha44ens when you try to di2ide by Iero.

89

.. +our third app#ication$ /um'#ay

Illustration .1: Division b) @ero no" handled properl)

#s you can see the a44lication no lon7er allows di2ision by Iero and in0or6s the user. #ll ha44ens without han7in7 the a44lication.

<.4 Working *it# num$er" in text 1aria$le" and text control"


Ne1t you will ha2e a look i0 nu6bers entered in a te1t control or held by a te1t 2ariable (Strin7) can be used 0or calculations. (lace 0ollowin7 cont8ols on +r6"ain with so6e 4ro4erties 4re-set3 1 Te?tBo? Na6e V tb1Nu6: 1 Te?tBo?3 Na6e V tb1Nu6; 1 Te?tBo?3 Na6e V tb1Result 1 Button3 Na6e V btn*alcTe1tY Te1t V *alc te1t 1 Te?tBo?3 Na6e V tb1OneLine When 0inished with 4lacin7 all controls and settin7 all 4ro4erties accordin7ly you should ha2e so6ethin7 like in the 0ollowin7 illustrations.

Illustration .2: 5rm1ain after adding some controls

90

.. +our third app#ication$ /um'#ay

6$($1

imple beginnings

#s the title says) you are 7oin7 to be7in si64le. Si64le in this case 6eans you ri7ht clicl on button DCall Te?tD and in 4o4 4 6enu select "<ent ; Clic@. This will brin7 you to the code editor ready to code the click e2ent o0 btn*alcTe1t. Now co4y all the code in between ,ublic ub btnCalcLClic@45 and "nd 45 4so the code o0 the entire routine e1ce4t (ublic Sub U and End()) and 4aste it into the ,ublic ub btnCalc7 Te?tLClic@45 e2ent. Ne1t chan7e all <b1... control na6es to tb1.. control na6es and the ./alue to .Te1t. The result should look like below (ublic Sub btn*alcTe1tM*lick() Select iSwitch *ase 9 tb1Result.Te1t N tb1Nu6:.Te1t Z tb1Nu6;.Te1t *ase : tb1Result.Te1t N tb1Nu6:.Te1t L tb1Nu6;.Te1t *ase ; tb1Result.Te1t N tb1Nu6:.Te1t ^ tb1Nu6;.Te1t *ase 8 tb1Result.Te1t N tb1Nu6:.Te1t 5 tb1Nu6;.Te1t *ase < tb1Result.Te1t N tb1Nu6:.Te1t _ tb1Nu6;.Te1t End Select *atch Select Error.*ode *ase ;> "essa7e.Warnin7(O$ou are not allowed to di2ide by IeroXXP) tb1Nu6;.Set+ocus *ase Else (rint Error.*ode End Select Error.*lear End Now hit W+=F to run the a44lication and see what will ha44en i0 you si64ly start calculatin7 with te1t. When started enter : in the two /alue'o1es and : in the two Te1t'o1es. Ne1t click both buttons to see what ha44ens.

Illustration . : ,alculating the te3t seems to "or#

91

.. +our third app#ication$ /um'#ay $ou will notice that calculatin7 the te1t 2alues see6 to work out o0 the bo1.

6$($% )e?t c9allenge


LetDs try so6ethin7 6ore challen7in7. LetDs say you want to ty4e D; Z 8D into t1bOneLine) ne1t click btn*alcTe1t and see the result in tb1Result. So6ethin7 Kuite di00erent. +or this you will need to 7et the two nu6bers and the o4erator out o0 a strin7. $ou can use strin7 0unctions to 7et this done. Ne1t you will need to trans0or6 the strin7 nu6bers to nu6bers and deter6ine what o4erator to use on the two nu6bers. Last ste4 is to return the result to tb1Result. So back to the code editor in !&E to chan7e the code to3 (ublic Sub btn*alcTe1tM*lick() &i6 sO4erator) s el4 #s Strin7 &i6 i' #s !nte7er &i6 iNu6:) iNu6; #s +loat D-- 0ind the 0irst nu6ber L i' N : i' N !nStr(tb1OneLine.Te1t) H H) i') s el4 N "id(tb1OneLine.Te1t) :) i' L :) iNu6: N /al(s el4) D-- 0ind the o4erator L !N* i' sO4erator N "id(tb1OneLine.Te1t) i') :) D-- 0ind the ne1t nu6ber L i' N !nStr(tb1OneLine.Te1t) H H) i') s el4 N "id(tb1OneLine.Te1t) i' Z :) Len(tb1OneLine.Te1t) L i') iNu6; N /al(s el4) Select sO4erator *ase HZH tb1Result.Te1t N iNu6: Z iNu6; *ase H-H tb1Result.Te1t N iNu6: L iNu6; *ase H^H tb1Result.Te1t N iNu6: ^ iNu6; *ase H5H tb1Result.Te1t N iNu6: 5 iNu6; *ase H_H tb1Result.Te1t N iNu6: _ iNu6; *ase Else DL show a warnin7 i0 an ille7al o4erator is 4ro2ided -"essa7e.Warnin7(HUnknown o4erator3 H J sO4erator) End Select *atch Select Error.*ode *ase ;> "essa7e.Warnin7(O$ou are not allowed to di2ide by IeroXXP) tb1Nu6;.Set+ocus *ase Else (rint Error.*ode

92

.. +our third app#ication$ /um'#ay End Select Error.*lear End #s you can see three new 0unctions are introduced. "id()) a strin7 0unction) !N* is an arith6etical 0unction and /al() a LocaliIation and Translation 0unction. #s you ha2e already worked with se2eral strin7 0unctions) and ha2e been shown where to 0ind the6) it is 4art o0 your learnin7 4rocess to 7et acKuainted with the online %a6bas docu6entation and learn to co64rehend the 0unction "id(). /al() and !N* are e14lained below. "?p8ession N /al( t8ing) /al con2erts a strin7 into a boolean) a nu6ber or a date) accordin7 to the strin7 contents. "?p8ession is the 2ariable that will hold the con2erted 2alue t8ing is the strin7 to con2ert. The order /al checks to deter6ine where to con2ert to is3 - &ate5Ti6e V returns &ate J Ti6e - +loatin7 4oint nu6ber V returns a 0loatin7 4oint nu6ber - >< bit Lon7 Nu6ber V returns a lon7 nu6ber - !nte7er V returns an inte7er - 'oolean V returns 6atchin7 boolean 2alue (9 or -:) - Otherwise V returns Null /al is a LocaliIation and Translation +unction. These 0unctions are use0ul when dealin7 with translations (lan7ua7e) and localisation (currency settin7s and such). "ore on /al see3 htt43557a6basdoc.or75hel45lan752al?28 O2er2iew o0 LocaliIation and Translation +unctions see3 htt43557a6basdoc.or75hel45cat5localiIe?28

I)C !ncre6ents a <a8iable. Ia8iable can be any tar7et or assi7n6ent) but "UST be nu6eric Sa6e as3 /ariable N /ariable Z : or /ariable ZN : !N* is a #rith6etical +unction. These 0unctions are use0ul when dealin7 with nu6eric data. 9ote3 The o44osite o0 !N* is &E* (decre6ents a 2ariable). "ore on !N* see3 htt43557a6basdoc.or75hel45lan75inc?28 O2er2iew o0 #rith6etical +unctions see3 htt43557a6basdoc.or75hel45cat5arith?28 Ti6e to run your a44lication once 6ore and see so6e results. Ty4e these strin7s into tb1OneLine and click *alcTe1t a0ter each entry3 ;Z< <= 5 9 8 ) => aL8

93

.. +our third app#ication$ /um'#ay $ou will notice that 6ost o0 abo2e either returns a result or 7i2es you a 6essa7e what is wron7) e1ce4t 0or the last one. !t does nothin7.

Illustration .4: Division b) @ero ta#en care off

Illustration .$: An#no"n operator ta#en care off

O0 course it is 4ossible to take care o0 this as well. a2e a 7o at it yoursel0 and see what you can co6e u4 with. The solution is 4ro2ided on the ne1t 4a7e) but really try 0i7ure out so6ethin7 yoursel0 0irst be0ore 6o2in7 to the ne1t 4a7e. Re6e6ber) 6ost learnin7 takes 4lace when thin7s 7o wron7) not when e2erythin7 works 0ro6 the be7innin7. !tDs the tryin7 to 6ake it work 4rocess that will stick 6uch lon7er and build co64rehension on the 6atter) not the co4y54aste and run 4rocess. #lso know that o0 you co6e u4 with another solution to 6ake it work doesnDt 6ake it wron7 as it works and you will ha2e learned about %a6bas doin7 so. # little hint 0or on the road3 *heck out !sNu6ber() in the %a6bas docu6entation.

94

.. +our third app#ication$ /um'#ay (ublic Sub btn*alcTe1tM*lick() ... s el4 N "id(tb1OneLine.Te1t) :) i' L :) !0 !sNu6ber(s el4) Then iNu6: N /al(s el4) Else "essa7e.Error(s el4 J H is no nu6berH) Sto4 E2ent Endi0 D-- 0ind the o4erator L i' ZN : ... s el4 N "id(tb1OneLine.Te1t) i' Z :) Len(tb1OneLine.Te1t) L i') !0 !sNu6ber(s el4) Then iNu6; N /al(s el4) Else "essa7e.Error(s el4 J H is no nu6berH) Sto4 E2ent Endi0 Select sO4erator ... End #s you 6i7ht noticed) instead o0 usin7 a Switch to esca4e the e2ent 0ro6 runnin7 e1cessi2e code ! chose to use the Sto4 E2ent. !t does e1actly what is says) it sto4s the e2ent 0ro6 runnin7 any 0urther. TO, "I")T This state6ent 6ust be used in an e2ent handler. !t tells the inter4reter that the e2ent that called the e2ent handler 6ust be cancelled. "ore on STO( E/ENT see3 htt43557a6basdoc.or75hel45lan75sto4e2ent?28 O2er2iew o0 E2ent 6ana7e6ent3 htt43557a6basdoc.or75hel45cat5e2ent?28

<.5 >o* a$out date and time


Ne1t to nu6bers you will 4robably need to handle date and ti6e in0or6ation in so6e o0 the a44lications you 4lan to built. LetDs ha2e a look at that. (lace 0ollowin7 cont8ols on +r6"ain with so6e 4ro4erties 4re-set3 1 #ateBo? Na6e V db1&ate 1 Te?tBo?3 Na6e V tb1&ate: 1 Te?tBo?3 Na6e V tb1&ate; 1 IalueBo?3 Na6e V 2b1&ate When 0inished with 4lacin7 all controls and settin7 all 4ro4erties accordin7ly you should ha2e so6ethin7 like in the 0ollowin7 illustrations.

95

.. +our third app#ication$ /um'#ay

Illustration .&: (ome e3tra controls added to pla) "ith date and time

6$*$1 Adding some code


+irst thin7 you are 7oin7 to do is 6ake sure that when selectin7 a date) the 2alue re4resentin7 the date is shown in 2b1&ate) the date 4art is shown in tb1&ate: and the ti6e 4art is shown in tb1&ate;. Select the &ate'o1 and ri7ht click. !n 6enu select "<ent ; C9ange. Ne1t ty4e 0ollowin7 code3 (ublic Sub db1&ateM*han7e() &i6 i&ays #s &ays !0 Not !sNull(db1&ate./alue) Then i&ays N db1&ate./alue 2b1&ate./alue N i&ays tb1&ate:.Te1t N +or6at(db1&ate./alue) Hdd-66-yyyyH) tb1&ate;.Te1t N +or6at(db1&ate./alue) Hhh3nn3ssH) Endi0 End +irst thin7 you 6i7ht notice is that db1&ate./alue returns a nu6ber o0 days i0 4ut into an !nte7er. Second thin7 you 6i7ht notice is the +or6at() 0unction. With 0or6at you can con2ert 2alues to strin7s in a certain out4ut 0or6at. So the Odd-66-yyyyP 6akes sure the 2alue is only the date 4art and in this order D:@-9@;9:8D. !0 you would chan7e it to Oyyyy-66-ddP it would look like D;9:8-9@-:@D. Sa6e 7oes 0or Ohh3nn3ssP re4resentin7 two di7its 0or hours) 6inutes and seconds se4arated by D3D "ake sure to ha2e a look at the user de0ined 0or6ats in the %a6bas docu6entation. There are a lot o0 e1a64les to be 0ound. Link is in the bo1 on the ne1t 4a7e.

96

.. +our third app#ication$ /um'#ay t8ing N +or6at("?p8ession) S) &o8mat T) *on2erts the e14ression into a strin7 by usin7 the fo8mat that de4ends on the ty4e o0 e?p8ession "?p8ession is the 2ariable that will hold the 2alue that will be con2erted &o8mat is the 0or6at descri4tion (how it needs to be returned) The 0unction can 0or6at date) ti6es) nu6bers and currency. +or6at is a LocaliIation and Translation +unction. These 0unctions are use0ul when dealin7 with translations (lan7ua7e) and localisation (currency settin7s and such). "ore on +or6at see3 htt43557a6basdoc.or75hel45lan750or6at?28 "ore on user de0ined 0or6ats see3 htt43557a6basdoc.or75hel45cat5user0or6at?28 O2er2iew o0 LocaliIation and Translation +unctions see3 htt43557a6basdoc.or75hel45cat5localiIe?28 Run the a44lication and see what it returns when selectin7 a date. Ne1t try the selectin7 the ne1t day and see the 2alue below the &ate'o1 7o u4 with :.

Illustration .*: !fter Date is selected the other controls are updated accordingl)

6$*$% +o8e date and time


To learn so6e 6ore about date and ti6e you will need to add one 6ore control to your 0or6. This control is a s4ecial control na6ed Ti6er. $ou 4lace it on your 0or6 but it will not be seen durin7 runti6e. *heck the illustration on the ne1t 4a7e to see where it is located. (lace 0ollowin7 cont8ol on +r6"ain with so6e 4ro4erties 4re-set3 1 Time8 Na6e V t6r!nter2alY Enabled V True The Ti6er is a 2irtual obEect that 7ets tri77ered in a certain inter2al and runs the code you would like. The inter2al is deter6ined by settin7 the Ti6er 4ro4erty #ela: (in 6illiseconds). The ti6er can be 4laced anywhere on the 0or6 itsel0.

97

.. +our third app#ication$ /um'#ay

Illustration .+: 2imer object can be found in the toolbo3 on tab '(pecial'

Illustration ..: 2imer object can be an)"here on the form

Now this is what you are about to do. E2ery :999 6illiseconds (: second)) tb1&ate: will show current date5ti6e in 0or6at Oddd 666 yyyy hh3nn3ssP) tb1&ate; will show current date5ti6e in 0or6at Ohh3nn3ss3uuP. To 7et current date5ti6e you will use the 0unction Now. Time8 This class i64le6ents a ti6er obEect # ti6er obEect raises e2ents re7ularly) each ti6e it is tri77ered. The a6ount o0 ti6e between each e2ent is s4eci0ied by the #ela: 4ro4erty. "ore on Ti6er see3 htt43557a6basdoc.or75hel45co6457b5ti6er?28 Ri7ht click the Ti6er obEect and click on its Ti6er e2ent and add 0ollowin7 code3

98

.. +our third app#ication$ /um'#ay (ublic Sub t6r!nter2alMTi6er() tb1&ate:.Te1t N +or6at(Now) Hddd 666 yyyy hh3nn3ssH) tb1&ate;.Te1t N +or6at(Now) Hhh3nn3ss3uuH) End

)ow Returns current date and ti6e$ Now is a &ate and Ti6e +unction. "ore on Now see3 htt43557a6basdoc.or75hel45lan75now?28 O2er2iew o0 &ate and Ti6e +unctions see3 htt43557a6basdoc.or75hel45cat5ti6e?28 Ti6e to run your a44lication and see what ha44ens. Just sit back and watch the tb1&ate: and ; chan7e. !0 nothin7 ha44ens you 0or7ot to set the Ti6er 4ro4erty Enabled to true.

Illustration 100: Ever) second the tb3Date1 and 2 change their content

<.

0not#er approac#, enumerate

!t was brou7ht to 6y attention by #lain) one or our contributors) that Nu6(lay 4ro2ided an e1celent occasion to e14lain enu6eratin7. So) thanks to #lain) here is a little e1tra added to the cha4ter.

6$-$1 i witc9 enume8ated


Earlier on (in @.8.:) you used the !nte7er 2ariable iSwitch and based on its 2alue deter6ined what o4erator to use. This is how the switch 2alue re4resented the O4erator in @.8.:3 O4erator3 Z ^ 5 _ Switch 2alue3 9 : ; 8 <

99

.. +our third app#ication$ /um'#ay !n the de6onstrated code in @.8.:) you as a de2elo4er need to know what 2alue re4resents what o4erator. To 6ake li0e easier you could i64le6ent enu6eration. Enu6eration can be used to declare a list o0 inte7er constants. This will hel4 you 7i2e a 6eanin70ul na6e to the di00erent o4erator 2alues that need testin7) as they are inte7ers. Usin7 ENU" your code will look like this3 (ri2ate iSwitch #s !nte7er DL line below is added -(ri2ate ENU" (lusSi7nN9) "inusSi7nN:) "ulti4lySi7nN;) &i2ideSi7nN8) (owerSi7nN< (ublic Sub +or6MO4en() "e.*enter rbt#ddM*lick End (ublic Sub rbt#ddM*lick() lblO4erator.Te1t N rbt#dd.Te1t iSwitch N (lusSi7n End (ublic Sub rbtSubtractM*lick() lblO4erator.Te1t N rbtSubtract.Te1t iSwitch N "inusSi7n D` chan7ed End (ublic Sub rbt"ulti4lyM*lick() lblO4erator.Te1t N rbt"ulti4ly.Te1t iSwitch N "ulti4lySi7n D` chan7ed End (ublic Sub rbt&i2ideM*lick() lblO4erator.Te1t N rbt&i2ide.Te1t iSwitch N &i2ideSi7n End (ublic Sub rbt(owerM*lick() lblO4erator.Te1t N rbt(ower.Te1t iSwitch N (owerSi7n End D` chan7ed D` chan7ed D` chan7ed DTo ensure lblO4erator shows Z

'y usin7 ENU" you can now test iSwicht in your code a7ainst so6ethin7 6ore 6eanin70ul like (lusSi7n instead o0 9 or &i2ideSi7n instead o0 =. !t 6akes 0or 6ore readable and easier 6aintained code in a later sta7e o0 de2elo46ent.

100

.. +our third app#ication$ /um'#ay "nume8ation decla8ation [(ublic \ (ri2ate ] ENU" Identifie8 S N Ialue T S) Identifie8 S N Ialue T ...T This keyword declares an enu6eration or a list o0 inte7er constants. !0 the Ialue o0 a constant is not s4ecidied) then it is the 2alue o0 the 4re2ious constant 4lus one) or Iero 0or the 0irst constant. "ore on Enu6eration declaration see3 htt43557a6basdoc.or75hel45lan75enu6decl?28 "ore on ENU" see3 htt43557a6basdoc.or75hel45lan75enu6?28

!n this cha4ter you ha2e worked with #rith6etical) &ate and ti6e and localiIation and translation 0unctions. $ou will ha2e noticed in this and 4re2ious cha4ters that a lot is 4ossible usin7 0unctions. $ou will co6e to learn that they can be use0ul in a lot o0 situations. They are 2ery 4ower0ul tools to use and so6e o0 the6 you will be usin7 a lot) so 7et 0a6iliar with the6. Now in %a6bas !&E click 6enu &ile ; Fuit. This 7ets you ready 0or the ne1t cha4ter.

101

10.

+our fourth app#ication$ Image'#ay

12 'ou8 fou8t9 application/ Image,la:


1?.1 W#at *ill $e done in t#i" c#apter
"ain 0ocus will be i6a7es. 0or that you will built a new a44lication. This will in2ol2e O4enin7 %a6bas !&E and 6ake a new 4roEect "ake a 6ain 0or6 (lace a nu6ber o0 controls on the 0or6 Write so6e code Run the a44lication 0ro6 %a6bas !&E $ou can learn about3 Workin7 with the %a6bas !&E Workin7 with i6a7es Workin7 with 4roEect 0ile 4aths User interaction (in4ut 0ro6 user) out4ut 0ro6 a44lication)

1?.2 +tart a ne* pro'ect and de"ign a form


O4en %a6bas8 !&E and start a new 7ra4hical a44lication 4roEect na6ed !6a7e(lay. Rena6e 0or6 +"ain to +r6"ain. &o this by ri7ht clickin7 &+ain in the (roEect 'rowser and ne1t click !ename. (lace 0ollowin7 cont8ols on +r6"ain with so6e 4ro4erties 4re-set3 1 ,ictu8eBo?3 Na6e V 4b1OneY Width V :;AY ei7ht V :;A 1 ,ictu8eBo?3 Na6e V 4b1TwoY Width V :;AY ei7ht V :;A 1 .abel3 Na6e V lbl&i6One Y Te1t V e64tyY #li7n6ent V *enter 1 .abel3 Na6e V lbl&i6Two Y Te1t V e64tyY #li7n6ent V *enter 1 Button3 Na6e V btnSwitchY Te1t V Switch !6a7e When 0inished with 4lacin7 all controls and settin7 all 4ro4erties accordin7ly you should ha2e so6ethin7 like in the ne1t illustration.

Illustration 101: ,ontrols placed on 5rm1ain

102

10.

+our fourth app#ication$ Image'#ay

1?.3 4et me "ee "ome image"


12$3$1 +a@ing some images
'e0ore 7ettin7 started with codin7) you will need so6e i6a7es. $ou will create so6e new ones ri7ht in the %a6bas !&E. +irst) in the ,8oCect B8owse8) ri7ht click the 0older &ata and in the 6enu click )ew ; &olde8. Na6e the 0older Images

Illustration 102: ,reate a ne" folder 'Images' in the project Data folder

Ne1t) ri7ht click the 0older Images and in 6enu select )ew ; Image$$$ Na6e the !6a7e "y!6a7e) e1tension (N%) width and hei7ht :;A 4i1els. *lick the newly 6ade i6a7e and 6ake so6ethin7) like colour the back7round) draw a circle. Just 4lay a bit with the !6a7e editor to 6ake so6e i6a7e.

Illustration 10 : 1ade some modern art image

103

10.

+our fourth app#ication$ Image'#ay

Now 0irst sa2e the i6a7e. Use the a<e ,8oCect button in the button bar below the 6enu. $ou will know i0 a i6a7e is sa2ed when the i6a7e is 2isible in the ,8oCect B8owse8 be0ore its na6e. To understand co64are !llustration on 4re2ious 4a7e (unsa2ed "y!6a7e.4n7) and the illustration below (sa2ed "y!6a7e.4n7 and Ne1t!6a7e.4n7). Ne1t) in the ,8oCect B8owse8) ri7ht click +:Image$png ans select Cop:. Now ri7ht click 0older Images and in 6enu select ,aste. Rena6e the i6a7e 0ile by ri7ht clickin7 it and selectin7 !ename$$ Na6e the i6a7e )e?tIm7 age$png and

Illustration 104: Image copied; edited and saved% 2hat ma#es t"o "or#s of art%

The only 7oal o0 this little e1ercise is to end u4 with two di00erent i6a7es siIed :;A 1:;A 4i1els. !0 you are 6issin7 the 4anel with i6a7e 4re2iew) le0t o0 the i6a7e editor) look 0or M and click on it to 6ake the 4anel a44ear. The M will chan7e to N (see illustration below).

Illustration 10$: 1a#e panel appear

$ou will 0ind 6ore o0 these 4anels that can be 2isible or not. So now you know what to look 0or.

12$3$% +a@e t9e images appea8


"ission is to 6ake the i6a7es you ha2e Eust created 2isible. +or this you will 7o the the 4ro4erties o0 both i6a7es and set the (icture 4ro4erty. +or this 0irst select 4b1One and in the ,8ope8tiesJHie8a8c9: 4anel look 0or ,ictu8e. *lick the O to o4en the D elect a ,ictu8eD 0or6. !n the le0t 4ane click o4en the 0older Image and select +:Image$png in the ri7ht 4anel.

104

10.

+our fourth app#ication$ Image'#ay

Illustration 10&: Opening the '(elect a picture' form

Ne1t click OD and see the i6a7e in the +or6 &esi7ner.

Illustration 10*: Image '1)Image%png' in 4icture6o3 picOne

Now do the sa6e 0or 4b1Two) but use Ne1t!6a7e.4n7

Illustration 10+: 6oth images in their 4icture6o3

105

10.

+our fourth app#ication$ Image'#ay

Now ri7ht click the 0or6 and select "<ent ; Open and add ne1t code3 (ublic Sub +or6MO4en() "e.*enter End

Ti6e to run the a44lication and ha2e a look at what you Eust created.

Illustration 10.: 8unning Image4la) for the first time

Well the i6a7es should be there. 'ut the button DSwitch !6a7eD still needs so6e work. %oal is to switch the i6a7es in the (icture'o1es when clickin7 button DSwitch !6a7eD. !n the editor you used the (icture'o1 4ro4erty (icture to add the i6a7e to the (icture'o1. So si64ly e1chan7in7 their 4ro4erties should do the Eob. 'ut 0or that you will need a te64orary stora7e 0or one o0 the two 4ro4erties. So you need to do so6ethin7 like3 :. Store 4icOne (icture 4ro4erty in the te64orary stora7e ;. Store 4icTwo (icture 4ro4erty in 4icOne (icture 4ro4erty 8. Store the te64orary stora7e in 4icTwo (icture 4ro4erty +or the te64orary stora7e you need a 2ariable o0 ty4e (icture. This because the (icture 4ro4erty o0 a (icture'o1 holds content o0 ty4e (icture. ,ictu8e This class re4resents a 4icture. The 4icture contents are stored in the dis4lay ser2er) not in the 4rocess 6e6ory like an !6a7e. "ore on (icture see3 htt43557a6basdoc.or75hel45co6457b.Kt<54icture?28 'ack to codin7 the click e2ent o0 btnSwitch3

106

10. (ublic Sub btnSwitchM*lick() &i6 4icTe64 #s (icture 4icTe64 N 4b1One.(icture 4b1One.(icture N 4b1Two.(icture 4b1Two.(icture N 4icTe64 End

+our fourth app#ication$ Image'#ay

Run the a44lication and click DSwitch 4ictureD to see i0 it works.

Illustration 110: ,lic#ing the button s"itches the images bet"een the 4icture6o3es

12$3$3 Code t9e labels


There are still two labels on the 0or6 that need so6e codin7. The labels will be used to show the di6ensions o0 the loaded i6a7e in the (icture'o1es abo2e. +or this you will need to read the (icture 4ro4erties Width and ei7ht. 'ack to codin73 (ublic Sub +or6MO4en() "e.*enter lbl&i6One.Te1t N 4b1One.(icture.Width J H 1 H J 4b1One.(icture. ei7ht J H 4i1elsH

lbl&i6Two.Te1t N 4b1Two.(icture.Width J H 1 H J 4b1Two.(icture. ei7ht J H 4i1elsH End Run the a44lication to see the result.

107

10.

+our fourth app#ication$ Image'#ay

Illustration 111: -abels no" sho"ing the image dimensions

When switchin7 the i6a7es nothin7 ha44ens to the Labels) so ti6e to 6ake a new i6a7e. !n D(roEect 'rowserD ri7ht click !6a7e 0older to create S6all!6a7e.4n7 (>< 1 ><) and 'i7!6a7e.4n7 (;=> 1 ;=>).

Illustration 112: 2"o more png images made in the editor

Now 7o to the +r6"ain in !&E and chan7e the 4ro4erties o0 4b1One and 4b1Two. "ake sure 4b1One has Little!6a7e.4n7 in its (icture 4ro4erty and 4b1Two has 'i7!6a7e.4n7 in its (icture 4ro4erty. Once done run the a44lication a7ain and see what it looks like.

108

10.

+our fourth app#ication$ Image'#ay

Illustration 11 : 8unning the application "ith the ne" images

When runnin7 the a44lication you will see that the labels 7i2e the 4ro4er di6ensions) but the i6a7es arenDt shown 4ro4erly. Little!6a7e.4n7 is To4Le0t in 4b1One) 'i7!6a7e.4n7 can only be seen 4artial) because o0 its siIe bein7 ;=> 1 ;=>) where 4b1Two is :;A 1 :;A. So that are so6e thin7s that need attention. Try clickin7 button DSwitch !6a7eD and see what ha44ens.

Illustration 114: Images s"itch but labels sho" "rong dimensions

#s you can see) the i6a7es switch) but the labels donDt. This 6akes sense as you coded the +or6MO4en e2ent) 6eanin7 that only on o4enin7 the 0or6 the Labels 7et their content. There is no code on the btnSwitchM*lick e2ent doin7 the sa6e. So 6ore work is needed. +irst you need to 6ake sure that an i6a7e is shown in its total in the (icture'o1es) no 6atter what its siIe is.

109

10.

+our fourth app#ication$ Image'#ay

Second you need to 6ake the Labels show the 4ro4er di6ensions. LetDs 7et to work3 +irst set the 4ro4erties o0 the both (icture'o1es to 6eet this3 #li7n6ent V *enter Stretch V True The 4ro4erty Stretch will 6ake sure the i6a7e is ada4ted to 6eet the di6ensions o0 the (icture'o1es) the #li7n6ent will center the i6a7e in the (icture'o1es. Ne1t 6ore code is needed to 6ake the Labels show the 4ro4er di6ensions. (ublic Sub btnSwitchM*lick() &i6 4icTe64 #s (icture 4icTe64 N 4b1One.(icture 4b1One.(icture N 4b1Two.(icture 4b1Two.(icture N 4icTe64 lbl&i6One.Te1t N 4b1One.(icture.Width J H 1 H J 4b1One.(icture. ei7ht J H 4i1elsH lbl&i6Two.Te1t N 4b1Two.(icture.Width J H 1 H J 4b1Two.(icture. ei7ht J H 4i1elsH End Now run the a44lication a7ain.

Illustration 11$: !ll "or#s as e3pected

$ou will 0ind all workin7 as e14ected) e2en when switchin7 the i6a7es the labels show th 4ro4er di6ensions. One thin7 you 6i7ht ha2e notices is that the code used 0or showin7 the label di6ensions in both +or6MO4en and btnSwitchM*lick e2ent are e1actly the sa6e. This 6eans you can write a routine with that code and call the routine 0ro6 both e2ents) rather than ha2in7 sa6e code at two 4laces. #d2anta7e is that i0 you need to chan7e so6ethin7 to the code) it only needs to be chan7ed at one 4lace. #s you only need this routine on +r6"ain you will code 0or a (ri2ate routine. ere is the 0ull code 0or +r6"ain3

110

10. (ublic Sub +or6MO4en() "e.*enter Show&i6ensions End (ublic Sub btnSwitchM*lick() &i6 4icTe64 #s (icture 4icTe64 N 4b1One.(icture 4b1One.(icture N 4b1Two.(icture 4b1Two.(icture N 4icTe64 Show&i6ensions End (ri2ate Sub Show&i6ensions()

+our fourth app#ication$ Image'#ay

lbl&i6One.Te1t N 4b1One.(icture.Width J H 1 H J 4b1One.(icture. ei7ht J H 4i1elsH lbl&i6Two.Te1t N 4b1Two.(icture.Width J H 1 H J 4b1Two.(icture. ei7ht J H 4i1elsH End

1?.4 4oading image" u"ing code


Last 4art o0 i6a7e handlin7 you will ha2e a look at is loadin7 the i6a7es usin7 code. +or this you will need to add so6e 6ore controls to the 0or6. (lace 0ollowin7 cont8ols on +r6"ain with so6e 4ro4erties 4re-set3 1 Button3 Na6e V btnOne"yY Te1t V Load "y 1 Button3 Na6e V btnOneS6allY Te1t V Load S6all 1 Button3 Na6e V btnTwoNe1t Te1t V Load Ne1t 1 Button3 Na6e V btnTwo'i7Y Te1t V Load 'i7 When 0inished with 4lacin7 all controls and settin7 all 4ro4erties accordin7ly you should ha2e so6ethin7 like in the ne1t illustration.

Illustration 11&: !ll ne" controls placed on 5rm1ain

111

10.

+our fourth app#ication$ Image'#ay

12$($1 T9e mission


Now that controls are in 4lace this is what needs to be done3 :. btnOne"y needs to load "y!6a7e.4n7 into 4b1One u4on clickin7 ;. btnOneS6all needs to load S6all!6a7e.4n7 into 4b1One u4on clickin7 8. btnTwoNe1t needs to load Ne1t!6a7e.4n7 into 4b1Two <. btnTwo'i7 needs to load 'i7!6a7e.4n7 into 4b1Two To load an i6a7e into a (icture'o1 4ro4erty (icture you will use (icture.Load. ,ictu8e$.oad Static +unction Load ((ath #s Strin7) #s (icture Loads a 4icture 0ro6 the disk. "ore on (icture see3 htt43557a6basdoc.or75hel45co6457b.Kt<54icture5load?28 To be able to load the 4icture you will need a 4ath. Since the i6a7es are within the 4roEect 0older so6e 6ore e14lanation is needed.

Illustration 11*: 4roject folder structure

+or %a6bas the &ata 0older 4resents the root 0older. So addressin7 the !6a7es 0older will 7i2e 4ath3 O!6a7esP. !0 you need 'i7!6a7e.4n7 the 4ath looks like3 O!6a7es5'i7!6a7e.4n7P:> When ty4in7 the code in !&E you will see that the auto-co64lete 0unction will reco7nise the 4ath and 0iles in there. Just hit "nte8 when 4ro4er 0ile is selected instead o0 ty4in7 al.

Illustration 11+: !uto/complete function of IDE "ill recognise the path


15

More on #aths and Gambas1 htt#1//gambasdoc.org/hel#/cat/#ath

112

10. The code looks like this3 (ublic Sub btnOne"yM*lick()

+our fourth app#ication$ Image'#ay

4b1One.(icture N (icture.Load(H!6a7es5"y!6a7e.4n7H) End (ublic Sub btnOneS6allM*lick() 4b1One.(icture N (icture.Load(H!6a7es5S6all!6a7e.4n7H) End (ublic Sub btnTwoNe1tM*lick() 4b1Two.(icture N (icture.Load(H!6a7es5Ne1t!6a7e.4n7H) End (ublic Sub btnTwo'i7M*lick() 4b1Two.(icture N (icture.Load(H!6a7es5'i7!6a7e.4n7H) End #0ter this all it takes is run and test.

Illustration 11.: 2he pictures seem to load but the labels aren't adapted

12$($% +a@e t9e labels wo8@ again


'ecause you 6ade a routine 0or u4datin7 the labels content) it shouldnDt be to hard to 7et the labels to work as needed. Si64ly call the routine 0ro6 each button click e2ent should do the trick. This is what is looks like3

113

10.

+our fourth app#ication$ Image'#ay

(ublic Sub btnOne"yM*lick() 4b1One.(icture N (icture.Load(H!6a7es5"y!6a7e.4n7H) Show&i6ensions End (ublic Sub btnOneS6allM*lick() 4b1One.(icture N (icture.Load(H!6a7es5S6all!6a7e.4n7H) Show&i6ensions End (ublic Sub btnTwoNe1tM*lick() 4b1Two.(icture N (icture.Load(H!6a7es5Ne1t!6a7e.4n7H) Show&i6ensions End (ublic Sub btnTwo'i7M*lick() 4b1Two.(icture N (icture.Load(H!6a7es5'i7!6a7e.4n7H) Show&i6ensions End #7ain) run the a44lication to see i0 the ada4tation to the code works.

Illustration 120: -oading images no" adapts the -abels accordingl)

This concludes this cha4ter. !n %a6bas !&E click 6enu &ile ; Fuit. This 7ets you ready 0or the ne1t cha4ter.

114

11.

+our fifth app#ication$ Simp#e%e&t)ditor

11 'ou8 fift9 application/ impleTe?t"dito8


11.1 W#at *ill $e done in t#i" c#apter
"ain 0ocus will be loadin7 and sa2in7 data and 6enus. 0or that you will built a new a44lication. This will in2ol2e O4enin7 %a6bas !&E and 6ake a new 4roEect "ake a 6ain 0or6 (lace a nu6ber o0 controls on the 0or6 "ake a 6enu and a toolbar Write so6e code Run the a44lication 0ro6 %a6bas !&E $ou can learn about3 Workin7 with the %a6bas !&E Sa2in7 data to 0ile O4enin7 a sa2ed 0ile "akin7 6enus and codin7 the6 "akin7 Toolbars and codin7 the6 Workin7 with local 0ile 4aths User interaction (in4ut 0ro6 user) out4ut 0ro6 a44lication)

11.2 +tart a ne* pro'ect and de"ign a form


O4en %a6bas8 !&E and start a new 7ra4hical a44lication 4roEect na6ed Si64leTe1tEditor. Rena6e 0or6 +"ain to +r6"ain. &o this by ri7ht clickin7 &+ain in the (roEect 'rowser and ne1t click !ename. (lace 0ollowin7 cont8ol on +r6"ain with so6e 4ro4erties 4re-set3 1 Te?tA8ea3 Na6e V t1a"ainY Te1t V e64ty When 0inished with 4lacin7 the control and settin7 all 4ro4erties accordin7ly you should ha2e so6ethin7 like in the ne1t illustration.

Illustration 121: 5rm1ain after placing the control

115

11.

+our fifth app#ication$ Simp#e%e&t)ditor

11.3 /aking a menu


'elie2e it or not) in this cha4ter you are 7oin7 to build a 0ull 0unctional te1t editor. With the 0or6 6ade you already can enter te1t in the Te1t#rea. Only thin7 6issin7 is sa2in7 it to a te1t 0ile. +or that you could add a sa2e button) but that is not what you will be doin7 here. To 6ake a 6enu ri7ht click +r6"ain in the !&E and select +enu edito8$$$ or 4ress Ct8l G ".

Illustration 122: (elect 1enu editor%%%

Once the 6enu editor is o4ened add @ 6enu entries by clickin7 button Inse8t @ ti6es.

Illustration 12 : !dding . menu entries in the 1enu editor

116

11.

+our fifth app#ication$ Simp#e%e&t)ditor

#ssu6in7 "enu@ is the 0irst and "enu: the last in the list) this is what you need to do 0or each o0 the 6enu entries:B3 +enu6/ Na6e V 6nu+ileY *a4tion V +ile +enu3/ Na6e V 6nu+ileNewY *a4tion V NewY *lick button Indent in Toolbar (see below) +enu1/ Na6e V 6nu+ileO4enY *a4tion V O4en..Y *lick button Indent in Toolbar +enu-/ Na6e V Se4erator:Y *a4tion V e64tyY *lick button Indent in Toolbar +enu*/ Na6e V 6nu+ileSa2eY *a4tion V Sa2eY *lick button Indent in Toolbar +enu(/ Na6e V 6nu+ileSa2e#sY *a4tion V Sa2e #s...Y *lick button Indent in Toolbar +enu3/ Na6e V 6nu+ile*loseY *a4tion V *loseY *lick button Indent in Toolbar +enu%/ Na6e V Se4erator;Y *a4tion V e64tyY *lick button Indent in Toolbar +enu1/ Na6e V 6nu+ileauitY *a4tion V auitY *lick button Indent in Toolbar

Illustration 124: Indent the menu entr)

!llustration below is how the end result should look like in the "enu Editor.

Illustration 12$: 2he end result of editing the menu entries

Ne1t close the "enu Editor and in !&E ha2e a look at your 0or6. *lick on +ile to see what ha44ens.
17

<ou do this by clic-ing the menu entry in the menu editor list and next changing the #ro#erties in the fields below the menu list. The selected menu entry will be the one you are editing.

117

11.

+our fifth app#ication$ Simp#e%e&t)ditor

Illustration 12&: 2here is a menu 5ile on 5rm1ain in IDE

11$3$1 Coding t9e menu ent8: )ew


Now click on )ew in the newly 6ade +ile 6enu and you will be taken to the code editor to the click e2ent o0 this 6enu entry. Lets add so6e code to +r6"ain3 (ublic Sub +or6MO4en() "e.*enter t1a"ain./isible N +alse End (ublic Sub 6nu+ileNewM*lick() t1a"ain.*lear t1a"ain./isible N True End Now run the a44lication and click 6enu &ile ; )ew to see what will ha44en. DNeeded i0 another te1t docu6ent is already loaded

Illustration 12*: 6efore and after clic#ing menu 5ile /7 'e"

118

11.

+our fifth app#ication$ Simp#e%e&t)ditor

11$3$% Adding Icon and @e:boa8d s9o8tcut to menu ent8: )ew


'ack to the "enu Editor to add an icon and a keyboard shortcut. Once in "enu Editor click on the 6enu entry 0or New and set shortcut to CT!. and )

Illustration 12+: (et the #e)board shortcut for menu 'e"

Ne1t at the 4ro4erty (icture click on the 0irst button behind the te1tbo1 o0 (icture to o4en the (icture dialo7. Select Tab toc@ and select the )ew icon:A.

Illustration 12.: (electing the ne" icon

19

0cons might differ from des-to# to des-to#& de#ending on ty#e of des-to# and themes selected.

119

11.

+our fifth app#ication$ Simp#e%e&t)ditor

*lick OD to close the (icture dialo7 a0ter you selected the icon )ew. Ne1t click OD a7ain to close the "enu Editor. Now run the a44lication a7ain and once runnin7 on your keyboard 4ress Ct8l G ) and see what ha44ens. This should 7i2e you an new e64ty te1t area) sa6e as when clickin7 the +ile V New entry. So no e1tra codin7 to 7et the Eob done. Now click on 6enu &ile to see your 6enu icon.

Illustration 1 0: 5ile menu "ith the ne"l) added icon for 'e"

Now that you can create a new te1t 0ile and are able to enter te1t into the Te1t#rea) ne1t ste4 will be to sa2e the content o0 the Te1t#rea to a 0ile.

11.4 +a1ing )our fir"t text file


When you want to sa2e a newly 6ade te1t 0ile you will want to browse 0or a location) but when the 0ile has been o4ened 0or editin7 you will already know where to sa2e. So) what we actually ha2e when sa2in7 a 0ile is two situations3 :. New te1t 0ile V no known location:@ ;. E1istin7 te1t 0ile V known location (as it has been o4ened 0ro6 so6ewhere) !0 you co64are this to Sa2e #s you will ha2e only one situation3 :. E1istin7 te1t 0ile V unknown location (althou7h it has been o4ened 0ro6 so6ewhere) See6s that one thin7 you need to know is whether a te1t 0ile is new or has been o4ened. #nd it also see6s that the actions reKuired 0or sa2in7 a new te1t 0ile are the sa6e as doin7 a sa2e as 0or an e1istin7 te1t 0ile. #nother thin7 o0 i64ortance is) that when sa2in7 an e1istin7 0ile you will need to know its location (bein7 4ath and 0ull na6e). These are the 0actors that will deter6ine our a44roach in sa2in7 te1t 0iles.

11$($1 On a mission
$es) !D6 7oin7 to send you on a 6ission. # 6ission to3

1:

ocation in the meaning of #ath and file name.

120

11.

+our fifth app#ication$ Simp#e%e&t)ditor

:. #dd a sa2e icon to 6enu sa2e ;. #dd the keyboard entry *trl Z S to the sa2e 6enu 8. a2e the code editor ready 0or codin7 the click e2ent 0or the Sa2e 6enu $our 6enu should look like the illustration on the ne1t 4a7e. !0 you donDt know how) read the back the 4art on how you did this 0or the 6enu entry New.

Illustration 1 1: 1a#e sure )our menu entr) for (ave has an icon and #e)board entr)

+enus and @e:boa8d s9o8tcuts !0 you take a closer look at 6enus in co66on a44lications like web browsers) docu6ent 6ana7ers) 6ail clients or whate2er) you will notice that 6enus ha2e a certain order and that this is actually o0 i64ortance. Why could this be i64ortant? !6a7ine one de2elo4er o0 a44lications 4lacin7 a +ile 6enu on a di00erent location) another one 4lacin7 Sa2e in 6enu entry Edit) another one thin7 that auit is better o0 in 6enu "isc. That would create a lot o0 con0usion a6on7st 4eo4le usin7 the di00erent a44lications as they would ha2e to search 0or a 0unction in di00erent 4laces de4endin7 on the a44lication they are usin7. So ha2in7 so6e sort a7ree6ent on 6enus 6akes li0e easier 0or those usin7 your a44lications. No user will e14ect the +ile 6enu to be at another location than ri7ht at the be7innin7 o0 the 6enu. No user will look 0or Sa2e in any other 6enu that +ile. So in all it 6akes sense. Sa6e 0or keyboard shortcuts. !0 you would use *trl Z S to o4en a 0ile) a lot o0 users would 7et con0used as they are used to *trl Z S 0or sa2in7. There are so6e directi2es 0or both %no6e and ,&E de2elo46ent. These directi2es are known as @uman interface guidelines or hig. a2e a look at the6 i0 you 4lan on desi7nin7 a44lications with 6enus) so you can 6ake 6enus that a44eal to the already 4resent D6enu Dintuition o0 the 4eo4le usin7 the6. "ore on u6an inter0ace 7uidelines3 htt4s355en.wiki4edia.or75wiki5 u6anMinter0aceM7uidelines %no6e hi7 on 6enus3 htt4s355de2elo4er.7no6e.or75hi7-book5stable56enus.ht6l.en ,&E hi7 on 6enus3 htt4355techbase.kde.or75(roEects5Usability5 !%5"enuM'ar

11$($% .ets sa<e some te?t


Now that all is a 4lace you can start workin7 0or a solution based on the earlier analysis 6ade.

121

11.

+our fifth app#ication$ Simp#e%e&t)ditor

!0 a 0ile is o4ened you know two thin7s3 :. its location ;. that it is NO new 0ile So you could work with a 2ariable that is set to true when a 0ile is loaded. When 0alse this 6eans you ha2e a new 0ile and no location. When true you ha2e an e1istin7 0ile and a location. E14ect that this 2ariable will be needed all o2er the +r6"ain) so best 6ake it a (ri2ate 2ariable. $ou ha2e learned about the di00erence between (ri2ate and (ublic. (ri2ate 6eanin7 that it can only be used within the class (in our case +r6"ain.*lass)) (ublic 6eanin7 you can use it in other classes as well (re6e6ber the search and re4lace e1a64le in Te1t(lay(T)wo). Ia8iables <e8sus ,8i<ate <a8iables <e8sus ,ublic <a8iables 3or AregularA variables you use &i6 to declare the6. Their reach is the +unction or "ethod they are declared in. $ou canDt use the6 outside the 0unction or 6ethod where they where declared. #s shortly discussed in cha4ter =. So6e 7ood 4ractices) and seen in the e1a64les so 0ar) ! 4recede 6y 2ariable na6es with a letter like s or !. This is to identi0y the ty4e o0 2ariable. 3or #rivate variables you use (ri2ate to declare the6. Their reach is the entire class they are declared in. $ou can use the6 in any 0unction or 6ethod within the class they where declared. $ou canDt use the6 in other classes. When usin7 (ri2ate 2ariables ! use the sa6e con2ention as 0or Dre7ularD 2ariables but add a b si7n in 0ront o0 the letter to 6ake the6 stick out and know as (ri2ate 2ariables. 3or #ublic variables you use (ublic to declare the6. Their reach is the entire class they are declared in and all other classes in the 4roEect. $ou can use the6 in any 0unction or 6ethod within the 4roEect they where declared. $ou can use the6 in other classes. +or (ublic 2ariables ! donDt use the sa6e con2ention as 0or Dre7ularD 2ariables. ! 7i2e the6 a 6eanin70ul na6e be7innin7 with a ca4ital letter and no indication o0 ty4e. ome e?amples/ T:pe Strin7 !nte7er) Lon7) Short) 'yte +loat 'oolean ObEect (handle) *ollection #rrays D!egula8D ,8i<ate ,ublic s"yNa6e bs"yNa6e "yNa6e i"yNu6 bi"yNu6 "yNu6 0"yNu6 b 0"yNu6 "yNu6 b!sLoaded bb!sLoaded !sLoaded h+ileToLoad bh+ileToLoad +ileToLoad c"y*oll bc"y*oll "y*oll deter6ined by ty4e (e.7 #rray o0 strin7s V sNa6es)

This a44roach is one that has 7rown throu7hout years o0 a44lication de2elo46ent in other lan7ua7es. !t is al6ost the sa6e as the na6in7 con2ention used in the in %a6bas written co64onents source code and in the o00icial e1a64les. ! ha2e nick na6ed 6y na6in7 a44roach the D7bwilly con2entionD 3) U4 to so6e codin7. #dd the 0ollowin7 to +r6"ain.class3 (ri2ate bbLoaded #s 'oolean N +alse (ublic Sub +or6MO4en() ...

122

11.

+our fifth app#ication$ Simp#e%e&t)ditor

U End (ublic Sub 6nu+ileSa2eM*lick() !0 bbLoaded Then DJust sa2e the 0ile Else DThis 6eans you ha2e a new 0ile (rint HNew 0ileH DNow you will need a 4ath and a 0ile na6e to sa2e to Endi0 End Now run and 6ake a new te1t 0ile and watch the !&E console out4ut. !t should 4rint DNew 0ileD when you click 6enu &ile ; a<e. Ne1t challen7e you 0ace is that you need so6e 0or6 o0 interaction with the user to 4ro2ide the a44lication with a sa2e 4ath and a na6e o0 the 0ile) 0or you to use when sa2in7 the 0ile.

11$($3 Get name and pat9 to sa<e to


To inter0ace with the user 0or 7ettin7 a sa2e 4ath and 0ile na6e you will be usin7 &ialo7. #ialog$ a<e&ile 4gb$fo8m$dialog5 Static +unction Sa2e+ile() #s 'oolean *alls the 0ile standard dialo7 to 7et the na6e o0 a 0ile to sa2e. Returns TRUE i0 the user clicked on the *ancel button) and +#LSE i0 the user clicked on the O, button. "ore on &ialo7.Sa2e+ile see3 htt43557a6basdoc.or75hel45co6457b.0or6.dialo75dialo75sa2e0ile? 28 "ore on &ialo7 see3 htt43557a6basdoc.or75hel45co6457b.0or6.dialo75dialo7?28 So &ialo7 is what you will use) back to the code editor in !&E. (ublic Sub 6nu+ileSa2eM*lick() !0 bbLoaded Then DJust sa2e the 0ile Else DThis 6eans you ha2e a new 0ile (rint HNew 0ileH DNow you will need a 4ath and a 0ile na6e to sa2e to D--- ST#RT O+ #&&E& *O&E ---&ialo7.Title N #44lication.Title J (H - Sa2e te1t 0ileH) &ialo7.(ath N User. o6e !0 &ialo7.Sa2e+ile() Then Return +ile.Sa2e(&ialo7.(ath) t1a"ain.Te1t) "essa7e.!n0o(H+ile H J +ile.Na6e(&ialo7.(ath) J H sa2ed.H) D--- EN& O+ #&&E& *O&E ----

123

11.

+our fifth app#ication$ Simp#e%e&t)ditor

Endi0 End

&ile$ a<e 4gb5 Static Sub Sa2e(+ileNa6e #s Strin7) &ata #s Strin7) Sa2e the contents o0 a strin7 into a 0ile. &ile$)ame 4gb5 Static +unction Na6e ((ath #s Strin7) Returns the na6e co64onent o0 a 0ile 4ath (na6e co64onent is 0ilena6e and 0ile e1tension). "ore on +ile.Sa2e see3 htt43557a6basdoc.or75hel45co6457b50ile5sa2e?28 "ore on +ile.Na6e see3 htt43557a6basdoc.or75hel45co6457b50ile5na6e?28 "ore on +ile see3 htt43557a6basdoc.or75hel45co6457b50ile?28 Use8$Home 4gb5 Static (ro4erty Read o6e #s Strin7 Returns the ho6e directory o0 the current user. "ore on User. o6e see3 htt43557a6basdoc.or75hel45co6457b5user5ho6e "ore on User see3 htt43557a6basdoc.or75hel45co6457b5user Run the a44lication) start a new te1t 0ile) ty4e so6e te1t.

Illustration 1 2: 8ead) to save m) first file

Ne1t click &ile ; a<e. !n the dialo7 0or6 browse to a location you want to sa2e the 0ile) in the Na6e 0iled ty4e Dtest.t1tD and click button a<e.

124

11.

+our fifth app#ication$ Simp#e%e&t)ditor

Illustration 1

: Dialog (ave te3t file

$ou will recei2e noti0ication when sa2ed.

Illustration 1 4: 'otification that the file has been saved

125

11.

+our fifth app#ication$ Simp#e%e&t)ditor

Run the a44lication a7ain o4en a new 0ile) ty4e so6e te1t) click &ile ; a<e and in the dialo7 click Cancel. Now that the 0ile is sa2ed you ha2e a e1cuse to code o4enin7 a 0ile) to see i0 the 0ile was really sa2ed.

11.5 (pening )our fir"t text file


O4enin7 the te1t 0ile is kind o0 re2erse to sa2in7 a te1t 0ile. +or o4enin7 the te1t 0ile you will need the user to browse to the 0ile and select it. Ne1t load the content o0 the te1t 0ile into t1a"ain. "ake sure to kee4 track o0 the 0act that the 0ile was loaded (bbLoaded) and to kee4 track o0 its location (4ath and 0ile na6e). So you need another 2ariable to store the location. 'est 7o (ri2ate a7ain as you 6i7ht need it in 6ore than one 6ethod.

11$*$1 Code fo8 opening


To inter0ace with the user 0or 7ettin7 a 0ile you will be usin7 &ialo7 a7ain. #ialog$Open&ile 4gb$fo8m$dialog5 Static +unction O4en+ile(S"ulti #s 'ooleanT) #s 'oolean *alls the 0ile standard dialo7 to 7et the na6e o0 a 0ile to o4en. Returns TRUE i0 the user clicked on the *ancel button) and +#LSE i0 the user clicked on the O, button. "ore on &ialo7.O4en+ile see3 htt43557a6basdoc.or75hel45co6457b.0or6.dialo75dialo75o4en0ile? 28 "ore on &ialo7 see3 htt43557a6basdoc.or75hel45co6457b.0or6.dialo75dialo7?28 So &ialo7)O4en+ile is what you will use) back to the %a6bas !&E. (ri2ate bbLoaded #s 'oolean N +alse (ri2ate bs(ath+ileLoaded #s Strin7 U (ublic Sub 6nu+ileO4enM*lick() &ialo7.Title N #44lication.Title J (H - O4en te1t 0ileH) &ialo7.(ath N User. o6e !0 &ialo7.O4en+ile() Then Return t1a"ain.Te1t N +ile.Load(&ialo7.(ath) t1a"ain./isible N True D"ake the Te1t#rea 2isible once 0ile loaded bs(ath+ileLoaded N &ialo7.(ath DStore the 0ull 4ath o0 the loaded 0ile bbLoaded N True DSet to True indicatin7 NO new 0ile End

&ile$.oad 4gb5 Static +unction Load(+ileNa6e #s Strin7) #s Strin7 Loads a 0ile and returns its contents as a strin7. "ore on +ile.Load see3 htt43557a6basdoc.or75hel45co6457b50ile5load?28 "ore on +ile see3 htt43557a6basdoc.or75hel45co6457b50ile?28 Run the a44lication and try to o4en the 0ile Dtest.t1tD) you 6ade and see i0 it works.

126

11.

+our fifth app#ication$ Simp#e%e&t)ditor

Illustration 1 $: Dialog Open te3t file

$ou should see that it works Eust 0ine. 'ut there is one thin7 you 0or7ot. The sa2in7 4art is coded 0or a new te1t 0ile) but not 0or an o4ened te1t 0ile. So 0irst back to the 6nuSa2e click e2ent.

11$*$% +a@e su8e an opened file can be sa<ed


$ou can 7o strai7ht to the !&E as you ha2e 6ana7ed to 7ather all intelli7ence (like a detecti2e) on the o4ened 0ile) the Eob should be easy. (ublic Sub 6nu+ileSa2eM*lick() !0 bbLoaded Then DJust sa2e the 0ile D--- ST#RT O+ #&&E& *O&E ---+ile.Sa2e(bs(ath+ileLoaded) t1a"ain.Te1t) "essa7e.!n0o(H+ile H J +ile.Na6e(bs(ath+ileLoaded) J H sa2ed.H) D--- EN& O+ #&&E& *O&E ---Else DThis 6eans you ha2e a new 0ile

127

11.

+our fifth app#ication$ Simp#e%e&t)ditor

Once added) run the a44lication) o4en test.t1t) add a line o0 te1t and sa2e.

Illustration 1 &: (aved the changes to the te3t file

*lose the a44lication and start it a7ain. O4en test.t1t and you should see a chan7ed 0ile.

11. 4ittle interme@@o


Ti6e to ha2e a look at the 6enu and how it looks. The 0act that you can sa2e) e2en when no te1t 0ile o4ened is at least disturbin7. Just try it) run the a44lication) do NOT o4en a 0ile or create a new one and click 6enu &ile ; a<e. The a44lication will not crash) but sa2e an e64ty 0ile. 'ut it does look odd. So the 0ile 6enu should beha2e accordin7 to the 0ile bein7 o4ened or not. When no 0ile there are so6e 6enu ite6s you would like to see disabled. These are3 :. Sa2e ;. Sa2e as... 8. *lose $ou know that bbLoaded will tell you i0 an a44lication is loaded) so e2ery ti6e this 'oolean is chan7ed) these 6enus need to chan7e. +or this you will create a (ri2ate 6ethod that sets the abo2e 6entioned 6enu ite6s to enabled or disabled accordin7 to the 2alue in bbLoaded;9. 'ack to editin7 the code in the !&E. (ri2ate Sub +or6MO4en() ... Set+ile"enu End D#dd to the end o0 the +or6MO4en e2ent

$'

<ou see that Fb oaded is being used all over the #roject. 8ow you -now why it has been declared Drivate.

128

11.

+our fifth app#ication$ Simp#e%e&t)ditor

(ublic Sub 6nu+ileO4enM*lick() U Set+ile"enu End (ublic Sub 6nu+ileauitM*lick() "e.*lose End (ri2ate Sub Set+ile"enu() !0 bbLoaded Then 6nu+ileSa2e.Enabled N True 6nu+ileSa2e#s.Enabled N True 6nu+ile*lose.Enabled N True Else 6nu+ileSa2e.Enabled N +alse 6nu+ileSa2e#s.Enabled N +alse 6nu+ile*lose.Enabled N +alse Endi0 End Now run the a44lication and see the 6enu without and with o4ened te1t 0ile. D#bout ti6e you can close the a44lication D#dd to the end o0 the 6nu+ileO4enM*lick e2ent

Illustration 1 *: 4art of the 5ile menu disabled as should be

This will work Eust test) but there is one e1ce4tion when is will not work as e14ected. See i0 you can disco2er it.

129

11.

+our fifth app#ication$ Simp#e%e&t)ditor

Well) did you disco2er it? !0 not run the a44lication a7ain 0ro6 scratch. *lick on &ile ; )ew (or use Ct8l G ))) ty4e so6e te1t and try to sa2e it.

Illustration 1 +: Oops%%; no "a) to save a ne" te3t file%%

&id you 0ind it hard to sa2e the te1t 0ile? Well) ! didnDt 6ana7e. %uess all you need is so6e 6ore code to sol2e this 4roble6. So) when a new 0ile is created you would like to be able to3 :. Sa2e the 0ile ;. *lose the 0ile (e2en without sa2in7) When closin7 a 0ile 6ake sure that bbLoaded is set to +alse) bs(ath+ileLoaded and t1a"ain are e64ty. #nother thin7 to take care o0 is that when a new 0ile is sa2ed 6ake sure that bbLoaded is set to True and bs(ath+ileLoaded holds the 4ath o0 the newly sa2ed 0ile. 'ack to the editor3 (ublic Sub 6nu+ileNewM*lick() U Set+ile"enuNew End (ublic Sub 6nu+ileSa2eM*lick() U Else U bs(ath+ileLoaded N &ialo7.(ath D#dd to the end o0 the 6nu+ileNewM*lick e2ent

130

11.

+our fifth app#ication$ Simp#e%e&t)ditor

bbLoaded N True "essa7e.!n0o(H+ile H J +ile.Na6e(&ialo7.(ath) J H sa2ed.H) Endi0 End (ublic Sub 6nu+ile*loseM*lick() t1a"ain.*lear t1a"ain./isible N +alse bs(ath+ileLoaded N Null bbLoaded N +alse Set+ile"enu End ... (ri2ate Sub Set+ile"enuNew() 6nu+ileSa2e.Enabled N True 6nu+ile*lose.Enabled N True End Run the a44lication and all should be workin7 correctly. Start a new te1t 0ile and see 0 you can sa2e it. Sa2e it and the Sa2e as should a44ear as well. Ne1t close it and the +ile 6enu should ada4t to the new situation and the Te1t#rea should be 7one. Ne1t start another new te1t 0ile) now close it and a7ain see the 6enu chan7e accordin7ly. One 6ore thin7) 6ake sure that 6enu +ile V O4en) +ile V *lose and +ile V auit all 7et an icon and +ile V auit 7ets a keyboard shortcut (*trl Z a).

Illustration 1 .: 2he end result of some polishing of the 5ile menu

131

11.

+our fifth app#ication$ Simp#e%e&t)ditor

11.. Back on track for +a1e 0"


Now all that is le0t 0or codin7 in the +ile 6enu is Sa2e #s... $ou ha2e arran7ed that the Sa2e #s 6enu ite6 will only be seen when an e1istin7 4roEect is o4ened or a new one has been sa2ed.

11$1$1 Code a<e As menu item


What needs to ha44en when Sa2e #s is launched is actually about the sa6e as when a new docu6ent 7ets sa2ed. The user will 7et a dialo7 to deter6ine 4ath and new 0ile na6e. $ou need to sa2e to this user deter6ines 4ath and 0ile na6e. The code is as 0ollows3 (ublic Sub 6nu+ileSa2e#sM*lick() &ialo7.Title N #44lication.Title J (H - Sa2e #s te1t 0ileH) &ialo7.(ath N User. o6e !0 &ialo7.Sa2e+ile() Then Return +ile.Sa2e(&ialo7.(ath) t1a"ain.Te1t) bs(ath+ileLoaded N &ialo7.(ath bbLoaded N True "essa7e.!n0o(H+ile H J +ile.Na6e(&ialo7.(ath) J H sa2ed.H) End Now run the a44lication) o4en a te1t 0ile and sa2e it with another na6e.

Illustration 140: (ave !s dialog

132

11.

+our fifth app#ication$ Simp#e%e&t)ditor

$ou will see that this works as e14ected.

Illustration 141: 'otification that the file "as saved using a ne" name

$ou 6i7ht also ha2e noticed that the code 0or Sa2e #s is) e1ce4t 0or the dialo7 0or6 ca4tion) e1actly the sa6e. So there is so6e 6ore work to be done

11$1$% implif:ing t9e code fo8 sa<ing


This co66on code 6akes 0or a 4er0ect e1a64le 0or a routine that can be called 0or both situation) si64li0yin7 your code) lessenin7 the 6ount o0 code and 6akin7 0or better 6aintainable code as you only need to 6odi0y code at one 4lace instead o0 two. ere is what you the chan7ed Sa2e and Sa2e #s e2ent will look like3 (ublic Sub 6nu+ileSa2eM*lick() !0 bbLoaded Then DJust sa2e the 0ile +ile.Sa2e(bs(ath+ileLoaded) t1a"ain.Te1t) "essa7e.!n0o(H+ile H J +ile.Na6e(bs(ath+ileLoaded) J H sa2ed.H) Else Sa2e&ialo7(OSa2eP) Endi0 End (ublic Sub 6nu+ileSa2e#sM*lick() Sa2e&ialo7(OSa2e #sP) End

$ouDll notice they are a lot shorter and 4ass an ar7u6ent 0or the &ialo7 0or6 ca4tion. (bein7 DSa2eD and DSa2e #sD). This how the Sa2e&ialo7() routine looks lik (it is 4ri2ate because you only use it in the current +r6"ain.class)3

133

11.

+our fifth app#ication$ Simp#e%e&t)ditor

(ri2ate Sub Sa2e&ialo7(&ialo7*a4tion #s Strin7) &ialo7.Title N #44lication.Title J (H - H J &ialo7*a4tion) &ialo7.(ath N User. o6e !0 &ialo7.Sa2e+ile() Then Return +ile.Sa2e(&ialo7.(ath) t1a"ain.Te1t) bs(ath+ileLoaded N &ialo7.(ath bbLoaded N True "essa7e.!n0o(H+ile H J +ile.Na6e(&ialo7.(ath) J H sa2ed.H) End &ialo7*a4tion is the 2ariable used to 4ass the ar7u6ent. So callin7 Sa2e&ialo7(OSa2eP) will 4ass the strin7 OSa2eP to 2ariable &ialo7*a4tion and use its 2alue where2er &ialo7*a4tion is used in the routine.

Illustration 142: 2he auto/help "ill #no" )our ne" routine

#s you see) 0or a 2ariable used as an ar7u6ent in a 6ethod (or 0unction) ! use no 4re0i1 to the 2ariable na6e) but a na6e that describes what ar7u6ent will be 0or. !tDs ty4e ! will know when usin7 the routine in code as the auto-hel4 will tell 6e (see illustration abo2e). Run the a44lication to see it all at work as it should. $ou should ha2e one workin7 2ery si64le Te1tEditor that you can actually use in real li0e situation. 'ut there is still roo6 0or i64ro2e6ent.

11.2 Putting t#e dot" on t#e i


There is o0 course always roo6 0or i64ro2e6ent. !64ro2in7 an a44lication can take 4lace on 6any 0ront like its 0unctionality) its user 0riendliness) its looks) its bu7s 0i1ed Eust to na6e a 0ew. $ou are 7oin7 to 0ocus on one i64ro2e6ent in 0unctionality. !t is 6ore a lack o0 0unctionality that needs to be i64le6ented and that is rather disturbin7. $ou 6i7ht ha2e noticed that) when editin7 a 0ile and ne1t closin7 it the a44lication will NOT warn you about the 0ile bein7 chan7ed and o00erin7 you an o4tion to sa2e it. #nother thin7 that will co6e around the corner when i64le6entin7 this 0unctionality is the 1 on the 6ain 0or6. Users can use this to close the window (and thus the a44lication) circu62entin7 all your code to catch the D0ile is edited and thus Kuestion 0or sa2in7D code u4on closin7.

134

11.

+our fifth app#ication$ Simp#e%e&t)ditor

#nother thin7 is that our 0or6 is rather s6all and when o4enin7 a lar7e te1t 0ile and ne1t dra77in7 the 0or6 bi77er the Te1t#rea will re6ain s6all. So6e work there as well. #nd last but not least) wouldnDt it be nice to ha2e a toolbar with o4en) sa2e and other buttons? So a 0ew 6ore thin7 to do be0ore the a44lication suits the needs.

11$3$1 Offe8 to sa<e w9en closed afte8 editing te?t file


The 0irst thin7 you need to do is deter6ine whether a 0ile is chan7ed or not. +or this another 4ri2ate 2ariable is introduced bb*han7ed (a 'oolean). The control you need to check 0or chan7es is t1a"ain) so that is where bb*han7ed needs to be set to true. T1a"ain has a *han7e e2ent and that is the one you need to code. Not 2ery 6uch code needed to do the Eob3 (ri2ate bb*han7ed #s 'oolean N +alse (ublic Sub t1a"ainM*han7e() bb*han7ed N True End Ne1t thin7 to do is to take care that when closin7 the a44lication our chan7e condition is checked and when chan7ed a 4ossibility to sa2e is o00ered.. This needs to be done at both 6enu ite6 *lose and auit. (ublic Sub 6nu+ile*loseM*lick() !0 bb*han7ed Then Do00er o4tion to sa2e Else t1a"ain.*lear D` will tri77er a chan7e e2ent and set bb*han7ed to true t1a"ain./isible N +alse bbLoaded N +alse bb*han7ed N +alse D` set to 0alse (see co66ent abo2e) bs(ath+ileLoaded N Null Set+ile"enu Endi0 End (ublic Sub 6nu+ileauitM*lick() !0 bb*han7ed Then Do00er o4tion to sa2e Else "e.*lose Endi0 End *ode 0or o00erin7 to sa2e is not yet there but you 6i7ht notice that it will 4robably be e1actly the sa6e 0or both *lose and auit e2ent. Now when o00erin7 the user to sa2e the 0ile you need to 7i2e so6e o4tions like D$esD) DNoD and D*ancelD and act accordin7ly.

135

11.

+our fifth app#ication$ Simp#e%e&t)ditor

Now on se2eral occasions "essa7e has been used in the e1a64le a44lications and one o0 its 2ariants "essa7e.!n0o (in this e1a64le a44lication). Well) there is 6ore you can do with "essa7e so ti6e to ha2e a closer look. $ou will be usin7 "essa7e.Warnin7 as that 6akes sense) you want to war users o0 unsa2ed work. +essage This class is used 0or dis4layin7 6essa7e bo1es #ll "essa7e 0unctions can ha2e u4 to three buttons3 - The 0irst button is always the de0ault button - The last button is always the cancel button "essa7e bo1es are 6odal) 6eanin7 the 4ro7ra6 is 4aused until one button is clicked. When a 6essa7e bo1 is closed the inde1 o0 the clicked button is returned. +essage$Wa8ning Static +unction Warnin7("essa7e #s Strin7 S) 'utton: #s Strin7) 'utton; #s Strin7) 'utton 8 #s Strin7) #s !nte7er &is4lays a warnin7 6essa7e with u4 to three buttons. The inde1 o0 the button clicked by the user is returned "ore on "essa7e.Warnin7 see3 htt43557a6basdoc.or75hel45co6457b.Kt<56essa7e5warnin7?28 "ore on "essa7e see3 htt43557a6basdoc.or75hel45co6457b.Kt<56essa7e?28 #s indicated abo2e when showin7 the "essa7e.Warnin7 (with three buttons) the a44lication will 4ause until user has 6ade his choise. When clickin7 DyesD you need to sa2e be0ore *lose or auit When clickin7 DNoD you *lose or auit without sa2in7 When clickin7 D*ancelD the *lose or auit has to be aborted. Lets try to code this in a new 4ri2ate 0unction 0or this3 (ri2ate +unction Warnin7!s*ancelled() #s 'oolean &i6 i#nswer #s !nte7er i#nswer N "essa7e.auestion(H+ile has been chan7edXGn&o you want to sa2e the 4roEect?H) H$esH) HNoH) H*ancelH) Select i#nswer *ase : 6nu+ileSa2eM*lick DW-- $es3 Let the code at the +ileSa2e 6enu handle the sa2in7 Return +alse *ase ; Return +alse DW-- No3 So no sa2in7 *ase Else Return True DW-- *ancel3 So cancelled End Select End Why a 0unction? Well a 0unction can do all a 6ethod can do) but it can also return so6ethin7. !n this case it returns True i0 button *ancel was clicked in "essa7e.Warnin7. That is why the 0unction is de-

136

11.

+our fifth app#ication$ Simp#e%e&t)ditor

clared #s 'oolean. The 2alue a0ter the word DReturnD has to be a 'oolean as this is the 2alue returned when the 0unction is called. Since you will call this 0unction 0ro6 *lose and auit e2ent it would be nice to know i0 D*ancelD was clicked. So when callin7 Warnin7!s*ancelled() it will return either True or +alse. When True) the warnin7 was cancelled so the *losin7 or auittin7 needs to be cancelled as well. ence the na6e o0 the 0unction Warnin7!s*ancelled) a na6e that 7i2es 6eanin7 to what the 0unction actually does. Now lets ha2e a look at the chan7ed code 0or *lose and auit e2ent3 (ublic Sub 6nu+ile*loseM*lick() !0 bb*han7ed Then !0 Not Warnin7!s*ancelled() Then t1a"ain.*lear D` "essa7e.Warnin7 recei2ed a yes or no t1a"ain./isible N +alse bbLoaded N +alse bb*han7ed N +alse bs(ath+ileLoaded N Null Set+ile"enu Endi0 Else t1a"ain.*lear t1a"ain./isible N +alse bbLoaded N +alse bb*han7ed N +alse bs(ath+ileLoaded N Null Set+ile"enu Endi0 End (ublic Sub 6nu+ileauitM*lick() !0 bb*han7ed Then !0 Not Warnin7!s*ancelled() Then "e.*lose Endi0 Else "e.*lose Endi0 End Ti6e to run the a44lication to see the e00ect o0 all the code. +irst o4en a 0ile) edit it and click &ile ; Close $ou should 7et a warnin7 tellin7 you that your 0ile has chan7ed and o00erin7 you the o4tion to sa2e it.

137

11.

+our fifth app#ication$ Simp#e%e&t)ditor

Illustration 14 : ,losing after editing gives a "arning and option to save

Now click Cancel to see what ha44ens. $ou should 7o back to the 0ile without closin7. Now click &ile ; Close a7ain and ne1t click )o. The a44lication will close the 0ile. Now o4en the sa6e 0ile and see i0 the chan7es where sa2ed. They shouldnDt be as you clicked DNoD. Now 6ake so6e chan7es and click &ile ; Close a7ain and select 'es. The 0ile should close and u4on o4enin7 the chan7es should be in the newly o4ened 0ile. Try the sa6e 0or &ile ; Fuit. !0 all that is done) one Kuestion3 &id you disco2er the bu7 in your a44lication?...

11$3$% BugPP W9at bugHP


When you intend so6ethin7 to work a certain 6anner to 4ro2ide certain 0unctionality and it doesnDt do that Eob under all circu6stances) then you can call that a bu7. Try o4enin7 a te1t 0ile) do NOT chan7e the 0ile and click &ile ; Close. What ha44ens? &o you 7et a warnin7 that your 0ile has been chan7ed when it isnDt? So6ethin7 does see6 to tri77er the *han7e e2ent o0 t1a"ain. +irst lets add a (rint state6ent to the *han7e e2ent so you can check in console when it is tri77ered. (ublic Sub t1a"ainM*han7e() bb*han7ed N True (rint O*han7edP End Now run the a44lication kee4 your eye on the console. When launchin7 the a44lication nothin7 ha44ens in the console. When clickin7 &ile ; Open nothin7 ha44ens in the console. When selectin7 a 0ile and clickin7 OD V console shows D*han7edD

138

11.

+our fifth app#ication$ Simp#e%e&t)ditor

Illustration 144: ,hanged sho"n in ,onsole means ,hange event is triggered

This actually 6akes sense as when loadin7 a 0ile to t1a"ain 6eans a chan7e 0or6 the 4oint o0 2iew o0 t1a"ain. ere is the 4iece o0 code run be0ore *han7e e2ent is tri77ered. (ublic Sub 6nu+ileO4enM*lick() &ialo7.Title N #44lication.Title J (H - O4en te1t 0ileH) &ialo7.(ath N User. o6e !0 &ialo7.O4en+ile() Then Return t1a"ain.Te1t N +ile.Load(&ialo7.(ath) D` tri77ers the chan7e e2ent #0ter t1a"ain.Te1t N +ile.Load(&ialo7.(ath)) instead o0 runnin7 the ne1t line o0 code) the chan7e e2ent 0or t1a"ain is tri77ered. So the %a6bas !nter4reter will 0irst e1ecute the t1a"ainM*han7e() e2ent be0ore returnin7 to the line o0 code a0ter loadin7 the 0ile. That is where you need to take care o0 business as chan7e is tri77ered u4on o4enin7 and bb*han7ed is set to true. #ddin7 0ollowin7 line should sol2e the 4roble6. (ublic Sub 6nu+ileO4enM*lick() &ialo7.Title N #44lication.Title J (H - O4en te1t 0ileH) &ialo7.(ath N User. o6e !0 &ialo7.O4en+ile() Then Return t1a"ain.Te1t N +ile.Load(&ialo7.(ath) bb*han7ed N +alse D` #dd this line Now try runnin7 the a44lication a7ain. &o NOT chan7e anythin7 to the 0ile and click 6enu &ile ; Close. !t should work 4ro4erly now. So one bu7 down) u4 to the ne1t one. O4en a 0ile) chan7e a 0ew thin7s and ne1t click on the 1 to close the window (and thus the a44lication). Ne1t o4en the a44lication a7ain and o4en the 0ile. &id the chan7es sa2e? ! bet you they didnDt) so there you ha2e a second bu7. To catch the closin7 o0 a 0or6 by the user usin7 1 is done in the +or6M*lose() e2ent. +or this you will code the +or6M*lose e2ent and ne1t cut and 4ast the code o0 the 6nu+ileauitM*lick e2ent to the +or6M*lose e2ent. Ne1t call the +or6M*lose e2ent 0ro6 6nu+ileauitM*lick e2ent.

139

11.

+our fifth app#ication$ Simp#e%e&t)ditor

(ublic Sub +or6M*lose() 0 bb*han7ed Then !0 Not Warnin7!s*ancelled() Then "e.*lose Endi0 Else "e.*lose Endi0 End (ublic Sub 6nu+ileauitM*lick() +or6M*lose End Now run the a44lication) o4en a 0ile) chan7e so6ethin7 and click 1. $ou will 7et the warnin7. *lick 'es and 0ile will sa2e and a44lication closes. O4en the sa6e 0ile and see i0 the chan7es sa2ed. Now chan7e a7ain and click 1. Select Can7 cel and you will see that the a44lication) instead o0 cancellin7 will close. To sol2e that 4roble6 you will need one 6ore line o0 code3 (ublic Sub +or6M*lose() 0 bb*han7ed Then !0 Not Warnin7!s*ancelled() Then "e.*lose Else D` add this Sto4 E2ent D` and this Endi0 Else "e.*lose Endi0 End

TO, "I")T STO( E/ENT This state6ent 6ust be in an e2ent handler. !t tells the inter4reter that the e2ent that called the e2ent handler 6ust be cancelled. "ore on STO( E/ENT see3 htt43557a6basdoc.or75hel45lan75sto4e2ent?28 "ore on E2ent "ana7e6ent see3 htt43557a6basdoc.or75hel45cat5e2ent?28 Now run the a44lication a7ain) o4en and chan7e a 0ile) click 1 and ne1t *ancel and the a44lication should stay o4ened. !n short) second bu7 down.

140

11.

+our fifth app#ication$ Simp#e%e&t)ditor

11$3$3 !esiKing t9e application window


*heck i0 you ha2e 7ot a Read"e 0ile or so6e other te1t 0ile with a 7ood Kuantity o0 te1t and o4en it usin7 Si64leTe1tEditor. $ou will notice that with a lot o0 te1t a bi77er siIe window would be better. So 7o to the border o0 the Si64leTe1tEditor window and dra7 it bi77er.

Illustration 14$: Dragging the "indo" bigger doesn't change the 2e3t!rea

$ou will notice the window will resiIe) but the Te1t#rea will not. To sol2e this 4roble6 7o to the +r6"ain 4ro4erties in the %a6bas !&E. Set the +r6"ain 4ro4erty A88agement to Ho8iKontal

Illustration 14&: (et the !rrangement propert) of 5rm1ain

Now run the a44lication) o4en you bi7 te1t 0ile and dra7 the 6ain window bi77er.

141

11.

+our fifth app#ication$ Simp#e%e&t)ditor

Illustration 14*: 2he 2e3t!rea no" does resi@e "ith the "indo"%%%

Te1t#rea does resiIe when dra77in7 the window bi77er) but not how you would like it. $ou should try settin7 4ro4erty A88angement to Ie8tical as well and 7i2e it a 7o. The one you need to 6ake it work 4ro4erly is &ill. So 6ake sure to end there.

Illustration 14+: !rrangement set to 5ill does the job

142

11.

+our fifth app#ication$ Simp#e%e&t)ditor

11$3$( Adding a ToolBa8


To add a Tool'ar you will need to 7o back to the desi7n o0 your 0or6 and 6ake so6e roo6 below your 6enu and abo2e your Te1t#rea. Ne1t add a bo1 abo2e the Te1t#rea. HBo?/ Na6e V hb1Tool'ar

Illustration 14.: :6o3 is found on the 2ab ,ontainer

Now select the bo1 and add a Tool'utton3 ToolButton/ Na6e V tbnNewY Width V ;;Y ei7ht V ;;Y (ictureV select the New icon 0ro6 stock ;;. $ou result should look like the illustration below. *heck the hierarchy to see i0 tbnNew is actually 4laced inside the hb1Tool'ar (2ery i64ortant).

Illustration 1$0: !ll ne" controls in place

Ne1t double click tbnNew and code its *lick e2ent3 (ublic Sub tbnNewM*lick() 6nu+ileNewM*lick End Run the a44lication and see what ha44ens when clickin7 New in the Toolbar. &ra7 the window bi77er and see what ha44ens there. #lthou7h the New button does 4roduce a new e64ty te1t 0ile) the 0or6s looks a bit weird.

143

11.

+our fifth app#ication$ Simp#e%e&t)ditor

Illustration 1$1: ! strange loo#ing form; but 'e" button "or#s%%

See6s you need to 0i1 the #rran7e6ent o0 the +r6"ain. %i2e it a 7o and try the di00erent o4tions. Now that you 4robably 0i7ured out that none will work) set the +r6"ain 4ro4erty A88ange7 ment to Ie8tical. Ne1t set the t1a"ain 4ro4erty "?pand to T8ue.

Illustration 1$2: (et t3a1ain propert) E3pand to 2rue

Now 7i2e it another run. $ou should be able to 6ake a new te1t 0ile) dra7 the window bi77er and all should look Eust 0ine. Now add a 0ew 6ore Tool'uttons to hb1Tool'ar. ToolButton/ Na6e V tbnO4enY Width V ;;Y ei7ht V ;;Y (ictureV select the O4en icon 0ro6 stock ;;. ToolButton/ Na6e V tbnSa2eY Width V ;;Y ei7ht V ;;Y (ictureV select the Sa2e icon

144

11.

+our fifth app#ication$ Simp#e%e&t)ditor

0ro6 stock ;;. ToolButton/ Na6e V tbnSa2e#sY Width V ;;Y ei7ht V ;;Y (ictureV select the Sa2e#s icon 0ro6 stock ;;. ToolButton/ Na6e V tbn*loseY Width V ;;Y ei7ht V ;;Y (ictureV select the *lose icon 0ro6 stock ;;. The result should look like illustration below. "ake sure the buttons are inside the bo1.

Illustration 1$ : 1a#e sure all 2ool6uttons are inside the :6o3

Ne1t code all the new Tool'uttons. Just let the6 run the click e2ent o0 the accordin7 6enu ite6. (ublic Sub tbnO4enM*lick() 6nu+ileO4enM*lick End (ublic Sub tbnSa2eM*lick() 6nu+ileSa2eM*lick End (ublic Sub tbnSa2e#sM*lick() 6nu+ileSa2e#sM*lick End (ublic Sub tbn*loseM*lick() 6nu+ile*loseM*lick End Ne1t take care that only those Tool'uttons are enabled that need to be enabled) Eust like you did with the 6enus. +or that you will need to 7o to the two 4ri2ate routines Set+ile"enu and Set+ile"enuNew. They will need to look like this3

145

11. (ri2ate Sub Set+ile"enu() !0 bbLoaded Then 6nu+ileSa2e.Enabled N True 6nu+ileSa2e#s.Enabled N True 6nu+ile*lose.Enabled N True tbnSa2e.Enabled N True tbnSa2e#s.Enabled N True tbn*lose.Enabled N True Else 6nu+ileSa2e.Enabled N +alse 6nu+ileSa2e#s.Enabled N +alse 6nu+ile*lose.Enabled N +alse tbnSa2e.Enabled N +alse tbnSa2e#s.Enabled N +alse tbn*lose.Enabled N +alse Endi0 End (ri2ate Sub Set+ile"enuNew() 6nu+ileSa2e.Enabled N True 6nu+ile*lose.Enabled N True tbnSa2e.Enabled N True tbn*lose.Enabled N True End

+our fifth app#ication$ Simp#e%e&t)ditor

D` added D` added D` added

D` added D` added D` added

D` added D` added

$ou will notice how easy it is to 6ake all this work because o0 a well set u4 code. Only a 0ew thin7s need to be done to 6ake all work. Ti6e 0or testin7 the a44lication. Run and try your new buttons) see i0 they are enabled at the 4ro4er ti6es) i0 they work. "ake a new 0ile with the New button) ty4e so6e te1t and sa2e it with the Sa2e button. *lose it with the *lose button and reo4en with the O4en button. &oes all work as e14ected? O4en a 0ile and chan7e it) try to close it with the button. &o you 7et a warnin7 that 0ile has been chan7ed? #ll should work as e14ected and like the 6enus worked.

Illustration 1$4: ! running resi@eable (impe2e3tEditor "ith 2oolbar

146

11.

+our fifth app#ication$ Simp#e%e&t)ditor

#ll that is le0t doin7 is 6ake the Tool'ar look a bit better. Si64ly set the 4ro4erty pacing o0 the hb1Tool'ar to T8ue

Illustration 1$$: 2oolbar "ith propert) (pacing set to 5alse

Illustration 1$&: 2oolbar "ith propert) (pacing set to 2rue

That wasnDt to hard.

11$3$* !eo8de8ing menu items


One 6ore thin7 be0ore this cha4ter co6es to an end. !n 6ost a44lications you will 0ind the 6enu ite6 *lose Eust abo2e Sa2e. $ou ha2e it below Sa2e#s. Lets 6ake a chan7e. O4en the "enu Editor. Select the 6enu ite6 Close and click the +o<e Up button twice.

Illustration 1$*: ! clic# on 1ove Ap button brings the item higher

147

11.

+our fifth app#ication$ Simp#e%e&t)ditor

11$3$- Adding new menus


$ou can also easily add a new 6enu. #nd that is your ne1t 6ission. #dd these 6enu ite6s to the botto6 o0 the current ones +enu(/ Na6e V 6nu/iewY *a4tion V /iew +enu3/ Na6e V 6nu/iewTool'arY *a4tion V ToolbarY *hecked V TrueY*lick button Indent in Toolbar +enu%/ Na6e V 6nu el4Y *a4tion V el4 +enu1/ Na6e V 6nu el4#boutY *a4tion V #bout... *lick button Indent in Toolbar

Illustration 1$+: !dded menus "ith 2ool6ar menu item set chec#ed

Run the a44lication to see your new 6enus. this one has a checkbo1.

a2e a s4ecial look at 6enu Iiew ; Toolba8 as

Illustration 1$.: ! ne" menu Bie" 2oolbar

148

11.

+our fifth app#ication$ Simp#e%e&t)ditor

Now when the 6enu ite6 Toolbar is checked you want the Tool'ar to be 2isible. When unchecked it needs to be in2isible. +or the #bout 6enu ite6 in 6enu el4 you will code a si64le "essa7e. (ublic Sub 6nu/iewTool'arM*lick() !0 6nu/iewTool'ar.*hecked Then 6nu/iewTool'ar.*hecked N +alse hb1Tool'ar./isible N +alse Else 6nu/iewTool'ar.*hecked N True hb1Tool'ar./isible N True Endi0 End (ublic Sub 6nu el4#boutM*lick() "essa7e.!n0o(#44lication.Na6e J H H J #44lication./ersion J HGn#n e1a64le a44lication by W. RaetsGnGn%NU %eneral (ublic License 2ersion 8H) End +or the 6nu/iewTool'ar you check its checked status and chan7e it. *licked when the Toolbar ite6 was checked will uncheck it and 2ice 2ersa. 'esides that it will 6ake the Tool'ar hb1Tool'ar 2isible or in2isible accordin7ly. The 6nu el4#bout code is Eust a si64le "essa7e.!n0o. a2e a look at your result.

Illustration 1&0: Cith 2ool6ar chec#ed

O4en a te1t 0ile and click 6enu /iew and see the Toolbar ite6 checked. Now click the 6enuite6 Toolba8 and see the Toolbar disa44ear (see illustration on ne1t 4a7e).

149

11.

+our fifth app#ication$ Simp#e%e&t)ditor

Illustration 1&1: Cith 2oolbar unchec#ed

*lick 6enu Help ; About$$$ to see your #bout screen.

Illustration 1&2: 1enu :elp /7 !bout sho"s some info on the application

11$3$1 Application caption


The last Ddot to 4ut on the !D is the a44lication ca4tion. &e0ault it shows the a44lications na6e) but it could be used di00erenty. When a te1t 0ile is o4en it could show the a44lication na6e and the na6e o0 the te1t 0ile. When a new te1t docu6ent is 6ade it could show a44lication na6e and Dnew te1t docu6entD. #s the ca4tion has to be set accordin7 to the current state o0 the te1t loaded into t1a"ain) it is 7oin7 to be needed at 6ore than one 4lace in our +r6"ain.class. # 4ri2ate routine would be the 4ro4er road to take. 'asically there are two situation3

150

11.

+our fifth app#ication$ Simp#e%e&t)ditor

:. No docu6ent loaded V no 0ile na6e ;. &ocu6ent loaded V 0ile na6e known or 0ile na6e is Dnew te1t docu6entD !0 you write a 4ri2ate 6ethod that sets the ca4tion these 0ile na6es need to 7et 4assed to the 4ri2ate 6ethod. +or that you use an ar7u6ent) but since there is not always a 0ile na6e to 4ass you need to 6ake the ar7u6ent O4tional) 6eanin7 you can 4ro2ide the ar7u6ent when callin7 the 6ethod but you donDt need to. 'ack to the code editor in !&E to write the routine3 (ri2ate Sub Set#44*a4tion(O4tional #44*a4tion #s Strin7) !0 !sNull(#44*a4tion) Then "e.*a4tion N #44lication.Na6e Else "e.*a4tion N #44lication.Na6e J O - O J #44*a4tion Endi0 End 'ecause #44*a4tion is an o4tional ar7u6ent) 6eanin7 it can be e64ty) you need to check what cause o0 action to take when e64ty (!sNull). Ne1t ste4 is to check at what 4laces the 6ethod Set#44*a4tion needs to be called. +irst 4lace is when a new docu6ent is 6ade. The ca4tion will need to show DNew te1t docu6entD alon7side the a44lication na6e. (ublic Sub 6nu+ileNewM*lick() t1a"ain.*lear t1a"ain./isible N True Set#44*a4tion(ONew te1t docu6entP) End Now run the a44lication and click button )ew in the Toolbar.

Illustration 1& : ''e" te3t document' added to application caption

151

11.

+our fifth app#ication$ Simp#e%e&t)ditor

Second 4lace to check is when o4enin7 a te1t 0ile. (ublic Sub 6nu+ileO4en*lick() ... t1a"ain.Te1t N +ile.Load(&ialo7.(ath) D` this is where the 0ile 7ets loaded Set#44*a4tion(+ile.Na6e(&ialo7.(ath)) D` this is where you set the ca4tion ... End #7ain run and o4en a te1t 0ile and see the 0ile na6e a44ear in the ca4tion.

Illustration 1&4: ,aption sho"ing the name of the opened te3t file

Third 4lace to check is when a new docu6ent is sa2ed (so it has a na6e) and when an e1istin7 docu6ent is sa2ed as (so it will 7et another na6e). This all ha44ens in 6ethod Sa2e&ialo7. So that is where you need to add the code3 (ublic Sub Sa2e&ialo7() ... +ile.Sa2e(&ialo7.(ath) t1a"ain.Te1t) D` this is where the 0ile 7ets sa2ed Set#44*a4tion(+ile.Na6e(&ialo7.(ath)) D` this is where you set the ca4tion ... End #nd last but not least) the ca4tion should be set when o4enin7 the 0or63 (ublic Sub +or6MO4en() "e.*enter Set#44*a4tion() ... D` this is where you set the ca4tion (no na6e known)

152

11.

+our fifth app#ication$ Simp#e%e&t)ditor

Run your a44lication once 6ore and try so6e di00erent thin7s) like startin7 a new te1t docu6ent and sa2in7 it) ne1t sa2e as. !0 you see the ca4tion chan7in7 the a44lication is 0inished 0or now.

Illustration 1&$: (ave the ne" te3t document

Illustration 1&&: !fter saving the caption changed

11.< 0 la"t note on code organi"ation


Since no a44lication is really 0ree o0 6aintenance) you should or7anise your code in such 6anners that) in a later sta7e (this can be 6onths and other a44lications later)) you understand what you ha2e been doin7 in the 4ast. *o66ent can contribute) as can orderin7 your code. On the ne1t illustration a screenshot o0 the code o0 this 4roEect in 6y !&E. $ou will see that ! 4laced all 6enu code to7ether) all toolbar code to7ether) all 4ri2ate routines to7ether. #ll these 7rou4s ha2e a co66ent indicatin7 where they start.

153

11.

+our fifth app#ication$ Simp#e%e&t)ditor

Illustration 1&*: 5rm1ain%class code organised in certain order

Not 2isible on the screenshot is that at certain 4oint in the code ! add co66ent to e14lain to 6ysel0 (in a later sta7e) what ! did there. "i7ht sound stran7e) but belie2e 6e) ! a6 2ery ha44y that ! 7i2e 6ysel0 these 4resents. This concludes this cha4ter. !n the ne1t cha4ter youDre 7oin7 to continue to work with Si64leTe1tEditor.

154

12.

)nd note for pre1re#ease 1

1% "nd note fo8 p8e78elease 1


This work is not 0inished yet. "ore cha4ters will be added as soon as 4ossible and current ones 6i7ht be 0urther re2ised. (lanned 0or the ne1t cha4ter (actually this cha4ter :;) is 4rintin7. #0ter that you will be di2in7 into so6e 6ore co64le1 situations when it co6es to handlin7 data and sa2in7 it) you will learn so6e new 6ore co64le1 controls and ha2e a closer look at what is 4ossible with 0or6 classes. This Eourney will take se2eral cha4ters. #nd what will be ne1t !D6 not sure about) a closer look at the !&E will be 4art o0 it 0ir sure. E14ect at least > or B 6ore cha4ters added to the 0inal release. "aybe in between another 4rerelase will beco6e a2ailable) a bit de4endin7 on how lon7 thin7s take to write. "eanwhile ! ho4e you enEoyed the 0irst :: cha4ters and workin7 with %a6bas. "ake sure to install the a44lication %a6basLearnin7 (see #44endi1 /!) ) 0or u4dated in0or6ation on this and other D ow To %a6basD-7uides and 6ore to hel4 the be7innin7 %a6bas de2elo4er. To be continued... (.S. If :ou encounte8 an: i88egula8ities in t9is 8elease please 8epo8t t9em bac@ 4see Ap7 pendi? I5 and 9elp imp8o<e t9e guide$

155

13 Appendices
Appendi? I/ .inu? command 8efe8ence
!0 you are new to Linu1) here are so6e co66ands that could be use0ul when installin7 %a6bas 0ro6 the o00icial website or 0ro6 S/N.

&ile commands
ls ls 7al cd dir cd cd J m@di8 dir 8m file 8m 78 dir 8m 7f file 8m 7f8 dir lists the current directory lists the current directory (hidden 0iles included) chan7e the directory dir (e1a64le3 cd 5usr5bin) chan7e to ho6e directory chan7e directory to root directory 6ake directory dir (e1a64le 6kdir %a6bas8) re6o2es 0ile file (e1a64le r6 6yin0o.t1t) re6o2es directory dir (e1a64le r6 %a6bas8) 0orced re6o2e 0ile file (e1a64le r6 -0 6yin0o.t1t) 0orced re6o2e directory dir (e1a64le r6 -r0 %a6bas8)

:stem info8mation
df du uptime uname 7a f8ee show disk usa7e show directory usa7e show syste6 u4ti6e show kernel in0or6ation show 6e6ory and swa4 in0or6ation

Installation f8om sou8ce


$Jconfigu8e ma@e ma@e install *on0i7ure the 4acka7e *o64ile the 4acka7e !nstall the 4acka7e

Appendi? II/ ubsc8ibe to t9e official Gambas mailing listsJ&o8um


When you are e14eriencin7 4roble6s installin7 %a6bas and you donDt 6ana7e to sol2e the6 you can always re4ort the6 on the %a6bas 6ailin7 list. +or this you will 0irst need to subscribe to the 6ailin7 list. O4en a browser and 7o to the %a6bas website and click on the 6enu D+ailing listsJ&o8umsD

Illustration 1&+: Gambas "ebsite / 1enu 1ailing lists95orums

This will o4en an o2er2iew o0 the a2ailable 6ailin7 lists and a 0oru6.

Illustration 1&.: Gambas "ebsite / Overvie" 1ailing -ists D 5orums

+or re4ortin7 your 4roble6s use the DGambas Use8sD list. The DGambas #e<elope8sP and DGambas ub<e8sion CommitsP lists are 6eant 0or those workin7 on the de2elo46ent o0 %a6bas8 (or those Eust curious)
II

Then there is a S4anish and +rench user list and ne1t the 0oru6 0or those who dislike 6ailin7 lists. O4tion :3 Subscribe to %a6bas Users 6ailin7 list When clickin7 DGambas Use8sD you 7et to the 4a7e to re7ister to the 6ailin7 list.

Illustration 1*0: (ubscribe to the Gambas/user mailinglist

Once re7istered you will recei2e all 4osts on the 6ailin7 list in the inbo1 o0 the 6ail account you si7ned u4 with. $ou will also be able to send 6ails to the list. O4tion ;3 Use the 0oru6 instead o0 the 6ailin7 list !0 you dislike 6ailin7 list there is a second o4tion. *lick D9ttp/JJwww$nabble$comJGambas7 f3(%*$9tmlD to 7o to the 0oru6.

III

Illustration 1*1: Gambas mailing list 5orum

On the 0oru6 you will 0ind all the to4ics and 4osts 0ro6 the 6ailin7 list. To 4ost you will need to re7ister an account.

IV

Appendi? III/ !epo8ting p8oblems on t9e official Gambas mailing list


When re4ortin7 your 4roble6s on the 6ailin7 list 0irst consider a 0ew 7ood 4ractices. :. Search the o00icial %a6bas 6ailin7 list 0oru6 0or an answer be0ore askin7 Kuestions ;. (ro2ide details on your syste6 when re4ortin7 your 4roble6 8. (ro2ide details on your 4roble6 when re4ortin7 your 4roble6 <. 'e clear on what 7oes wron7 and where it 7oes wron7 and the ste4s that took you there =. (ro2ide a source archi2e with a 4roEect that shows the 4roble6 When ha2in7 4roble6s 4ro2ide these details on your syste63 :. ,ernel in0or6ation (use una6e -a in a ter6inal) ;. &istribution na6e and 2ersion (E1a64le3 Linu1 "int :8 "aya) 8. &eskto4 used (E1a64les are3 %no6e) ,&E) L.&E) 10ce) "ate) *inna6on)...) <. %a6bas 2ersion #ll this can be 0ound in the %a6bas !&E 6enu H ; :stem info8mations$$$

Illustration 1*2: Gambas IDE / ()stem information form

Just co4y54aste into your 6ail or attach a te1t 0ile you 4asted the in0or6ation into.

Appendi? II/ W9ite Island oftwa8e and Gambas&o8um Buic@ tou8


On White !sland So0tware and %a6bas+oru6 a (roEect *ollaboration +oru6 DHow to Gam7 bas 3D has been created o4en to the 4ublic;:. ere is a Kuick tour to 7et you started on the 0oru6. To 7o to White !slands So0tware and %a6bas+oru6 o4en a web browser and ty4e htt4355whiteislandso0tware.co65inde1.4h4?4a7eNstart as destination.

Illustration 1* : Chite Island and Gambas5orum <circled the login and join=

Once there lo7 in or Eoin (see illustration abo2e). When lo77ed in you will be brou7ht to the sa6e 4a7e a7ain but now with 6ore details where the lo7in used to be. *lick on &o8um in the to4 6enu (Eust behind o6e). $ou will 7et an o2er2iew o0 all 0oru6s 4resent when scrollin7 down. +oru6s are cate7oriIed into these cate7ories with 0ollowin7 0oru6s 4er cate7ory ;;3 Website *o66unity Website *o66unity %a6bas News %eneral !ntroduce yoursel0 %eneral chat %ra4hic and !6a7es Website co66ent to4ics The %a6bas+oru6 *oder %a6bas !&E #sk auin %a6bas 2ersion 8 %a6bas 2ersions : and ; Ras4berry (i %a6bas based 4roEects "e6berDs Loun7es 7bWilly
$1

$$

<ou can visit it as guest and read all information. To #ost Auestions on the forum you need to ma-e a free account. .ategories and forums as #resent on ?ctober 1+th $'1$.

VI

sholIy konae14ress Natasha&aystar (i7a So0tware %a6bas (roEect hostin7 !64ortant notices (Read-only) The #d6inDs loun7e ow To...5 owdo !... &o6ains

!0 you want to look 0or so6ethin7 or 4ost so6ethin7 6ake sure to 7o to the 4ro4er cate7ory and 0oru6. When you 2isit the %a6bas+oru6 to check what you 6issed since your last 2isit there is an easy o4tion at the to4 le0t o0 your user na6e D,ost since last <isitD. *lick it and you will see what you 6issed out on.

Illustration 1*4: (earch; 4ost since last visit and 2opics "ith unread posts

#nother handy o4tion is the DsearchD button 0or searchin7 the 0oru6 i0 you ha2e so6e 4roble6 with %a6bas. !0 no answer to your 4roble6 can be 0ound Eust start a new to4ic in the 4ro4er cate7ory and 0oru6. DTopics wit9 un8ead postsD can co6e in 4ractical at ti6es as well. With this in0or6ation you ha2e the basics to 7et started on the 0oru6. There is a lot 6ore out there like a so0tware re4ository) 7uides and so on) but that is not what this 7uide is about.

VII

Appendi? I/ Getting 9elp wit9 t9e =How to Gambas 3= guides


On White !sland So0tware and %a6bas+oru6 a %a6bas based 4roEect D ow to %a6bas 8D has been created o4en to the 4ublic;8. !t is set u4 so that 4eo4le ha2in7 trouble with the 7uide can 7o there and ask their Kuestions (or check i0 so6eone else asked the sa6e) or do su77estions on the 6anual. These Kuestions and su77estions will be taken into account on i64ro2in7 the 7uides. So 0eel 0ree do 6ake 7ood use o0 this 4ossibility. +irst browse to the White !sland So0tware and %a6bas +oru6;< and lo7in (i0 you want to 4ost your 4roble6). *lick on D&o8umD in the to46enu and scroll down to the cate7ory D%a6bas based 4roEectsD where you will 0ind the 0oru6 D ow to %a6bas 8D under 7bWilly.

Illustration 1*$: Gambas based projects "ith sub forum ':o" to Gambas

'

!0 you click the 0oru6 you will 0ind se2eral sub 0oru6s and so6e 7eneral to4ics. *urrent sub 0oru6s are3 !nstallin7 %a6bas This contains to4ics concernin7 the 7uide D!nstallin7 %a6basD. (ost your Kuestions and5or su77estions on the 7uide D!nstallin7 %a6basD here. 'uildin7 %U! #44lications This contains to4ics concernin7 the 7uide D'uildin7 %U! #44licationsD. (ost your Kuestions and5or su77estions on the 7uide D'uildin7 %U! #44licationsD here (roEect 6e6ber Section This section is 0or 4roEect contributors only. !0 you want to contribute send a (" on the 0oru6 to 7bWilly. The nu6ber o0 sub 0oru6s will 7row with the nu6ber o0 7uides is the series.

$%

$+

<ou can visit it as guest and read all information. To #ost Auestions on the forum you need to ma-e a free account. in- to website1 htt#1//whiteislandsoftware.com/index.#h#G#ageHstart

VIII

Illustration 1*&: (ub forums and topics of ':o" to Gambas

' forum

Each sub 0oru6 will ha2e its own to4ics. "ake sure to 4ost in the 4ro4er sub 0oru6. So 0or Kuestions on this 6anual D'uildin7 %U! #44licationsD you click Building GUI Applications to see the to4ics 4resent.

Illustration 1**: 2opics in sub forum '6uilding GAI !pplications'

Three to4ics are o0 i64ortance3 Su77estions 0or i64ro2e6ents (roble6s with instructions in the 6anual Other Kuestions The to4ics are kind o0 sel0 e14lainin7) Eust 4ost in the 4ro4er one and it will be 4icked u4.

IX

Appendi? II/ Application Gambas .ea8ning a lea8ning aid fo8 Gambas


%a6bas Learnin7 is an a44lication to 4ro2ide so6e aid to those learnin7 %a6bas.

Illustration 1*+: Gambas -earning / a learning aid for Gambas

The a44lication 4ro2ides you with3 an o2er2iew and download o0 the D ow To %a6bas 8D 7uides (6ost recent 2ersion) %a6bas 8 code sni44ets 0or co66on tasks a direct access to the %a6bas 8 lan7ua7e inde1 a direct access to the %a6bas 8 co64onent inde1 a direct access to the %a6bas 8 error 6essa7es inde1 a direct access to the ow To %a6bas 0oru6 The a44lication recei2es re6ote 6etadata u4dates;= on the nu6ber) 2ersion and state o0 7uides a2ailable. New code sni44ets that are a2ailable are also 4art o0 the 6etadata u4dates o0 %a6bas Learnin7 and will u4date the code sni44ets seen in %a6bas Learnin7. +or 6ore screenshots and download 2isit3 htt4355howto7a6bas.or75

$2

This reAuires a wor-ing internet connection

Appendi? III/ O<e8<iew of t9e =How To Gambas 3= guides


ere is the list o0 7uides 4ublished and 4lanned3 :. !nstallin7 %a6bas +irst 2ersion 4ublished3 No2e6ber :9th) ;9:; ;. 'uildin7 %U! #44lications +irst :: cha4ters 4re-4ublished3 Se4te6ber ;@th) ;9:8

XI

1( Bibliog8ap9: ,8ima8: sou8ces


%a6bas website3 %a6bas 8 &ocu6entation) URL3 htt43557a6basdoc.or75hel45?28 SLast accessed ;A59@5;9:8T. %a6bas website3 Re4ortin7 a 4roble6) bu7 or crash) URL3 htt43557a6basdoc.or75hel45doc5re4ort?2iew SLast accessed :<5:95;9:;T

econda8: sou8ces
Wiki4edia3 %a6bas) URL3 htt4355en.wiki4edia.or75wiki5%a6bas S#ccessed 9@5::5;9:;T Wiki4edia3 * &ataty4es) URL3 htt4s355en.wiki4edia.or75wiki5*MdataMty4es S#ccessed 9;5985;9:8T Wiki4edia3 ObEect-oriented 4ro7ra66in7 URL3 htt4355en.wiki4edia.or75wiki5ObEect-orientedM4ro7ra66in7 S#ccessed ;859@5;9:8T

XII

Вам также может понравиться