Jump to content

Switch-Case Variable number of CASE


Recommended Posts

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 CASE

Here'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

  • Moderators

OhBobSaget,

Do we assume that $Array holds the same number of elements as there are items within the tray menu?

M23

Edit: 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 by Melba23

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:

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

 

Link to comment
Share on other sites

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 FTP

Now 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

  • Moderators

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



WEnd

I used this file:

FTPHost_0
FTPHost_1
FTPHost_2
FTPHost_3

How is that?

M23

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:

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

 

Link to comment
Share on other sites

  • Moderators

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

WEnd

M23

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:

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

 

Link to comment
Share on other sites

Version v.3.3.14.0

Great 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 by OhBobSaget
This was the version of SciTE
Link to comment
Share on other sites

  • Moderators

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

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:

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

 

Link to comment
Share on other sites

  • Moderators

OhBobSaget,

Great - glad I could help.

M23

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:

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

 

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...