- StPendl Oct 29, 2011
This step now gets rid of the radio buttons, which are now obsolete due to the implemented color pick selection.
We only have to remove all the definitions for the radio buttons and consolidate the mouse and radio button event handlers into one.
To consolidate the event handlers we replace:
SUB MouseClick Handle$, PosX, PosY
RBhandle$ =Left$(Handle$,Len(Handle$)-1); "r"; Right$(Handle$,1)#RBhandle$ "set"call SetBandValue RBhandle$
ENDSUBSUB SetBandValue Handle$
' parse handle of calling control
Band =val(Right$(Handle$,1))
ColorName$ =Mid$(Handle$,4,Len(Handle$)-5)' get position of colorFOR i =1to10if Colors$(i,1)= ColorName$ thenexitforif i <4thenif Tolerances$(i,1)= ColorName$ thenexitforNEXT
With the version to parse the graphics box handle:
SUB MouseClick Handle$, PosX, PosY
' parse handle of calling control
Band =val(Right$(Handle$,1))
ColorName$ =Mid$(Handle$,4,Len(Handle$)-4)' get position of colorFOR i =1to10if Colors$(i,1)= ColorName$ thenexitforif i <4thenif Tolerances$(i,1)= ColorName$ thenexitforNEXT
The complete code now looks like this.
'Resistor Calculator.bas'Author: salaion Yahoo! Group'Date: 27.11.10'' List of Colors at http://en.wikipedia.org/wiki/List_of_colors'' 27.11.10 00:18:50 - Initial as posted' 27.11.10 00:21:40 - added array for colors and reduced redundant code for color boxes' 27.11.10 00:34:07 - replaced duplicate command strings by variables' 27.11.10 02:22:16 - consolidated event handler'' always take the time for some sleep or you will make things worse ;-)'' 27.11.10 10:56:24 - removed radio buttonsnomainwinWindowWidth=820:WindowHeight=600UpperLeftX=int((DisplayWidth-WindowWidth)/2)UpperLeftY=int((DisplayHeight-WindowHeight)/2)BackgroundColor$="green"GLOBAL Band1value, Band2value, Band3value, Band4value
dim Colors$(10,2), Tolerances$(3,2)[Colors]DATA"Black"," 0 0 0"DATA"Brown","170 100 20"DATA"Red","255 0 0"DATA"Orange","255 180 0"DATA"Yellow","255 255 0"DATA"Green"," 45 157 40"DATA"Blue"," 0 0 255"DATA"Violet","170 0 180"DATA"Gray","127 127 127"DATA"White","255 255 255"[Tolerances]DATA"Gold","212 175 55"DATA"Silver","200 200 250"DATA"None"," 21 228 255"'Fill the arraysRESTORE[Colors]FOR i =1to10READ ColorName$, ColorValue$
Colors$(i,1)= ColorName$
Colors$(i,2)= ColorValue$
NEXTRESTORE[Tolerances]FOR i =1to3READ ColorName$, ColorValue$
Tolerances$(i,1)= ColorName$
Tolerances$(i,2)= ColorValue$
NEXTSTATICTEXT#w.Ohm,"",500,200,300,20STATICTEXT#w.Tole,"",500,230,300,20STATICTEXT#w,Colors$(1,1),10,42,50,20STATICTEXT#w,Colors$(2,1),10,82,50,20STATICTEXT#w,Colors$(3,1),10,122,50,20STATICTEXT#w,Colors$(4,1),10,162,50,20STATICTEXT#w,Colors$(5,1),10,202,50,20STATICTEXT#w,Colors$(6,1),10,242,50,20STATICTEXT#w,Colors$(7,1),10,282,50,20STATICTEXT#w,Colors$(8,1),10,322,50,20STATICTEXT#w,Colors$(9,1),10,362,50,20STATICTEXT#w,Colors$(10,1),10,402,50,20STATICTEXT#w,Tolerances$(1,1),420,42,50,20STATICTEXT#w,Tolerances$(2,1),420,82,50,20STATICTEXT#w,Tolerances$(3,1),420,122,50,20STATICTEXT#w,"K=Kilo ",470,300,100,20STATICTEXT#w,"M=Mega ",470,330,100,20STATICTEXT#w,"Light Blue is The Default Color of Resistance ",470,360,400,20'Band1STATICTEXT#w,"First Digit",70,8,80,20groupbox#w,"",70,20,70,420graphicbox#w.Black1 ,90,40,30,30graphicbox#w.Brown1 ,90,80,30,30graphicbox#w.Red1 ,90,120,30,30graphicbox#w.Orange1,90,160,30,30graphicbox#w.Yellow1,90,200,30,30graphicbox#w.Green1 ,90,240,30,30graphicbox#w.Blue1 ,90,280,30,30graphicbox#w.Violet1,90,320,30,30graphicbox#w.Gray1 ,90,360,30,30graphicbox#w.White1 ,90,400,30,30'Band2STATICTEXT#w,"Second Digit",150,8,90,20groupbox#w,"",160,20,70,420graphicbox#w.Black2 ,180,40,30,30graphicbox#w.Brown2 ,180,80,30,30graphicbox#w.Red2 ,180,120,30,30graphicbox#w.Orange2,180,160,30,30graphicbox#w.Yellow2,180,200,30,30graphicbox#w.Green2 ,180,240,30,30graphicbox#w.Blue2 ,180,280,30,30graphicbox#w.Violet2,180,320,30,30graphicbox#w.Gray2 ,180,360,30,30graphicbox#w.White2 ,180,400,30,30'Band3STATICTEXT#w,"Multiplier",250,8,80,20groupbox#w,"",250,20,70,420graphicbox#w.Black3 ,270,40,30,30graphicbox#w.Brown3 ,270,80,30,30graphicbox#w.Red3 ,270,120,30,30graphicbox#w.Orange3,270,160,30,30graphicbox#w.Yellow3,270,200,30,30graphicbox#w.Green3 ,270,240,30,30graphicbox#w.Blue3 ,270,280,30,30graphicbox#w.Violet3,270,320,30,30graphicbox#w.Gray3 ,270,360,30,30graphicbox#w.White3 ,270,400,30,30'Band4STATICTEXT#w,"Tolerance",340,8,80,20groupbox#w,"",340,20,70,420graphicbox#w.Gold4 ,360,40,30,30graphicbox#w.Silver4,360,80,30,30graphicbox#w.None4 ,360,120,30,30'Framgraphicbox#w.title,488,53,306,44stylebits#w.title,0,_WS_BORDER,0,0'Selected Bands of Body Resistancegraphicbox#w.Band1,155,470,15,35graphicbox#w.Band2,185,470,15,35graphicbox#w.Band3,215,470,15,35graphicbox#w.Band4,255,470,15,35stylebits#w.Band1,0,_WS_BORDER,0,0stylebits#w.Band2,0,_WS_BORDER,0,0stylebits#w.Band3,0,_WS_BORDER,0,0stylebits#w.Band4,0,_WS_BORDER,0,0'General Body Resistancegraphicbox#w.37,130,470,168,35stylebits#w.37,0,_WS_BORDER,0,0'Terminal Wiresgraphicbox#w.42,80,485,50,5graphicbox#w.43,300,485,50,5open"Resistor Calculator"for window_nf as#w
#w "trapclose [quit]"#w "font Times_New_Roman 13"#w.title "cls;down;color blue;backcolor yellow;size 15;place 0 0;boxfilled 306 44"#w.title "font Times_New_Roman 16 bold;color red"#w.title "place 13 28;\Resistor Color Code of 4 Bands"#w.title "flush"FOR Band =1to3FOR Color =1to10
Handle$ ="#w."; Colors$(Color,1); Band
#Handle$ "cls;down;fill "; Colors$(Color,2); ";flush"#Handle$ "when leftButtonUp MouseClick"NEXTNEXTFOR Tolerance =1to3
Handle$ ="#w."; Tolerances$(Tolerance,1); 4#Handle$ "cls;down;fill "; Tolerances$(Tolerance,2); ";flush"#Handle$ "when leftButtonUp MouseClick"NEXT'Color of Terminal Wires
CommandString$ ="cls; down; fill 127 127 127;flush"#w.42 CommandString$
#w.43 CommandString$
'General Body Resistance Color
CommandString$ ="cls; down; fill 21 228 255;flush"'light blue#w.37 CommandString$
#w.Band1 CommandString$
#w.Band2 CommandString$
#w.Band3 CommandString$
#w.Band4 CommandString$
' apply initial settingscall MouseClick "#w.Black1",0,0call MouseClick "#w.Black2",0,0call MouseClick "#w.Black3",0,0call MouseClick "#w.None4",0,0[none]WAIT[quit]close#w
ENDSUB MouseClick Handle$, PosX, PosY
' parse handle of calling control
Band =val(Right$(Handle$,1))
ColorName$ =Mid$(Handle$,4,Len(Handle$)-4)' get position of colorFOR i =1to10if Colors$(i,1)= ColorName$ thenexitforif i <4thenif Tolerances$(i,1)= ColorName$ thenexitforNEXT' if we had no match exitIf i =11ThenExitSubSelectCase Band
Case1
Band1value =(i-1)*10Case2
Band2value = i-1Case3
Band3value =10 ^ (i-1)Case4
Band4value = i*5if Band4value =15then Band4value =20CaseElseExitSubEndSelectSelectCase Band
Case1,2,3
ColorValue$ = Colors$(i,2)Case4
ColorValue$ = Tolerances$(i,2)CaseElseExitSubEndSelect
GBhandle$ ="#w.Band"; Band
#GBhandle$ "cls;down;fill "; ColorValue$; ";flush"
vv=(Band1value+Band2value)*Band3value
SelectCaseCase vv>=1000000#w.Ohm "Resistance Value=";vv/1000000;" M Ohm"Case vv>=1000#w.Ohm "Resistance Value=";vv/1000;" K Ohm"CaseElse#w.Ohm "Resistance Value=";vv;" Ohm"EndSelectif Band4value>0then#w.Tole "Tolerance Value =";" +/- ";Band4value;"%"ENDSUB
Streamlining your code
Chapter 6 - Remove radio buttons
-This step now gets rid of the radio buttons, which are now obsolete due to the implemented color pick selection.
We only have to remove all the definitions for the radio buttons and consolidate the mouse and radio button event handlers into one.
To consolidate the event handlers we replace:
With the version to parse the graphics box handle:
The complete code now looks like this.
Chapter 1 - Starting the mission
Chapter 2 - Using arrays to reduce redundant code
Chapter 3 - Use variables for duplicate command strings
Chapter 4 - Consolidate event handlers
Chapter 5 - Apply mouse selection
Chapter 6 - Remove radio buttons
Chapter 7 - Use a single graphics box for the resistor display
Chapter 8 - Further reduce the amount of GUI controls (Using hot-spots)
Chapter 9 - Adding some eye-candy
Chapter 10 - Summary