StPendl Oct 29, 2011
=Streamlining your code= ==Chapter 5 - Apply mouse selection== //[[user:StPendl|1319905735]]// Now we are ready to start implementing the initial request to enable color selection by clicking the color boxes instead of the radio buttons. We only have to apply the same naming convention to the graphics boxes used to display the tolerance colors to be able to use a unified mouse event handler. [[code format="lb"]] graphicbox #w.Gold4 , 410, 40,30,30 graphicbox #w.Silver4, 410, 80,30,30 graphicbox #w.None4 , 410,120,30,30 [[code]] Now we assign the mouse event handlers to all the graphics boxes, which is a piece of cake now, since we use loops to apply changes: [[code format="lb"]] FOR Band = 1 to 3 FOR Color = 1 to 10 Handle$ = "#w."; Colors$(Color,1); Band #Handle$ "cls;down;fill "; Colors$(Color,2); ";flush" #Handle$ "when leftButtonUp MouseClick" NEXT NEXT FOR Tolerance = 1 to 3 Handle$ = "#w."; Tolerances$(Tolerance,1); 4 #Handle$ "cls;down;fill "; Tolerances$(Tolerance,2); ";flush" #Handle$ "when leftButtonUp MouseClick" NEXT [[code]] Finally we just need to add the initial display and the mouse event handler. [[code format="lb"]] ' apply initial settings call MouseClick "#w.Black1", 0, 0 call MouseClick "#w.Black2", 0, 0 call MouseClick "#w.Black3", 0, 0 call MouseClick "#w.None4", 0, 0 [none] WAIT [quit] close #w END SUB MouseClick Handle$, PosX, PosY RBhandle$ = Left$(Handle$, Len(Handle$)-1); "r"; Right$(Handle$, 1) #RBhandle$ "set" call SetBandValue RBhandle$ END SUB [[code]] The complete code now looks like this. [[code format="lb"]] '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 ' 27.11.10 02:39:08 - mouse selection applied nomainwin WindowWidth = 850:WindowHeight = 600 UpperLeftX=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 arrays RESTORE [Colors] FOR i = 1 to 10 READ ColorName$, ColorValue$ Colors$(i,1) = ColorName$ Colors$(i,2) = ColorValue$ NEXT RESTORE [Tolerances] FOR i = 1 to 3 READ ColorName$, ColorValue$ Tolerances$(i,1) = ColorName$ Tolerances$(i,2) = ColorValue$ NEXT STATICTEXT #w.Ohm, "" , 500, 200, 300, 20 STATICTEXT #w.Tole,"" , 500, 230, 300, 20 STATICTEXT #w,Colors$( 1,1), 10, 42, 50, 20 STATICTEXT #w,Colors$( 2,1), 10, 82, 50, 20 STATICTEXT #w,Colors$( 3,1), 10, 122, 50, 20 STATICTEXT #w,Colors$( 4,1), 10, 162, 50, 20 STATICTEXT #w,Colors$( 5,1), 10, 202, 50, 20 STATICTEXT #w,Colors$( 6,1), 10, 242, 50, 20 STATICTEXT #w,Colors$( 7,1), 10, 282, 50, 20 STATICTEXT #w,Colors$( 8,1), 10, 322, 50, 20 STATICTEXT #w,Colors$( 9,1), 10, 362, 50, 20 STATICTEXT #w,Colors$(10,1), 10, 402, 50, 20 STATICTEXT #w,Tolerances$(1,1), 470, 42, 50, 20 STATICTEXT #w,Tolerances$(2,1), 470, 82, 50, 20 STATICTEXT #w,Tolerances$(3,1), 470, 122, 50, 20 STATICTEXT #w,"K=Kilo " , 500, 300,100, 20 STATICTEXT #w,"M=Mega " , 500, 330,100, 20 STATICTEXT #w,"Light Blue is The Default Color of Resistance " , 500, 360,400, 20 'Band1 groupbox #w.FirstDigit, "First Digit", 70,10,80,430 radiobutton #w.Blackr1 , "", SetBandValue,[none] ,75, 45,20,20'Black=0 radiobutton #w.Brownr1 , "", SetBandValue,[none] ,75, 85,20,20'Brown=1 radiobutton #w.Redr1 , "", SetBandValue,[none] ,75,125,20,20'Red=2 radiobutton #w.Oranger1 , "", SetBandValue,[none] ,75,165,20,20'Orange=3 radiobutton #w.Yellowr1 , "", SetBandValue,[none] ,75,205,20,20'Yellow=4 radiobutton #w.Greenr1 , "", SetBandValue,[none] ,75,245,20,20'Green=5 radiobutton #w.Bluer1 , "", SetBandValue,[none] ,75,285,20,20'Blue=6 radiobutton #w.Violetr1 , "", SetBandValue,[none] ,75,325,20,20'Violet=7 radiobutton #w.Grayr1 , "", SetBandValue,[none] ,75,365,20,20'Gray=8 radiobutton #w.Whiter1 , "", SetBandValue,[none] ,75,405,20,20'White=9 'Band2 groupbox #w.SecondtDigit, "Second Digit", 180,10,80,430 radiobutton #w.Blackr2 , "", SetBandValue,[none] ,185, 45,20,20'Black=0 radiobutton #w.Brownr2 , "", SetBandValue,[none] ,185, 85,20,20'Brown=1 radiobutton #w.Redr2 , "", SetBandValue,[none] ,185,125,20,20'Red=2 radiobutton #w.Oranger2 , "", SetBandValue,[none] ,185,165,20,20'Orange=3 radiobutton #w.Yellowr2 , "", SetBandValue,[none] ,185,205,20,20'Yellow=4 radiobutton #w.Greenr2 , "", SetBandValue,[none] ,185,245,20,20'Green=5 radiobutton #w.Bluer2 , "", SetBandValue,[none] ,185,285,20,20'Blue=6 radiobutton #w.Violetr2 , "", SetBandValue,[none] ,185,325,20,20'Violet=7 radiobutton #w.Grayr2 , "", SetBandValue,[none] ,185,365,20,20'Gray=8 radiobutton #w.Whiter2 , "", SetBandValue,[none] ,185,405,20,20'White=9 'Band3 groupbox #w.Multiplier, "Multiplier", 280,10,80,430 radiobutton #w.Blackr3 , "", SetBandValue,[none] ,285, 45,20,20'Black=0 radiobutton #w.Brownr3 , "", SetBandValue,[none] ,285, 85,20,20'Brown=1 radiobutton #w.Redr3 , "", SetBandValue,[none] ,285,125,20,20'Red=2 radiobutton #w.Oranger3 , "", SetBandValue,[none] ,285,165,20,20'Orange=3 radiobutton #w.Yellowr3 , "", SetBandValue,[none] ,285,205,20,20'Yellow=4 radiobutton #w.Greenr3 , "", SetBandValue,[none] ,285,245,20,20'Green=5 radiobutton #w.Bluer3 , "", SetBandValue,[none] ,285,285,20,20'Blue=6 radiobutton #w.Violetr3 , "", SetBandValue,[none] ,285,325,20,20'Violet=7 radiobutton #w.Grayr3 , "", SetBandValue,[none] ,285,365,20,20'Gray=8 radiobutton #w.Whiter3 , "", SetBandValue,[none] ,285,405,20,20'White=9 'Band4 groupbox #w.Tolerance, "Tolerance", 380,10,80,430 radiobutton #w.Goldr4 , "", SetBandValue,[none] ,385, 45,20,20'Gold=5% radiobutton #w.Silverr4 , "", SetBandValue,[none] ,385, 85,20,20'Silver=10% radiobutton #w.Noner4 , "", SetBandValue,[none] ,385,125,20,20'None=20% 'Fram graphicbox #w.add, 530,60,292,30'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx stylebits #w.add, 0,_WS_BORDER, 0,0 graphicbox #w.up, 523,53,306,7 stylebits #w.up, 0,_WS_BORDER, 0,0' graphicbox #w.down, 523,90,306,7 stylebits #w.down, 0,_WS_BORDER, 0,0' graphicbox #w.side1, 523,53,7,44 stylebits #w.side1, 0,_WS_BORDER, 0,0' graphicbox #w.adds2, 822,53,7,44 stylebits #w.adds2, 0,_WS_BORDER, 0,0' 'Band1 graphicbox #w.Black1 , 100, 40,30,30 graphicbox #w.Brown1 , 100, 80,30,30 graphicbox #w.Red1 , 100,120,30,30 graphicbox #w.Orange1, 100,160,30,30 graphicbox #w.Yellow1, 100,200,30,30 graphicbox #w.Green1 , 100,240,30,30 graphicbox #w.Blue1 , 100,280,30,30 graphicbox #w.Violet1, 100,320,30,30 graphicbox #w.Gray1 , 100,360,30,30 graphicbox #w.White1 , 100,400,30,30 'Band2 graphicbox #w.Black2 , 210, 40,30,30 graphicbox #w.Brown2 , 210, 80,30,30 graphicbox #w.Red2 , 210,120,30,30 graphicbox #w.Orange2, 210,160,30,30 graphicbox #w.Yellow2, 210,200,30,30 graphicbox #w.Green2 , 210,240,30,30 graphicbox #w.Blue2 , 210,280,30,30 graphicbox #w.Violet2, 210,320,30,30 graphicbox #w.Gray2 , 210,360,30,30 graphicbox #w.White2 , 210,400,30,30 'Band3 graphicbox #w.Black3 , 310, 40,30,30 graphicbox #w.Brown3 , 310, 80,30,30 graphicbox #w.Red3 , 310,120,30,30 graphicbox #w.Orange3, 310,160,30,30 graphicbox #w.Yellow3, 310,200,30,30 graphicbox #w.Green3 , 310,240,30,30 graphicbox #w.Blue3 , 310,280,30,30 graphicbox #w.Violet3, 310,320,30,30 graphicbox #w.Gray3 , 310,360,30,30 graphicbox #w.White3 , 310,400,30,30 'Band4 graphicbox #w.Gold4 , 410, 40,30,30 graphicbox #w.Silver4, 410, 80,30,30 graphicbox #w.None4 , 410,120,30,30 'Selected Bands of Body Resistance graphicbox #w.Band1, 155,470,15,35 graphicbox #w.Band2, 185,470,15,35 graphicbox #w.Band3, 215,470,15,35 graphicbox #w.Band4, 255,470,15,35 stylebits #w.Band1, 0,_WS_BORDER,0,0 stylebits #w.Band2, 0,_WS_BORDER,0,0 stylebits #w.Band3, 0,_WS_BORDER,0,0 stylebits #w.Band4, 0,_WS_BORDER,0,0 'General Body Resistance graphicbox #w.37, 130,470,168,35 stylebits #w.37, 0,_WS_BORDER,0,0 'Terminal Wires graphicbox #w.42, 80,485,50,5 graphicbox #w.43, 300,485,50,5 open "Resistor Calculator" for window_nf as #w #w "trapclose [quit]" #w "font Times_New_Roman 13" #w.add "cls;down;fill yellow;font Times_New_Roman 16 bold;color red;backcolor yellow" #w.add "place 5 20;\Resistor Color Code of 4 Bands" #w.add "flush" CommandString$ = "cls;down;fill blue;flush" #w.up CommandString$ #w.down CommandString$ #w.side1 CommandString$ #w.adds2 CommandString$ FOR Band = 1 to 3 FOR Color = 1 to 10 Handle$ = "#w."; Colors$(Color,1); Band #Handle$ "cls;down;fill "; Colors$(Color,2); ";flush" #Handle$ "when leftButtonUp MouseClick" NEXT NEXT FOR Tolerance = 1 to 3 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 settings call MouseClick "#w.Black1", 0, 0 call MouseClick "#w.Black2", 0, 0 call MouseClick "#w.Black3", 0, 0 call MouseClick "#w.None4", 0, 0 [none] WAIT [quit] close #w END SUB MouseClick Handle$, PosX, PosY RBhandle$ = Left$(Handle$, Len(Handle$)-1); "r"; Right$(Handle$, 1) #RBhandle$ "set" call SetBandValue RBhandle$ END SUB SUB SetBandValue Handle$ ' parse handle of calling control Band = val(Right$(Handle$, 1)) ColorName$ = Mid$(Handle$, 4, Len(Handle$)-5) ' get position of color FOR i = 1 to 10 if Colors$(i,1) = ColorName$ then exit for if i < 4 then if Tolerances$(i,1) = ColorName$ then exit for NEXT ' if we had no match exit If i = 11 Then Exit Sub Select Case Band Case 1 Band1value = (i-1) * 10 Case 2 Band2value = i-1 Case 3 Band3value = 10 ^ (i-1) Case 4 Band4value = i*5 if Band4value = 15 then Band4value = 20 Case Else Exit Sub End Select Select Case Band Case 1, 2, 3 ColorValue$ = Colors$(i,2) Case 4 ColorValue$ = Tolerances$(i,2) Case Else Exit Sub End Select GBhandle$ = "#w.Band"; Band #GBhandle$ "cls;down;fill "; ColorValue$; ";flush" vv=(Band1value+Band2value)*Band3value Select Case Case vv>=1000000 #w.Ohm "Resistance Value=";vv/1000000;" M Ohm" Case vv>=1000 #w.Ohm "Resistance Value=";vv/1000;" K Ohm" Case Else #w.Ohm "Resistance Value=";vv;" Ohm" End Select if Band4value>0 then #w.Tole "Tolerance Value =";" +/- ";Band4value;"%" END SUB [[code]] [[StreamLineCode1|Chapter 1 - Starting the mission]] [[StreamLineCode2|Chapter 2 - Using arrays to reduce redundant code]] [[StreamLineCode3|Chapter 3 - Use variables for duplicate command strings]] [[StreamLineCode4|Chapter 4 - Consolidate event handlers]] **Chapter 5 - Apply mouse selection** [[StreamLineCode6|Chapter 6 - Remove radio buttons]] [[StreamLineCode7|Chapter 7 - Use a single graphics box for the resistor display]] [[StreamLineCode8|Chapter 8 - Further reduce the amount of GUI controls]] [[StreamLineCode9|Chapter 9 - Adding some eye-candy]] [[StreamLineCode10|Chapter 10 - Summary]]