Case Anweisung mit Array?

  • Guten Tag,

    ich wollte euch um Hilfe bitten, da ich schon lange an diesem Problem hänge...

    Folgendes:

    Ich habe ein GUI und habe ettliche Buttons, die mit GUIIDS belegt sind (von 8-80).
    Nun habe ich die Button IDS die ich benötige alle in ein Array gepackt (8,12,16,20 usw.)

    Nun habe ich folgenden Quellcode Ausschnitt:

    [autoit]


    While 1
    $msg = GUIGetMsg()
    Select
    case $msg = $GUI_EVENT_CLOSE
    exit
    Case $msg = $array
    GUICtrlSetBkColor ($array,$COLOR_BLACK)
    EndSelect
    WEnd

    [/autoit]


    Ich möchte mit dem Programm erreichen, damit ich meine Buttons (die alle im Array mit den jeweiligen IDS hinterlegt sind) per Klick schwarz färbe.
    Ich will dazu nicht jede ID mit einer "Case $msg = $ID GUICtrlSetBkColor ($ID,$COLOR_BLACK)" umfärben sondern mit einer Case anweisung alles in eins packen...


    Nun ist meine Frage... Geht das?


    Wäre um Hilfe sehr dankbar


    Gruß Qu1cks3r

    ~~MBI~~

    _______________________________

    everything is possible with AutoIT

    ___________________________

    Einmal editiert, zuletzt von MBI (13. August 2014 um 13:08)

  • OnEventMode verwenden und alle auf eine Funktion setzen, Beispiel:

    Spoiler anzeigen
    [autoit]

    Opt("GUIOnEventMode", 1)
    $Form1 = GUICreate("Form1", 601, 338, 192, 124)
    Global $aControls[9] = [GUICtrlCreateButton("Button1", 32, 24, 161, 81), GUICtrlCreateButton("Button1", 216, 24, 161, 81), GUICtrlCreateButton("Button1", 408, 24, 161, 81), _
    GUICtrlCreateButton("Button1", 32, 128, 161, 81), GUICtrlCreateButton("Button1", 216, 128, 161, 81), GUICtrlCreateButton("Button1", 408, 128, 161, 81), _
    GUICtrlCreateButton("Button1", 32, 232, 161, 81), GUICtrlCreateButton("Button1", 216, 232, 161, 81), GUICtrlCreateButton("Button1", 408, 232, 161, 81)]

    [/autoit] [autoit][/autoit] [autoit]

    For $i = 0 To UBound($aControls) - 1
    GUICtrlSetOnEvent($aControls[$i], "_ColorBlack")
    Next

    [/autoit] [autoit][/autoit] [autoit]

    GUISetOnEvent(-3, "_Exit")
    GUISetState(@SW_SHOW)

    [/autoit] [autoit][/autoit] [autoit]

    While Sleep(1000)
    WEnd

    [/autoit] [autoit][/autoit] [autoit]

    Func _Exit()
    Exit
    EndFunc

    [/autoit] [autoit][/autoit] [autoit]

    Func _ColorBlack()
    For $i = 0 To UBound($aControls) - 1
    GUICtrlSetBkColor($aControls[$i], 0x000000)
    Next
    EndFunc

    [/autoit]
  • Ah okay, ja stimmt.

    Danke für die fixe antwort!

    Klappt ;)

    Danke dir!

    ~~MBI~~

    _______________________________

    everything is possible with AutoIT

    ___________________________

  • Hi,
    ein Beispiel:

    [autoit]


    #AutoIt3Wrapper_Au3Check_Parameters=-q -d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7
    _main()
    Func _main()
    Local $input, $nMsg, $result, $aBtn[20]
    Local $aBtnLbl[20] = ["7", "8", "9", " / ", "4", "5", "6", " * ", "1", "2", "3", " - ", ".", "0", "^", " + ", "(", ")", "Clear", "="]
    GUICreate("Mod-TRechner-Tut", 235, 325)
    $input = GUICtrlCreateInput("", 10, 10, 215, 30, BitOR(0x0800, 0x0002)) ; $ES_READONLY $ES_RIGHT
    For $k = 0 To 19
    $aBtn[$k] = GUICtrlCreateButton($aBtnLbl[$k], 10 + Mod($k, 4) * 55, 50 + Int($k / 4) * 55, 50, 50)
    Next

    [/autoit][autoit][/autoit][autoit]

    GUISetState(@SW_SHOW)

    [/autoit][autoit][/autoit][autoit]

    Do
    $nMsg = GUIGetMsg()
    For $i = 0 To 18
    If $nMsg = $aBtn[$i] Then GUICtrlSetData($input, GUICtrlRead($input) & $aBtnLbl[$i])
    Next
    If $nMsg = $aBtn[19] Then
    $result = Execute(StringRegExpReplace(GUICtrlRead($input), '[^\.\(\)\*\/\-\+\d]', ''))
    ;~ $result = Execute(GUICtrlRead($input))
    If $result == "1.#INF" Or $result == "-1.#IND" Then $result = "Error"
    GUICtrlSetData($input, $result)
    EndIf
    If $nMsg = $aBtn[18] Then GUICtrlSetData($input, "")
    Until $nMsg = -3
    EndFunc ;==>_main
    ; Ende

    [/autoit]
    • Offizieller Beitrag

    Auch bei Deinem Beispiel finde ich die Switch-Variante übersichtlicher:

    Spoiler anzeigen
    [autoit]


    #AutoIt3Wrapper_Au3Check_Parameters=-q -d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7
    _main()
    Func _main()
    Local $input, $nMsg, $result, $aBtn[20]
    Local $aBtnLbl[20] = ["7", "8", "9", " / ", "4", "5", "6", " * ", "1", "2", "3", " - ", ".", "0", "^", " + ", "(", ")", "Clear", "="]
    GUICreate("Mod-TRechner-Tut", 235, 325)
    $input = GUICtrlCreateInput("", 10, 10, 215, 30, BitOR(0x0800, 0x0002)) ; $ES_READONLY $ES_RIGHT
    For $k = 0 To 19
    $aBtn[$k] = GUICtrlCreateButton($aBtnLbl[$k], 10 + Mod($k, 4) * 55, 50 + Int($k / 4) * 55, 50, 50)
    Next

    [/autoit] [autoit][/autoit] [autoit]

    GUISetState(@SW_SHOW)

    [/autoit] [autoit][/autoit] [autoit]

    Do
    $nMsg = GUIGetMsg()
    Switch $nMsg
    Case $aBtn[0] To $aBtn[17]
    GUICtrlSetData($input, GUICtrlRead($input) & $aBtnLbl[$nMsg - $aBtn[0]])
    Case $aBtn[19]
    $result = Execute(StringRegExpReplace(GUICtrlRead($input), '[^\.\(\)\*\/\-\+\d]', ''))
    If $result == "1.#INF" Or $result == "-1.#IND" Then $result = "Error"
    GUICtrlSetData($input, $result)
    Case $aBtn[18]
    GUICtrlSetData($input, "")
    EndSwitch
    Until $nMsg = -3
    EndFunc ;==>_main
    ; Ende

    [/autoit]
  • So

    Spoiler anzeigen
    [autoit]

    Opt("GUIOnEventMode", 1)

    [/autoit] [autoit][/autoit] [autoit]

    $hgui = GUICreate("getid", 220, 220)
    For $i = 0 To 15 ;16 Buttons erzeugen
    $id = GUICtrlCreateButton(Chr($i + 65), 10 + Mod($i, 4) * 50, 10 + Int($i / 4) * 50, 40, 40);raster
    GUICtrlSetOnEvent(-1, "getid") ;funktion zuweisen
    Next
    GUISetOnEvent(-3, "_exit") ;ende
    GUISetState()

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    While Sleep(10)
    WEnd

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    Func getid()
    $id = @GUI_CtrlId ;gibt die ID zurück
    MsgBox(0, "Test", "Es wurde der Button mit der ID= " & $id & " gedrückt!", 1)
    EndFunc ;==>getid

    [/autoit] [autoit][/autoit] [autoit]

    Func _exit() ;ende
    Exit
    EndFunc ;==>_exit

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit][/autoit] [autoit][/autoit]

    oder so

    Spoiler anzeigen
    [autoit]

    ;by Andy

    [/autoit] [autoit][/autoit] [autoit]

    $numberofbuttons = 24 ;number from 1 to 30
    Dim $buttoncontrolID[$numberofbuttons + 1]

    [/autoit] [autoit][/autoit] [autoit]

    GUICreate("test")
    For $i = 1 To $numberofbuttons ;we make an array of buttons
    $buttoncontrolID[$i] = GUICtrlCreateButton(" Button " & $i, 10 + ((($i > 10) + ($i > 20)) * 100), 30 + ($i - (($i > (($i > 20) + 1) * 10) * (($i > 20) + 1) * 10)) * 30,85,30)
    Next
    $somethingbutton=guictrlcreatebutton("do something ",310,30,80,30 )

    [/autoit] [autoit][/autoit] [autoit]

    ;here you could give the buttons an other text or color
    guictrlsetdata($buttoncontrolID[1] ,"Push!")
    GUICtrlSetColor($buttoncontrolID[1],0xFF00FF)
    guictrlsetdata($buttoncontrolID[13],"Push ME!")
    GUICtrlSetColor($buttoncontrolID[13],0x000FFF)
    guictrlsetdata($buttoncontrolID[17],"Push ME TOO!")
    GUICtrlSetColor($buttoncontrolID[17],0xFF0000)

    [/autoit] [autoit][/autoit] [autoit]

    GUISetState()

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    While 1
    $msg = GUIGetMsg()
    ;ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $msg = ' & $msg & @crlf & '>Error code: ' & @error & @crlf) ;### Debug Console
    Switch $msg
    Case -3 ;exit is pressed
    Exit
    case $somethingbutton
    Msgbox (0,"","Button ""do something"" is pressed")

    [/autoit] [autoit][/autoit] [autoit]

    Case $buttoncontrolID[1] To $buttoncontrolID[$numberofbuttons] ;one of the "array"-Buttons is pressed
    call ("_arraybutton_"&($numberofbuttons-($buttoncontrolID[$numberofbuttons]-$msg))) ;names oft the functions are _arraybutton_1() _arraybutton_2().....
    If @error = 0xDEAD And @extended = 0xBEEF Then MsgBox(4096, "ERROR", "Function "&"_arraybutton_"&($numberofbuttons-($buttoncontrolID[$numberofbuttons]-$msg))&" does not exist!")
    EndSwitch
    WEnd

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    ;*******************functions********************************
    func _arraybutton_1()

    [/autoit] [autoit][/autoit] [autoit]

    msgbox(0,"_arraybutton_"&($numberofbuttons-($buttoncontrolID[$numberofbuttons]-$msg)),"arraybutton pressed: "&GUICtrlRead($msg))
    endfunc
    ;
    ; here are the funcs of the buttons
    ;
    ;
    func _arraybutton_13()
    msgbox(0,"_arraybutton_"&($numberofbuttons-($buttoncontrolID[$numberofbuttons]-$msg)),"arraybutton pressed: "&GUICtrlRead($msg))
    endfunc

    [/autoit] [autoit][/autoit] [autoit]

    func _arraybutton_17()
    msgbox(0,"_arraybutton_"&($numberofbuttons-($buttoncontrolID[$numberofbuttons]-$msg)),"arraybutton pressed: "&GUICtrlRead($msg))
    endfunc

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit][/autoit] [autoit][/autoit]
  • So

    Spoiler anzeigen
    [autoit]

    Opt("GUIOnEventMode", 1)

    [/autoit] [autoit][/autoit] [autoit]

    $hgui = GUICreate("getid", 220, 220)
    For $i = 0 To 15 ;16 Buttons erzeugen
    $id = GUICtrlCreateButton(Chr($i + 65), 10 + Mod($i, 4) * 50, 10 + Int($i / 4) * 50, 40, 40);raster
    GUICtrlSetOnEvent(-1, "getid") ;funktion zuweisen
    Next
    GUISetOnEvent(-3, "_exit") ;ende
    GUISetState()

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    While Sleep(10)
    WEnd

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    Func getid()
    $id = @GUI_CtrlId ;gibt die ID zurück
    MsgBox(0, "Test", "Es wurde der Button mit der ID= " & $id & " gedrückt!", 1)
    EndFunc ;==>getid

    [/autoit] [autoit][/autoit] [autoit]

    Func _exit() ;ende
    Exit
    EndFunc ;==>_exit

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit][/autoit] [autoit][/autoit]

    oder so

    Spoiler anzeigen
    [autoit]

    ;by Andy

    [/autoit] [autoit][/autoit] [autoit]

    $numberofbuttons = 24 ;number from 1 to 30
    Dim $buttoncontrolID[$numberofbuttons + 1]

    [/autoit] [autoit][/autoit] [autoit]

    GUICreate("test")
    For $i = 1 To $numberofbuttons ;we make an array of buttons
    $buttoncontrolID[$i] = GUICtrlCreateButton(" Button " & $i, 10 + ((($i > 10) + ($i > 20)) * 100), 30 + ($i - (($i > (($i > 20) + 1) * 10) * (($i > 20) + 1) * 10)) * 30,85,30)
    Next
    $somethingbutton=guictrlcreatebutton("do something ",310,30,80,30 )

    [/autoit] [autoit][/autoit] [autoit]

    ;here you could give the buttons an other text or color
    guictrlsetdata($buttoncontrolID[1] ,"Push!")
    GUICtrlSetColor($buttoncontrolID[1],0xFF00FF)
    guictrlsetdata($buttoncontrolID[13],"Push ME!")
    GUICtrlSetColor($buttoncontrolID[13],0x000FFF)
    guictrlsetdata($buttoncontrolID[17],"Push ME TOO!")
    GUICtrlSetColor($buttoncontrolID[17],0xFF0000)

    [/autoit] [autoit][/autoit] [autoit]

    GUISetState()

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    While 1
    $msg = GUIGetMsg()
    ;ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $msg = ' & $msg & @crlf & '>Error code: ' & @error & @crlf) ;### Debug Console
    Switch $msg
    Case -3 ;exit is pressed
    Exit
    case $somethingbutton
    Msgbox (0,"","Button ""do something"" is pressed")

    [/autoit] [autoit][/autoit] [autoit]

    Case $buttoncontrolID[1] To $buttoncontrolID[$numberofbuttons] ;one of the "array"-Buttons is pressed
    call ("_arraybutton_"&($numberofbuttons-($buttoncontrolID[$numberofbuttons]-$msg))) ;names oft the functions are _arraybutton_1() _arraybutton_2().....
    If @error = 0xDEAD And @extended = 0xBEEF Then MsgBox(4096, "ERROR", "Function "&"_arraybutton_"&($numberofbuttons-($buttoncontrolID[$numberofbuttons]-$msg))&" does not exist!")
    EndSwitch
    WEnd

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    ;*******************functions********************************
    func _arraybutton_1()

    [/autoit] [autoit][/autoit] [autoit]

    msgbox(0,"_arraybutton_"&($numberofbuttons-($buttoncontrolID[$numberofbuttons]-$msg)),"arraybutton pressed: "&GUICtrlRead($msg))
    endfunc
    ;
    ; here are the funcs of the buttons
    ;
    ;
    func _arraybutton_13()
    msgbox(0,"_arraybutton_"&($numberofbuttons-($buttoncontrolID[$numberofbuttons]-$msg)),"arraybutton pressed: "&GUICtrlRead($msg))
    endfunc

    [/autoit] [autoit][/autoit] [autoit]

    func _arraybutton_17()
    msgbox(0,"_arraybutton_"&($numberofbuttons-($buttoncontrolID[$numberofbuttons]-$msg)),"arraybutton pressed: "&GUICtrlRead($msg))
    endfunc

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit][/autoit] [autoit][/autoit]