Jump to content
Sign in to follow this  
litlmike

Better Way to Code This?

Recommended Posts

litlmike

I assume there must be a better way to write this, can you point me in the right direction?

There are 40 buttons at the top of the GUI, and when the user presses any of those 40 buttons, it will change a label's color to green (at the bottom of the GUI). Instead of making 40 different cases, I assume there must be a more efficient way to write just one case, that satifisies all 40 buttons. Suggestions?

To see an example, you can run the script and the top left button "Sapphire" under "Chipped" will show you what I am trying to accomplish.

Thanks in Advance!

#include <GUIConstants.au3>


Global $Color
#Region ### START Koda GUI section ### 
$Form1_1 = GUICreate("Gem TD Combinations", 1066, 900, 1300, 50)
$Group1 = GUICtrlCreateGroup("AGroup1", 8, 15, 175, 337)
$Button1 = GUICtrlCreateButton("Sapphire", 24, 63, 75, 25, 0)
$Input1 = GUICtrlCreateInput("AInput1", 104, 63, 49, 21)
$Label1 = GUICtrlCreateLabel("Chipped", 104, 39, 51, 20)
GUICtrlSetFont(-1, 10, 400, 0, "arial")
$Button2 = GUICtrlCreateButton("Diamond", 24, 95, 75, 25, 0)
$Input2 = GUICtrlCreateInput("AInput1", 104, 95, 49, 21)
$Button3 = GUICtrlCreateButton("Opal", 24, 127, 75, 25, 0)
$Input3 = GUICtrlCreateInput("AInput1", 104, 127, 49, 21)
$Button4 = GUICtrlCreateButton("Amethyst", 24, 287, 75, 25, 0)
$Input4 = GUICtrlCreateInput("AInput1", 104, 290, 49, 21)
$Button5 = GUICtrlCreateButton("Topaz", 24, 255, 75, 25, 0)
$Input5 = GUICtrlCreateInput("AInput1", 104, 260, 49, 21)
$Button6 = GUICtrlCreateButton("Ruby", 24, 223, 75, 25, 0)
$Input6 = GUICtrlCreateInput("AInput1", 104, 223, 49, 21)
$Button7 = GUICtrlCreateButton("Aquamarine", 24, 191, 75, 25, 0)
$Input7 = GUICtrlCreateInput("AInput1", 104, 191, 49, 21)
$Button8 = GUICtrlCreateButton("Emerald", 24, 159, 75, 25, 0)
$Input8 = GUICtrlCreateInput("AInput1", 104, 159, 49, 21)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$Group2 = GUICtrlCreateGroup("AGroup1", 200, 15, 175, 337)
$Button9 = GUICtrlCreateButton("Sapphire", 216, 63, 75, 25, 0)
$Input9 = GUICtrlCreateInput("AInput1", 296, 63, 49, 21)
$Label2 = GUICtrlCreateLabel("Flawed", 296, 39, 45, 20)
GUICtrlSetFont(-1, 10, 400, 0, "arial")
$Button10 = GUICtrlCreateButton("Diamond", 216, 95, 75, 25, 0)
$Input10 = GUICtrlCreateInput("AInput1", 296, 95, 49, 21)
$Button11 = GUICtrlCreateButton("Opal", 216, 127, 75, 25, 0)
$Input11 = GUICtrlCreateInput("AInput1", 296, 127, 49, 21)
$Button12 = GUICtrlCreateButton("Amethyst", 216, 287, 75, 25, 0)
$Input12 = GUICtrlCreateInput("AInput1", 296, 290, 49, 21)
$Button13 = GUICtrlCreateButton("Topaz", 216, 255, 75, 25, 0)
$Input13 = GUICtrlCreateInput("AInput1", 296, 260, 49, 21)
$Button14 = GUICtrlCreateButton("Ruby", 216, 223, 75, 25, 0)
$Input14 = GUICtrlCreateInput("AInput1", 296, 223, 49, 21)
$Button15 = GUICtrlCreateButton("Aquamarine", 216, 191, 75, 25, 0)
$Input15 = GUICtrlCreateInput("AInput1", 296, 191, 49, 21)
$Button16 = GUICtrlCreateButton("Emerald", 216, 159, 75, 25, 0)
$Input16 = GUICtrlCreateInput("AInput1", 296, 159, 49, 21)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$Group3 = GUICtrlCreateGroup("AGroup1", 392, 15, 175, 337)
$Button17 = GUICtrlCreateButton("Sapphire", 408, 63, 75, 25, 0)
$Input17 = GUICtrlCreateInput("AInput1", 488, 63, 49, 21)
$Label3 = GUICtrlCreateLabel("Regular", 488, 39, 48, 20)
GUICtrlSetFont(-1, 10, 400, 0, "arial")
$Button18 = GUICtrlCreateButton("Diamond", 408, 95, 75, 25, 0)
$Input18 = GUICtrlCreateInput("AInput1", 488, 95, 49, 21)
$Button19 = GUICtrlCreateButton("Opal", 408, 127, 75, 25, 0)
$Input19 = GUICtrlCreateInput("AInput1", 488, 127, 49, 21)
$Button20 = GUICtrlCreateButton("Amethyst", 408, 287, 75, 25, 0)
$Input20 = GUICtrlCreateInput("AInput1", 488, 290, 49, 21)
$Button21 = GUICtrlCreateButton("Topaz", 408, 255, 75, 25, 0)
$Input21 = GUICtrlCreateInput("AInput1", 488, 260, 49, 21)
$Button22 = GUICtrlCreateButton("Ruby", 408, 223, 75, 25, 0)
$Input22 = GUICtrlCreateInput("AInput1", 488, 223, 49, 21)
$Button23 = GUICtrlCreateButton("Aquamarine", 408, 191, 75, 25, 0)
$Input23 = GUICtrlCreateInput("AInput1", 488, 191, 49, 21)
$Button24 = GUICtrlCreateButton("Emerald", 408, 159, 75, 25, 0)
$Input24 = GUICtrlCreateInput("AInput1", 488, 159, 49, 21)
$Group5 = GUICtrlCreateGroup("AGroup1", 568, 15, 175, 337)
$Button33 = GUICtrlCreateButton("Sapphire", 584, 63, 75, 25, 0)
$Input33 = GUICtrlCreateInput("AInput1", 664, 63, 49, 21)
$Label5 = GUICtrlCreateLabel("Flawless", 664, 39, 55, 20)
GUICtrlSetFont(-1, 10, 400, 0, "arial")
$Button34 = GUICtrlCreateButton("Diamond", 584, 95, 75, 25, 0)
$Input34 = GUICtrlCreateInput("AInput1", 664, 95, 49, 21)
$Button35 = GUICtrlCreateButton("Opal", 584, 127, 75, 25, 0)
$Input35 = GUICtrlCreateInput("AInput1", 664, 127, 49, 21)
$Button36 = GUICtrlCreateButton("Amethyst", 584, 287, 75, 25, 0)
$Input36 = GUICtrlCreateInput("AInput1", 664, 290, 49, 21)
$Button37 = GUICtrlCreateButton("Topaz", 584, 255, 75, 25, 0)
$Input37 = GUICtrlCreateInput("AInput1", 664, 260, 49, 21)
$Button38 = GUICtrlCreateButton("Ruby", 584, 223, 75, 25, 0)
$Input38 = GUICtrlCreateInput("AInput1", 664, 223, 49, 21)
$Button39 = GUICtrlCreateButton("Aquamarine", 584, 191, 75, 25, 0)
$Input39 = GUICtrlCreateInput("AInput1", 664, 191, 49, 21)
$Button40 = GUICtrlCreateButton("Emerald", 584, 159, 75, 25, 0)
$Input40 = GUICtrlCreateInput("AInput1", 664, 159, 49, 21)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$Group6 = GUICtrlCreateGroup("AGroup1", 760, 15, 175, 337)
$Button41 = GUICtrlCreateButton("Sapphire", 776, 63, 75, 25, 0)
$Input41 = GUICtrlCreateInput("AInput1", 856, 63, 49, 21)
$Label6 = GUICtrlCreateLabel("Perfect", 856, 39, 45, 20)
GUICtrlSetFont(-1, 10, 400, 0, "arial")
$Button42 = GUICtrlCreateButton("Diamond", 776, 95, 75, 25, 0)
$Input42 = GUICtrlCreateInput("AInput1", 856, 95, 49, 21)
$Button43 = GUICtrlCreateButton("Opal", 776, 127, 75, 25, 0)
$Input43 = GUICtrlCreateInput("AInput1", 856, 127, 49, 21)
$Button44 = GUICtrlCreateButton("Amethyst", 776, 287, 75, 25, 0)
$Input44 = GUICtrlCreateInput("AInput1", 856, 290, 49, 21)
$Button45 = GUICtrlCreateButton("Topaz", 776, 255, 75, 25, 0)
$Input45 = GUICtrlCreateInput("AInput1", 856, 260, 49, 21)
$Button46 = GUICtrlCreateButton("Ruby", 776, 223, 75, 25, 0)
$Input46 = GUICtrlCreateInput("AInput1", 856, 223, 49, 21)
$Button47 = GUICtrlCreateButton("Aquamarine", 776, 191, 75, 25, 0)
$Input47 = GUICtrlCreateInput("AInput1", 856, 191, 49, 21)
$Button48 = GUICtrlCreateButton("Emerald", 776, 159, 75, 25, 0)
$Input48 = GUICtrlCreateInput("AInput1", 856, 159, 49, 21)
;~ GUICtrlCreateGroup("", -99, -99, 1, 1)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
$wLabel = 100 ; Label Width

