Jump to content

Dynamically generate switch/case list with a for loop?


NYCmitch25
 Share

Go to solution Solved by Subz,

Recommended Posts

I have a switch statement that uses array values $aServer[0], $aServer[1], etc..  Is there a way to be dynamic and create it using a for loop instead?

 

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Input
            Switch GuiCtrlRead ( $input )
    Case "-Select-"
        GUICtrlSetData ( $label1, "Please select from menu." )
    Case $aServer[0]
        GUICtrlSetData ( $label1, $aDetail[0] )
        GUICtrlSetFont(-1, 13, $FW_NORMAL, $GUI_FONTNORMAL, "")
    Case $aServer[1]
        GUICtrlSetData ( $label1, $aDetail[1] )
        GUICtrlSetFont(-1, 13, $FW_NORMAL, $GUI_FONTNORMAL, "")
    Case $aServer[2]
        GUICtrlSetData ( $label1, $aDetail[2] )
        GUICtrlSetFont(-1, 13, $FW_NORMAL, $GUI_FONTNORMAL, "")
    Case $aServer[3]
        GUICtrlSetData ( $label1, $aDetail[3] )
        GUICtrlSetFont(-1, 13, $FW_NORMAL, $GUI_FONTNORMAL, "")
 EndSwitch

 

Link to comment
Share on other sites

  • Solution

Maybe something like this instead

#include <Array.au3>
#include <GuiComboBox.au3>
#include <GUIConstantsEx.au3>

Global $aServers[5][2] = [["-Select-",""],["Server 1","Details 1"],["Server 2","Details 2"],["Server 3","Details 3"],["Server 4","Details 4"]]

Example()

Func Example()
    Local $hWnd = GUICreate("Example", 300, 200)
    Local $idComboBox = GUICtrlCreateCombo("", 10, 10, 185, 20)
        GUICtrlSetData($idComboBox, _ArrayToString($aServers, "|", -1, -1, "|", 0, 0), $aServers[0][0])
    Local $idLabel = GUICtrlCreateLabel("", 200, 10, 100, 20)
    Local $idButton_Close = GUICtrlCreateButton("Close", 210, 170, 85, 25)
    GUISetState(@SW_SHOW)
    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE, $idButton_Close
                ExitLoop
            Case $idComboBox
                $iComboBox = _GUICtrlComboBox_GetCurSel($idComboBox)
                GUICtrlSetData($idLabel, $aServers[$iComboBox][1])
                MsgBox(4096, "Selection", "Server = " & $aServers[$iComboBox][0] & @CRLF & "Details = " & $aServers[$iComboBox][1])
        EndSwitch
    WEnd
EndFunc

 

Link to comment
Share on other sites

Thanks, I updated my code to look as follows:

;/---------------------------------------------------------------------
   Local $hWnd = GUICreate("HPE iLO Health Checker v1", 615, 600, 190, 122)
   Local $idComboBox = GUICtrlCreateCombo("", 0, 0, 609, 21)
   GUICtrlSetData($idComboBox, _ArrayToString($aServers, "|", -1, -1, "|", 0, 0), $aServers[0][0])
   GUICtrlSetFont(-1, 13, $FW_NORMAL, $GUI_FONTNORMAL, "")
   Local $idLabel = GUICtrlCreateLabel("", 20, 110, 100, 20)
   Local $idButton_Close = GUICtrlCreateButton("Exit Application", 0, 32, 609, 25)
   GUICtrlSetFont(-1, 13, $FW_NORMAL, $GUI_FONTNORMAL, "")
   $Label1 = GUICtrlCreateLabel("RESULTS", 8, 75, 1000, 625)
   GUICtrlSetFont(-1, 13, $FW_NORMAL, $GUI_FONTNORMAL, "")
   GUISetState(@SW_SHOW)
   While 1
      Switch GUIGetMsg()
         Case $GUI_EVENT_CLOSE, $idButton_Close
            ExitLoop
         Case $idComboBox
            $iComboBox = _GUICtrlComboBox_GetCurSel($idComboBox)
            GUICtrlSetData($label1, $aServers[$iComboBox][1])
            GUICtrlSetFont(-1, 13, $FW_NORMAL, $GUI_FONTNORMAL, "")
            ;MsgBox(4096, "Selection", "Server = " & $aServers[$iComboBox][0] & @CRLF & "Details = " & $aServers[$iComboBox][1])
        EndSwitch
    WEnd
;/---------------------------------------------------------------------

 

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...