Sign in to follow this  
Followers 0
RegularGuy

Can "Switch Case" read an array

5 posts in this topic

Hi,

I need help writing this script so that I don't need to write so many repetitive lines. I'm sorry if this has been answered previously, I've already spent several hours looking and trying things, but I just don't know what to look for anymore. I'm still quite new to this all.

Thanks in advance for you help

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <SliderConstants.au3>
#include <Array.au3>
#include <EditConstants.au3>
#include <ButtonConstants.au3>


$loop = 10 ;======================THIS IS HOW TO CONTROL THE NUMBER OF LOOPS===================
Global $Guiarr[$loop][2]
Global $numarr[$loop] = ["Zero", "One", "Two","Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"]
$x = 0
$y = 30

GUICreate ("Gui Loop", 300, 300)
Do
    $Guiarr[$x][0] = GUICtrlCreateCheckbox($numarr[$x], 30, $y, 75, 20)
    $Guiarr[$x][1] = GUICtrlCreateEdit("0", 110, $y, 50, 20, 0x2000)
    GuiCtrlSetState (-1, $GUI_DISABLE)
    $x = $x + 1
    $y = $y + 20
Until $x =  $loop

GUISetState ()
While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case $Guiarr[0][0]
            _Enable ($Guiarr[0][0],$Guiarr[0][1])

        Case $Guiarr[1][0]
            _Enable ($Guiarr[1][0],$Guiarr[1][1])

        Case $Guiarr[2][0]
            _Enable ($Guiarr[2][0],$Guiarr[2][1])

        Case $Guiarr[3][0]
            _Enable ($Guiarr[3][0],$Guiarr[3][1])

        Case $Guiarr[4][0]
            _Enable ($Guiarr[4][0],$Guiarr[4][1])

        Case $Guiarr[5][0]
            _Enable ($Guiarr[5][0],$Guiarr[5][1])

        Case $Guiarr[6][0]
            _Enable ($Guiarr[6][0],$Guiarr[6][1])

        Case $Guiarr[7][0]
            _Enable ($Guiarr[7][0],$Guiarr[7][1])

        Case $Guiarr[8][0]
            _Enable ($Guiarr[8][0],$Guiarr[8][1])

        Case $Guiarr[9][0]
            _Enable ($Guiarr[9][0],$Guiarr[9][1])

        Case -3
            Exit
    EndSwitch
WEnd

Func _Enable ($check, $edit)
    IF GUICtrlRead($check) == 1 Then
        GUICtrlSetstate ($edit, $GUI_ENABLE)
    Else
        GUICtrlSetstate ($edit, $GUI_DISABLE)
    EndIf
EndFunc

Share this post


Link to post
Share on other sites



RegularGuy,

Switch cannot read an array, but you can inside a Case - like this: ;)

While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case -3
            Exit
        Case Else
            For $i = 0 To $loop - 1
                If $msg = $Guiarr[$i][0] Then _Enable ($Guiarr[$i][0],$Guiarr[$i][1])
            Next
    EndSwitch
WEnd

All clear? :)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

Melba23,

Fantastic!!! That's Exactly what I was looking for.

Interesting that you used "Case Else". Not that I want to do this but just more for curiosity's sake;

How would you then call 2 different arrays that needed to run two different Functions?

So if we take my original example and then add another group of Check boxes that when checked would do something else.

I hope that makes sense. I guess my question is, can you read an array without using "Case Else"?

Thank you so much for you help!!

Share this post


Link to post
Share on other sites

RegularGuy,

How would you then call 2 different arrays that needed to run two different Functions?

We would need use a little trick. ;)

AutoIt allocates ControlIDs sequentially if you create the controls in IMMEDIATE succession. So we do just that and then use a Case to look for the range of IDs:

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <SliderConstants.au3>
#include <Array.au3>
#include <EditConstants.au3>
#include <ButtonConstants.au3>


$loop = 10 ;======================THIS IS HOW TO CONTROL THE NUMBER OF LOOPS===================
Global $Guiarr_1[$loop][2], $Guiarr_2[$loop][2]
Global $numarr[$loop] = ["Zero", "One", "Two","Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"]

GUICreate ("Gui Loop", 600, 300)

; Create a range of checkboes
$y = 30
For $i = 0 To $loop - 1
    $Guiarr_1[$i][0] = GUICtrlCreateCheckbox($numarr[$i], 30, $y, 75, 20)
    $y = $y + 20
Next
; Create another range of checkboxes
$y = 30
For $i = 0 To $loop - 1
    $Guiarr_2[$i][0] = GUICtrlCreateCheckbox($numarr[$i], 330, $y, 75, 20)
    $y = $y + 20
Next
; Create other controls
$y = 30
For $i = 0 To $loop - 1
    $Guiarr_1[$i][1] = GUICtrlCreateInput("0", 110, $y, 50, 20, 0x2000)
    GuiCtrlSetState (-1, $GUI_DISABLE)
    $Guiarr_2[$i][1] = GUICtrlCreateLabel("", 410, $y, 50, 20)
    GUICtrlSetBkColor(-1, 0x00FF00)
    GuiCtrlSetState (-1, $GUI_DISABLE)
    $y = $y + 20
Next

GUISetState ()

While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case -3
            Exit
        Case $Guiarr_1[0][0] To $Guiarr_1[$loop - 1][0] ; Is it in the first range
            For $i = 0 To $loop - 1
                If $msg = $Guiarr_1[$i][0] Then _Enable ($Guiarr_1[$i][0],$Guiarr_1[$i][1])
            Next
        Case $Guiarr_2[0][0] To $Guiarr_2[$loop - 1][0] ; Is it in the second range
            For $i = 0 To $loop - 1
                If $msg = $Guiarr_2[$i][0] Then _Colour ($Guiarr_2[$i][0],$Guiarr_2[$i][1])
            Next
    EndSwitch
WEnd

Func _Enable ($check, $edit)
    IF GUICtrlRead($check) = 1 Then
        GUICtrlSetstate ($edit, $GUI_ENABLE)
    Else
        GUICtrlSetstate ($edit, $GUI_DISABLE)
    EndIf
EndFunc

Func _Colour ($check, $label)
    IF GUICtrlRead($check) = 1 Then
        GUICtrlSetBkColor ($label, 0xFF0000)
    Else
        GUICtrlSetBkColor ($label, 0x00FF00)
    EndIf
EndFunc

It takes a bit longer to set up because you need to create the checkboxes in separate loops, but it makes the While...WEnd loop much easier.

All clear? :)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

Melba23,

That was brilliant! That answers all my questions. Thank you So much for you help.

I didn't realize that you could write "Case $Guiarr_1[0][0] To $Guiarr_1[$loop - 1][0]"

That helps so much.

Thanks again

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  
Followers 0