$Button49 = GUICtrlCreateButton("Silver", 50, 420, 75, 25, 0)
GUICtrlSetFont(-1, 10, 800, 0, "arial")
$Group7 = GUICtrlCreateGroup("", 25, 400, 129, 129)
$Label8 = GUICtrlCreateLabel("C. Sapphire", 59, 456,    $wLabel, 17)
$Label9 = GUICtrlCreateLabel("C. Diamond",  59, 480,     $wLabel, 17)
$Label10 = GUICtrlCreateLabel("C. Topaz",   59, 504,     $wLabel, 17)

;;;;;;;;;;;;;
GUICtrlCreateGroup("", -99, -99, 1, 1)
$Button50 = GUICtrlCreateButton("Malachite", 193, 420, 75, 25, 0)
GUICtrlSetFont(-1, 10, 800, 0, "arial")
$Group8 = GUICtrlCreateGroup("", 168, 400, 129, 129)
$Label7 = GUICtrlCreateLabel("C. Opal", 202, 456, $wLabel, 17)
$Label11 = GUICtrlCreateLabel("C. Emerald", 202, 480, $wLabel, 17)
$Label12 = GUICtrlCreateLabel("C. Aquamarine", 202, 504, 70, 17)
;;;;;;;;;;;;;
$Button51 = GUICtrlCreateButton("Star Ruby", 337, 420, 75, 25, 0)
GUICtrlSetFont(-1, 10, 800, 0, "arial")
$Group9 = GUICtrlCreateGroup("", 312, 400, 129, 129)
$Label13 = GUICtrlCreateLabel("C. Amethyst", 346, 456, $wLabel, 17)
$Label14 = GUICtrlCreateLabel("C. Ruby", 346, 480, $wLabel, 17)
$Label15 = GUICtrlCreateLabel("Flawed Ruby", 346, 504, 70, 17)
;;;;;;;;;;;;;
$Button52 = GUICtrlCreateButton("Jade", 473, 420, 75, 25, 0)
GUICtrlSetFont(-1, 10, 800, 0, "arial")
$Group10 = GUICtrlCreateGroup("", 448, 400, 129, 129)
$Label16 = GUICtrlCreateLabel("Emerald", 482, 456, $wLabel, 17)
$Label17 = GUICtrlCreateLabel("Opal", 482, 480, $wLabel, 17)
$Label18 = GUICtrlCreateLabel("Flawed Saphhire", 482, 504, 80, 17)

