Jump to content
Sign in to follow this  

Switch not working

Recommended Posts

I have never used switch in AutoIT earlier.


But today, when I started to implement it in my code, I am getting errors.

So I tried to execute default example, but still getting the same error.


Any suggestions please...

Share this post

Link to post
Share on other sites


Works fine for me:

#include <MsgBoxConstants.au3>

Local $sMsg = ""

Switch @HOUR
    Case 6 To 11
        $sMsg = "Good morning"
    Case 12 To 17
        $sMsg = "Good Afternoon"
    Case 18 To 21
        $sMsg = "Good Evening"
    Case Else
        $sMsg = "Go to bed!"



Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:


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


If you want help with a picture, go to an image related forum, if you want help with a script, post the script and not a screenshot of it.

If this script were bigger, and you wanted help with it, someone who wanted to run your script would have to retype the whole thing because all we have is a screenshot. People tend to ignore you when you post screenshots of a text file, post the text next time.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

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  

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By abberration
      Software Installer
      version 1.2 - Jan 2, 2021
      Hello, everyone!
      Today, I present to you a new version of Software Installer. This script is designed to be used on CDs/DVDs/USB drives where you put software in a folder (called Software) and you can select multiple software and it will automate installing your selected software one after another with little to no interaction. To do this, you simply need to configure each software with a silent switch (most software have one or more). You can put your software in categories to organize them on the main GUI.

      The major new things concerning this project:
      Added a Detect button, where the software tries to detect what kind of installer is used, saving you from having to find out on your own. It's not perfect, but it detects many common installer types. Added a Installers.db file for installers that were not created by the most common methods (Inno Setup, NSIS, etc). I will continue to add to this file. If you have any that you find that you would like to contribute, please PM. I would very much appreciate your help. This version searches for all subfolders now and shows .exe and .msi files that you may not want to see on the main screen. Therefore, I have added the option to "Hide This Item On Main GUI". Added the ability to install up to 2 software before and up to 2 software or registry entries after installing your selected software. This will allow you to install things like service packs, DotNet, etc. (prerequisites). And it will allow you to apply your licensing information through registry entries or patches/updates. If you want the full package (source code, compiled .exe (both 32/64 bit), changelog, installers.db, icon, etc.), you can download it here:
      I have a lifetime account at FileFactory, so the link should never die and you do NOT need an account or pay them money to download. Simply choose the free/slow download option. The file is small and will still download fairly quickly. If you have trouble downloading it, PM me. I have the previous versions of this script, but they are inferior to this version, so if you want the older versions, PM me and I will provide them.
      If you just want the bare minimum, the script, installers.db and changelog are attached on this forum below.
      Software Installer.au3 Installers.db Changelog.pdf
    • By Skysnake
      From From here, bottom of the post
      I am not arguing the logic of this, merely would like to point out that if there is such a rule, it is not documented...  Are there other such rules?
    • By WoodGrain
      Hi guys,
      Could someone please tell me what I'm doing wrong with this code?
      I'm trying to take user input and then run a function with the same name, but it's not calling the function.
      So in the below after pressing Shift+F8 I would enter the text (without quotes) "testFunc".
      As you can see I've also tried this with IF statements, I'm new to using Switch/Case.
      HotKeySet("+{F8}", RunManually) Func RunManually() ;Use a case statement with 1 hot key and an InputBox to manually run functions $funcName = InputBox("Which Func to Run?", "Enter the name of the function to run") MsgBox(0, "Entered value", $funcName) ;If $funcName = testFunc Then testFunc() ;If $funcName = test2Func Then test2Func() Switch $funcName Case testFunc MsgBox(0, "Calling", "Calling Function") testFunc() MsgBox(0, "Called", "Function call finished") Case test2Func MsgBox(0, "Calling", "Calling Function") test2Func() MsgBox(0, "Called", "Function call finished") EndSwitch EndFunc While 1 ;testFunc() Sleep(1000) WEnd Func testFunc() MsgBox(0, "func running", "Seems to work!") EndFunc Func test2Func() MsgBox(0, "func 2 running", "2 Seems to work!") EndFunc Thanks!
    • By SaeidN
      I want to tell pixelsearch to search for red color shades in for example 5 different x,y,h,w of the screen. If color red was not found in these 5 area, then do something.
      I wrote this, but it's working only if the first case is not found. Is select a good choice? or it's better to use switch or if or something else?
      ٍEdit: all "do something"s are same function. (search for red color untill in these 5 areas, it couldnot find red color, then if it couldn't find red color, perform that 1 function)
      Select Case 1 $color1 = PixelSearch(67, 614, 77, 617, 0xE62121, 10) If @error Then do something... EndIf Case 2 $color2 = PixelSearch(165, 614, 175, 617, 0xE62121, 10) If @error Then do something... EndIf Case 3 $color3 = PixelSearch(265, 614, 275, 617, 0xE62121, 10) If @error Then do something... EndIf Case 4 $color4 = PixelSearch(365, 614, 375, 617, 0xE62121, 10) If @error Then do something... EndIf Case 5 $color5 = PixelSearch(465, 614, 475, 617, 0xE62121, 10) If @error Then do something... EndIf EndSelect  
    • By lgvlgv
      Hi all!
      I have a problem with creating the right varibles in the switch statment and wonder if some on can help me figure out the best way to procide?
      i got the part when i create all trayitems with a "FOR" statement from an ini file working, but i fail when it comes to the switch statement.
      ini file look like this..
      and so on...
      here is part of my script..
      it works for a few min.. then the hole application hangs (when compiled, seems to work in test mode, before i compile it?)
      is this part that seems to cose the error?
      Case Else If $Msg > 0 Then _DebugOut("While Statement - $Msg: " & $Msg) $aSearch = _ArraySearch($Linksarray, $gText, 0, 0, 0, 1, 1, 0) If @error Then Else $aSearch1 = _ArraySearch($Linksarray, $Linksarray[$aSearch][0], 0, 0, 0, 1, 1, 0) If @error Then Else _DebugOut("While Statement - $aSearch1: " & $Linksarray[$aSearch1][0]) _DebugOut("While Statement - $aSearch1: " & $Linksarray[$aSearch1][1]) ShellExecute($Linksarray[$aSearch][1], "", "", "open") EndIf EndIf EndIf here is both functions...
      Func _WinMain() _readConfigFile() Opt("TrayMenuMode", 11) Opt("GUIOnEventMode", 0) ;0=disabled, 1=OnEvent mode enabled _TrayInit() ; Main GUI Loop Local $tvHandle = 0 $iBegin = TimerInit() $LatencyUrl = IniRead($ConfigFile, "General", "LatencyUrl", "www.charger.se") $LatencyMS = IniRead($ConfigFile, "General", "LatencyMS", "500") HotKeySet("+!z", '_ShowITSupport') HotKeySet("+!x", '_ShowITSupport') While 1 $Msg = GUIGetMsg() $gText = _GetMenuText($Msg) If TimerDiff($iBegin) > 5000 Then ; Loop until 5 sec have passed _Ping($LatencyUrl, $LatencyMS) #cs Local $net_test = _IsInternetConnectedEx() ; ConsoleWrite("$net_test: " & $net_test & @CRLF) If $net_test = 1 Then Local $net_test2 = __GetConnectedAdapterIPMAC() If StringInStr($net_test2, "Wireless") Or StringInStr($net_test2, "Wired") Then EndIf Else ConsoleWrite("$net_test2? " & __GetConnectedAdapterIPMAC() & @CRLF) EndIf #ce ; ConsoleWrite("Counter: " & $iBegin & @CRLF) $iBegin = TimerInit() EndIf Switch $Msg Case $ToggleLogonUser _DebugOut("While Statement - $ToggleLogonUser: " & $gText) _ToggleLogonUser() Case $TrayTS _DebugOut("While Statement - $TrayTS: " & $gText) _DebugOut(@ScriptDir & "TeamViewerQS_sv-idcnnz7xxq.exe" & @CRLF) FileInstall("Res\TeamViewerQS_sv-idcnnz7xxq.exe", @ScriptDir & "\TeamViewerQS_sv-idcnnz7xxq.exe", 0) ShellExecute("TeamViewerQS_sv-idcnnz7xxq.exe", "", @ScriptDir, 'open') ; Sleep(10000) Case $ResetInternet _DebugOut("While Statement - $ResetInternet: " & $gText) $tmpNessage = MsgBox(20, "Warning", "You are about to do a complete Network reset!") If $tmpNessage = 6 Then _DebugOut("While Statement - $ResetInternet: MsgBox, Returns: " & $tmpNessage) Else _DebugOut("While Statement - $ResetInternet: MsgBox, Returns: " & $tmpNessage) EndIf Case $TrayExit _DebugOut("While Statement - $TrayExit: " & $gText) _TrayIconDelete($nTrayIcon1) ExitLoop Case Else #cs If $Msg > 0 Then _DebugOut("While Statement - $Msg: " & $Msg) $aSearch = _ArraySearch($Linksarray, $gText, 0, 0, 0, 1, 1, 0) If @error Then Else $aSearch1 = _ArraySearch($Linksarray, $Linksarray[$aSearch][0], 0, 0, 0, 1, 1, 0) If @error Then Else _DebugOut("While Statement - $aSearch1: " & $Linksarray[$aSearch1][0]) _DebugOut("While Statement - $aSearch1: " & $Linksarray[$aSearch1][1]) ShellExecute($Linksarray[$aSearch][1], "", "", "open") EndIf EndIf EndIf #ce EndSwitch Sleep(10) WEnd _TrayIconDelete($nTrayIcon1) EndFunc ;==>_WinMain Func _TrayInit() Opt('TrayMenuMode', 3) ; Default tray menu items (Script Paused/Exit) will not be shown. Opt('TrayAutoPause', 0) ; Script will not be paused when clicking the tray icon. ;Opt("TrayOnEventMode", 1) Opt('MustDeclareVars', 0) ; *** Create the tray icon *** ;Func _TrayIconCreate($sToolTip = "", $sIconFile = @AutoItExe, $nIconID = 0, $sCallback = "", $nMsg = 0, $hIcon = 0) ;TraySetIcon(@ScriptFullPath, "201") $nTrayIcon1 = _TrayIconCreate("Datornamn: " & @ComputerName & @CRLF & "IPAdress: " & @IPAddress1 & " ," & @IPAddress2, @ScriptFullPath, -1) _TrayIconSetClick(-1, 16) _TrayIconSetState() ; Show the tray icon ; *** Create the tray context menu *** Local $iIndex = _ArraySearch($GeneralArray, "Kund", 0, 0, 0, 1, 1, 0) $TrayMenuname = "ChaQ - " & $GeneralArray[$iIndex][1] $nTrayMenu1 = _TrayCreateContextMenu() ; is the same like _TrayCreateContextMenu(-1) or _TrayCreateContextMenu($nTrayIcon1) $nSideItem3 = _CreateSideMenu($nTrayMenu1) _SetSideMenuText($nSideItem3, $TrayMenuname) _SetSideMenuColor($nSideItem3, 0x00FFFF) ; yellow; default color - white _SetSideMenuBkColor($nSideItem3, 0x802222) ; bottom start color - dark blue _SetSideMenuBkGradColor($nSideItem3, 0x4477AA) ; top end color - orange _SetSideMenuImage($nSideItem3, "shell32.dll", 309, True) $TrayCase = _TrayCreateItem("SupportCenter, Add a Case") _TrayCreateItem("") _TrayItemSetIcon($TrayCase, @ScriptFullPath, -7) $TrayTS = _TrayCreateItem("Teamviwer") _TrayCreateItem("") _TrayItemSetIcon($TrayTS, @ScriptFullPath, -9) $MenuLinks = _TrayCreateMenu("Links") _TrayCreateItem("") _TrayItemSetIcon($MenuLinks, "shell32.dll", -44) $MenuTools = _TrayCreateMenu("Program") _TrayCreateItem("") _TrayItemSetIcon($MenuTools, "", 0) $chargeitem = _TrayCreateMenu("Tools") $chargeitem1 = _TrayCreateMenu("Settings", $chargeitem) _TrayCreateItem("") $TrayExit = _TrayCreateItem("Exit") _TrayItemSetIcon($TrayCase, @ScriptFullPath, -7) _TrayItemSetIcon($MenuLinks, "shell32.dll", -318) _TrayItemSetSelIcon($MenuLinks, "shell32.dll", -44) _TrayItemSetIcon($MenuTools, "shell32.dll", -20) _TrayItemSetSelIcon($MenuTools, "shell32.dll", -20) _TrayItemSetIcon($chargeitem, @ScriptFullPath, -11) _TrayItemSetSelIcon($chargeitem, @ScriptFullPath, -11) _TrayItemSetIcon($chargeitem1, @ScriptFullPath, -11) _TrayItemSetSelIcon($chargeitem1, @ScriptFullPath, -11) _TrayItemSetIcon($TrayHelp, "mstsc_101.ico", 0) _TrayItemSetIcon($TrayTS, @ScriptFullPath, -9) _TrayItemSetIcon($TrayExit, "shell32.dll", -28) ; *** Sub menu items *** For $x = 1 To $Linksarray[0][0] _DebugOut("$Linksarray " & $Linksarray[$x][0]) If StringInStr($Linksarray[$x][1], "http") Then $Tray_Item[$x] = _TrayCreateItem($Linksarray[$x][0], $MenuLinks) _TrayItemSetIcon($Tray_Item[$x], "shell32.dll", -14) Else $Tray_Item[$x] = _TrayCreateItem($Linksarray[$x][0], $MenuTools) _TrayItemSetIcon($Tray_Item[$x], $Linksarray[$x][1], "") EndIf Next $EnableBit = _TrayCreateItem("Enable (BIT/WSUS)", $chargeitem) $DisableBit = _TrayCreateItem("Disable (BIT/WSUS)", $chargeitem) $ResetInternet = _TrayCreateItem("Reset Internet Connection", $chargeitem) $ToggleLogonUser = _TrayCreateItem("Toggle Username/logon screen", $chargeitem) _TrayItemSetIcon($TrayTSSite, "shell32.dll", -14) _TrayItemSetIcon($TraySupportSite, "shell32.dll", -14) _TrayItemSetIcon($TraySupportSite1, "shell32.dll", -14) _TrayItemSetIcon($DisableBit, "shell32.dll", -275) _TrayItemSetIcon($EnableBit, "shell32.dll", -276) _TrayItemSetIcon($ResetInternet, @ScriptFullPath, -12) _TrayItemSetIcon($ToggleLogonUser, "regedit.exe", -1) GUICtrlSetState($chargeitem, $GUI_DISABLE) GUICtrlSetState($EnableBit, $GUI_DISABLE) GUICtrlSetState($DisableBit, $GUI_DISABLE) GUICtrlSetState($ResetInternet, $GUI_DISABLE) EndFunc ;==>_TrayInit  
  • Create New...