OhBobSaget Posted August 21, 2015 Share Posted August 21, 2015 Hi,This is a general programming question maybe not related to AutoIT specifically.I would like to know how to do a Switch Case with a variable number of CASEHere's the thing i would prefer not to do ( Create multiple Switch Case for each size ) :If UBound($Array) = 2 Then Switch TrayGetMsg() Case $DepotDocFTP[0] Blahblah.. Case $DepotDocFTP[1] Blahblah.. EndSwitch ElseIf UBound($Array) = 3 Then Switch TrayGetMsg() Case $DepotDocFTP[0] Blahblah.. Case $DepotDocFTP[1] Blahblah.. Case $DepotDocFTP[2] Blahblah.. EndSwitch ElseIf UBound($Array) = 4 Then Switch TrayGetMsg() Case $DepotDocFTP[0] Blahblah.. Case $DepotDocFTP[1] Blahblah.. Case $DepotDocFTP[2] Blahblah.. Case $DepotDocFTP[3] Blahblah.. EndSwitch EndIf Etc...So is there a way around this ?Thank you Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted August 21, 2015 Moderators Share Posted August 21, 2015 (edited) OhBobSaget,Do we assume that $Array holds the same number of elements as there are items within the tray menu?M23Edit: Perhaps something along these lines?Opt("TrayMenuMode", 3) ; Default tray menu items will not be shown. $iCount = Number(InputBox("Number", "Choose how many")) Switch $iCount Case 1 To 5 Local $aArray[$iCount], $aDepotDocFTP[$iCount + 1] For $i = 0 To $iCount - 1 $aArray[$i] = $i $aDepotDocFTP[$i] = TrayCreateItem("FTP " & $i) Next TrayCreateItem("") $aDepotDocFTP[$i] = TrayCreateItem("Exit") EndSwitch While 1 $iTrayEvent = TrayGetMsg() For $i = 0 To UBound($aDepotDocFTP) - 2 If $iTrayEvent = $aDepotDocFTP[$i] Then ConsoleWrite("FTP " & $i & @CRLF) ExitLoop EndIf Next If $iTrayEvent = $aDepotDocFTP[UBound($aDepotDocFTP) - 1] Then Exit EndIf WEnd Edited August 21, 2015 by Melba23 OhBobSaget 1 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: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
OhBobSaget Posted August 21, 2015 Author Share Posted August 21, 2015 Thank you MELBA23 Here's a summary of what i am trying to do.I will have the program read a ini file to get the FTP names to connect to. Here's the first part (something like this) :$fFTP = FileOpen(@ScriptDir & "\FTPHosts.ini") $lFTPList = FileReadToArray($fFTP) FileClose($fFTP) TrayCreateItem("") For $i=0 to UBound($lFTPList)-1 $TrayItemFTPHosts[$i] = TrayCreateItem($lFTPList[$i]) Next TrayCreateItem("") Local $idExit = TrayCreateItem("Quit") TraySetState($TRAY_ICONSTATE_SHOW) TraySetToolTip("Connect FTP Manager")With this i get a tray Menu with a variable number of items.Next, i will use FileOpenDialog to ask the user for a file to deposit on the selected FTPNow i need to connect to the FTP host chosen in the tray menu. I used some of your code but it does not do what i need ( It display FTP 5 in the console then exit the program ) :While 1 $iTrayEvent = TrayGetMsg() For $i = 0 To UBound($DepotDocFTP) - 2 If $iTrayEvent = $DepotDocFTP[$i] Then ConsoleWrite("FTP " & $i & @CRLF) ExitLoop EndIf Next If $iTrayEvent = $DepotDocFTP[UBound($DepotDocFTP) - 1] Then Exit EndIf WEnd I will try to find out why it quits...i tried removing the ExitLoop feature but now it shows FTP 5, FTP 6, FTP 7, FTP 8 then it quits the program. Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted August 21, 2015 Moderators Share Posted August 21, 2015 OhBobSaget,Try this combined version:#include <MsgBoxConstants.au3> Opt("TrayMenuMode", 3) ; Default tray menu items will not be shown. ; Read fileinto an array - no need to open/close it $lFTPList = FileReadToArray(@ScriptDir & "\FTPHosts.ini") ; Create array to hold trayitem ControlIDs Global $TrayItemFTPHosts[@extended] ; Create the tray items For $i = 0 To UBound($lFTPList) - 1 $TrayItemFTPHosts[$i] = TrayCreateItem($lFTPList[$i]) Next TrayCreateItem("") Local $idExit = TrayCreateItem("Quit") While 1 $iTrayEvent = TrayGetMsg() ; Look to see if it is an FTP item For $i = 0 To UBound($TrayItemFTPHosts) - 2 If $iTrayEvent = $TrayItemFTPHosts[$i] Then MsgBox($MB_SYSTEMMODAL, "Selected", $lFTPList[$i]) ExitLoop EndIf Next ; Check for exit If $iTrayEvent = $idExit Then Exit EndIf WEndI used this file:FTPHost_0 FTPHost_1 FTPHost_2 FTPHost_3How is that?M23 OhBobSaget 1 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: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
OhBobSaget Posted August 21, 2015 Author Share Posted August 21, 2015 Excellent ! The only problem left is the @extended variable that is causing a problem->Array variable has incorrect number of subscripts or subscript dimension range exceeded.: Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted August 21, 2015 Moderators Share Posted August 21, 2015 OhBobSaget,That does not happen to me - what AutoIt version do you run?Try this version instead - it reads the size directly:#include <MsgBoxConstants.au3> Opt("TrayMenuMode", 3) ; Default tray menu items will not be shown. ; Read fileinto an array - no need to open/close it $lFTPList = FileReadToArray(@ScriptDir & "\FTPHosts.ini") ; Create array to hold trayitem ControlIDs Global $TrayItemFTPHosts[UBound($lFTPList)] ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ; Create the tray items For $i = 0 To UBound($lFTPList) - 1 $TrayItemFTPHosts[$i] = TrayCreateItem($lFTPList[$i]) Next TrayCreateItem("") Local $idExit = TrayCreateItem("Quit") While 1 $iTrayEvent = TrayGetMsg() ; Look to see if it is an FTP item For $i = 0 To UBound($TrayItemFTPHosts) - 1 ; And this needs to be changed too, sorry <<<<<<<<<<<<<<<<<<<<< If $iTrayEvent = $TrayItemFTPHosts[$i] Then MsgBox($MB_SYSTEMMODAL, "Selected", $lFTPList[$i]) ExitLoop EndIf Next ; Check for exit If $iTrayEvent = $idExit Then Exit EndIf WEndM23 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: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
OhBobSaget Posted August 21, 2015 Author Share Posted August 21, 2015 (edited) Version v.3.3.14.0Great idea to read the file first and use the number of lines to create the other array Thank you ! But i would be interested to know how to use the @extended if it works, might be handy for the future Edited August 21, 2015 by OhBobSaget This was the version of SciTE Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted August 21, 2015 Moderators Share Posted August 21, 2015 OhBobSaget,As explained in the Help file, FileReadToArray returns the number of lines read in @extended - which Is why I used it to size the array, and wondered if you used an much earlier version. But as you use 3.3.14.0 I am at a loss as to why it does not work for you too. However, you have your script working and that is the main thing.M23 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: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
OhBobSaget Posted August 21, 2015 Author Share Posted August 21, 2015 It does work now with @extended with the FileReadToArray before the declaration of the next array.Thank you Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted August 21, 2015 Moderators Share Posted August 21, 2015 OhBobSaget,Great - glad I could help.M23 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: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now