;;;;;;;;;;;;;
$Button55 = GUICtrlCreateButton("Red Crystal - AA", 595, 420, 110, 25, 0)
GUICtrlSetFont(-1, 10, 800, 0, "arial")
$Group13 = GUICtrlCreateGroup("", 584, 400, 129, 129)
$Label25 = GUICtrlCreateLabel("Flawless Emerald", 618, 456, 90, 17)
$Label26 = GUICtrlCreateLabel("Ruby", 618, 480, 80, 17)
$Label27 = GUICtrlCreateLabel("Flawed Amethyst", 618, 504, 80, 17)
$Group14 = GUICtrlCreateGroup("", 728, 400, 129, 129)
;;;;;;;;;;;;;;;;
$Button56 = GUICtrlCreateButton("Dark Emerald", 753, 420, 91, 25, 0)
GUICtrlSetFont(-1, 10, 800, 0, "arial")
$Label28 = GUICtrlCreateLabel("Perfect Emerald", 762, 456, 80, 17)
$Label29 = GUICtrlCreateLabel("Flawless Saphire", 762, 480, 80, 17)
$Label30 = GUICtrlCreateLabel("Flawed Topaz", 762, 504, 80, 17)
$Group15 = GUICtrlCreateGroup("", 864, 400, 129, 129)

;;;;;;;;;;;;;;
$Button57 = GUICtrlCreateButton("Yellow Sapphire", 873, 420, 115, 25, 0)
GUICtrlSetFont(-1, 10, 800, 0, "arial")
$Label31 = GUICtrlCreateLabel("P. Sapphire", 898, 456, $wLabel, 17)
$Label32 = GUICtrlCreateLabel("Flawless Topaz", 898, 480, $wLabel, 17)
$Label33 = GUICtrlCreateLabel("Flawless Ruby", 898, 504, $wLabel, 17)
$Group16 = GUICtrlCreateGroup("", 24, 535, 129, 129)
;;;;;;;;;;;;;;

$Button58 = GUICtrlCreateButton("Blood Stone", 49, 555, 83, 25, 0)
GUICtrlSetFont(-1, 10, 800, 0, "arial")
$Label34 = GUICtrlCreateLabel("Perfect Ruby", 58, 591, $wLabel, 17)
$Label35 = GUICtrlCreateLabel("Flawless Aquamarine", 58, 615, $wLabel, 17)
$Label36 = GUICtrlCreateLabel("Amethyst", 58, 639, $wLabel, 17)
$Group17 = GUICtrlCreateGroup("", 160, 535, 129, 129)

;;;;;;;;;;;;;;
$Button59 = GUICtrlCreateButton("Uranium 238", 177, 555, 91, 25, 0)
GUICtrlSetFont(-1, 10, 800, 0, "arial")
$Label37 = GUICtrlCreateLabel("Perfect Topaz", 194, 591, $wLabel, 17)
$Label38 = GUICtrlCreateLabel("Flawed Opal", 194, 615, $wLabel, 17)
$Label39 = GUICtrlCreateLabel("Sapphire", 194, 639, $wLabel, 17)
$Group18 = GUICtrlCreateGroup("", 184, 935, 129, 129)

;;;;;;;;;;;;;;;;;;;;;
$Button61 = GUICtrlCreateButton("Gold", 337, 555, 75, 25, 0)
GUICtrlSetFont(-1, 10, 800, 0, "arial")
$Label43 = GUICtrlCreateLabel("Perfect Amethyst", 346, 591, 80, 17)
$Label44 = GUICtrlCreateLabel("Flawless Amethyst", 346, 615, 90, 17)
$Label45 = GUICtrlCreateLabel("Flawed Diamond", 346, 639, 80, 17)
$Group19 = GUICtrlCreateGroup("", 312, 535, 129, 129)


;;;;;;;;;;;;;;
$Button62 = GUICtrlCreateButton("Pink Diamond", 481, 555, 99, 25, 0)
GUICtrlSetFont(-1, 10, 800, 0, "arial")
$Label46 = GUICtrlCreateLabel("Perfect Diamond", 498, 591, 80, 17)
$Label47 = GUICtrlCreateLabel("Topaz", 498, 615, $wLabel, 17)
$Label48 = GUICtrlCreateLabel("Diamond", 498, 639, 47, 17)
$Group20 = GUICtrlCreateGroup("", 464, 535, 129, 129)

;;;;;;;;;;;;;;
$Button63 = GUICtrlCreateButton("Black Opal", 625, 555, 75, 25, 0)
GUICtrlSetFont(-1, 10, 800, 0, "arial")
$Label49 = GUICtrlCreateLabel("Perfect Opal", 634, 591, $wLabel, 17)
$Label50 = GUICtrlCreateLabel("Flawless Diamond", 634, 615, $wLabel, 17)
$Label51 = GUICtrlCreateLabel("Aquamarine", 634, 639, 47, 17)
$Group21 = GUICtrlCreateGroup("", 600, 535, 129, 129)


;;;;;;;;;;;;;;
$Button65 = GUICtrlCreateButton("Paraiba Tourmaline",   745, 555, 110, 25, 0)
GUICtrlSetFont(-1, 8, 800, 0, "arial")
$Label55 = GUICtrlCreateLabel("Perfect Aquamarine",     760, 591, 100, 17)
$Label56 = GUICtrlCreateLabel("Flawless Opal",          760, 615, 100, 17)
$Label57 = GUICtrlCreateLabel("Flawed Emerald",         760, 639, 100, 17)
$Label57 = GUICtrlCreateLabel("Flawed Aquamarine",      760, 663, 100, 17)
$Group23 = GUICtrlCreateGroup("", 736, 535, 129, 150)


GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit

        Case $Button1
            _SetFont_Green ($Label8)


    EndSwitch
WEnd

Func _SetFont_Green ($vButton, $Color = 0x3cb371)
    GUICtrlSetColor ( $vButton, $Color)

EndFunc
Edited by litlmike

Share this post


Link to post
Share on other sites
Toady

Consider revising your gui layout, such as using a dropdown list or a listview to select the group you want to edit. Put those 5 groups into a dropdown or listview and name them accordingly.

Doing it this way will reduce the amount of controls you need by 5!

After that just use a case select like this:

Case: Group 1 selected

Do stuff with inputs

Case: Goup 2 selected

Do stuff with inputs


www.itoady.com

A* (A-star) Searching Algorithm - A.I. Artificial Intelligence bot path finding

Share this post


Link to post
Share on other sites
litlmike

Consider revising your gui layout, such as using a dropdown list or a listview to select the group you want to edit. Put those 5 groups into a dropdown or listview and name them accordingly.

Doing it this way will reduce the amount of controls you need by 5!

After that just use a case select like this:

Case: Group 1 selected

Do stuff with inputs

Case: Goup 2 selected

Do stuff with inputs

Thanks for the suggestion, but I see this being an issue, in that the data has to be entered in 1-click. To my knowledge a drop down/listview would require 2-clicks.

Would some sort of array, or loop solve the problem, while allowing buttons to be used?

Share this post


Link to post
Share on other sites
Toady

... :)

Im not saying using the listview or dropdown to trigger an event, Im saying that your GUI is way too redundant.

Yes an array is the way to go... good luck!


www.itoady.com

A* (A-star) Searching Algorithm - A.I. Artificial Intelligence bot path finding

Share this post


Link to post
Share on other sites
eltorro

Use GuiOnEventMode

Create your controls in an 2 dimensional array, use element [0] to hold the GuiCtrlCreateButton element [1] to hold the color.

Like this:

#include <GUIConstants.au3>
Opt("GUIOnEventMode", 1)
Local $Controls[12][2]
Local $y =0
$GUI = GuiCreate("",800,600,10,10)
GUISetOnEvent($GUI_EVENT_CLOSE,"_Exit")
For $x = 0 to 15
    $color = "0x"&Hex(($x*255/15),2)&Hex(($x*255/15),2)&Hex(($x*255/15),2)
    $Controls[$x][0]=GUICtrlCreateButton("Color:"&$color,10,$y)
    GuiCtrlSetOnEvent(-1,"ButtonHandler")
    $Controls[$x][1]=$color
    ConsoleWrite($color&@LF)
    $y+= 30
Next
GUISetState()

While 1
    Sleep(100)
WEnd

Func ButtonHandler()
    $CtrlId=@GUI_CtrlId
    For $x= 0 to UBound($Controls,1)-1 step 2
        If $Controls[$x][0] = $CtrlId Then
            GUISetBkColor($Controls[$x][1])
            ExitLoop
        ElseIf $x < UBound($Controls,1) Then
            If $Controls[$x+1][0] = $CtrlId Then
                GUISetBkColor($Controls[$x+1][1])
                ExitLoop
            EndIf
        EndIf
    Next
EndFunc
Func _Exit()
    Exit
EndFunc
Edited by eltorro

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×