Sign in to follow this  
Followers 0
klynch1969

dynamic number of Switch-Case entries (how?)

4 posts in this topic

I am fairly new to AutoIT, but think I have a decent understanding. I have been working on creating a dynamically sized GUI based on an array taken from a text file. So far so good. But I am trying to figure out if it possible to have the Switch/Case checking work dynamically also. I only put in one static Case entry to show what response I need to generate.

#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>
#include <GuiConstants.au3>
#include <Array.au3>
#include <file.au3>

Opt('MustDeclareVars', 1)

Dim $NewArray ; the array that is read is always +1, because position zero is the number of array items in the file
If Not _FileReadToArray(@ScriptDir & "\system_command-array.txt",$NewArray) Then
   MsgBox(4096,"Error", " Error reading Array    error:" & @error)
   Exit
EndIf

Local $avArray1 = split1($NewArray,1) ; split off first half of array
Local $avArray2 = split1($NewArray,2) ; split off second half of array

Func split1(ByRef $NewArray,$j)
    Local $splitArray[2]
    Local $OneArray[UBound($NewArray)-1] ; set OneArray size
    For $i = 1 to UBound($NewArray)-1
        $splitArray = StringSplit(($NewArray[$i]), ",")
        $OneArray[$i-1] = $splitArray[$j]
    Next
    Return $OneArray
EndFunc

Dim $systemField[UBound($avArray1)]
Dim $commandField[UBound($avArray2)]
Dim $execute[UBound($avArray1)]
Dim $view[UBound($avArray1)]
Dim $checkMark[UBound($avArray1)]

Example()

Func Example()
    Local $msg
    Local $width = 600
    Local $gui
    Local $checks
    Local $sc
    
    $gui = GUICreate("GUI Dynamic Interface", $width, (UBound($avArray1)*20)+50)  ; dynamically create GUI height based on array size
    GUISetBkColor(0xDBDEFF)

    $checks = GUICreate("", 20, (UBound($avArray1)*15), 570, 37, $WS_POPUP, BitOR($WS_EX_LAYERED, $WS_EX_MDICHILD), $gui)
    GUISetBkColor(0xDBDEFF)
    
    For $i = 0 to UBound($avArray1) -1
        $checkMark[$i] = GUICtrlCreatePic(@ScriptDir & "\green_check_mark.gif", 0, $i*20, 0, 0)
        GUICtrlSetState($checkMark[$i],$GUI_HIDE)
    Next
    
    GUISetState(@SW_SHOW, $checks)
    GUISetState(@SW_SHOW, $gui)
    
    GUISetFont(8.5, 800, 0, "Arial")
    GUICtrlCreateLabel("System", 13, 25)
    GUISetFont(8.5, 400, 0, "Arial")
    
    GUISetFont(8.5, 800, 0, "Arial")
    GUICtrlCreateLabel("Command", 173, 25)
    GUISetFont(8.5, 400, 0, "Arial")
    
    For $i = 0 to UBound($avArray1) -1
        If StringInStr($avArray1[$i],"system") =0 Then
            $systemField[$i] = GUICtrlCreateInput($avArray1[$i], 10, $i*20+40, 150, 20)
            If StringInStr($avArray2[1], "command") Then
                GUICtrlSetState(-1, $GUI_HIDE)
            Else
                GUICtrlSetState(-1, $GUI_DISABLE)
            EndIf
            $commandField[$i] = GUICtrlCreateInput($avArray2[$i], 170, $i*20+40, 280, 20)
            If StringInStr($avArray2[1], "command") Then
                GUICtrlSetState(-1, $GUI_HIDE)
            Else
                GUICtrlSetState(-1, $GUI_DISABLE)
            EndIf
            $execute[$i] = GUICtrlCreateButton("Execute", 470, $i*20+40, 55, 20)
            If StringInStr($avArray2[1], "command") Then
                GUICtrlSetState(-1, $GUI_HIDE)
            EndIf
            $view[$i] = GUICtrlCreateButton("View", 530, $i*20+40, 35, 20)
            If StringInStr($avArray2[1], "command") Then
                GUICtrlSetState(-1, $GUI_HIDE)
            Else
                GUICtrlSetState(-1, $GUI_DISABLE)
            EndIf
        EndIf
    Next

    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                Exit
            Case $execute[0]
                $sc = 0
                If $avArray2[$sc] = "pcAnywhere connection" Then
                    MsgBox(0,"","pcAnywhere connection")
                    GUICtrlSetState($checkMark[$sc],$GUI_SHOW)
                    GUICtrlSetState($view[$sc],$GUI_ENABLE)
                ElseIf StringInStr($avArray2[$sc], "command") Then
                    Msgbox(0,"","For future use")
                Else
                    IF WinExists("[CLASS:PuTTY]") Then
                        MsgBox(0,"","[CLASS:PuTTY]")
                        GUICtrlSetState($checkMark[$sc],$GUI_SHOW)
                        GUICtrlSetState($view[$sc],$GUI_ENABLE)
                    EndIf
                EndIf
            Case $view[0]
                $sc = 0
                If $avArray2[$sc] = "pcAnywhere connection" Then
                    MsgBox(0,"","pcAnywhere connection")
                ElseIf StringInStr($avArray2[$sc], "command") Then
                    Msgbox(0,"","For future use")
                Else
                    IF WinExists("[CLASS:PuTTY]") Then
                        MsgBox(0,"","[CLASS:PuTTY]")
                    EndIf
                EndIf
        EndSwitch
    WEnd
EndFunc

Do I have to create $execute[1], $view[1], $execute[2], $view[2], etc. or is there another way so I don't have so much redundency. What if I have 100 array items in the text file?

This is what is in my text file at this time

server1.mydomain.com,pcanywhere connection
server2.mydomain.com,pcanywhere connection
server3.mydomain.com,pcanywhere connection
server4.mydomain.com,pcanywhere connection
server5.mydomain.com,pcanywhere connection
server6.mydomain.com,pcanywhere connection
server7.mydomain.com,pcanywhere connection
server8.mydomain.com,pcanywhere connection
server9.mydomain.com,pcanywhere connection
server10.mydomain.com,pcanywhere connection
server11.mydomain.com,pcanywhere connection

Share this post


Link to post
Share on other sites



klynch1969,

If you intend to do exactly the same thing when an "Execute" or "View" button is pressed, other than using the contents of the different inputs, then there is a little trick you can use.

Change your GUI creation code to make sure all the "Execute" buttons are created in IMMEDIATE succession, followed by the "View" buttons. They will then have successive ControlIDs which you can test for in your loop. (Note: This will be true if you are creating the controls at the start of your script - if you have already deleted some controls and are creating more, then all bets are off! :) )

Here is a example of what I mean - I have changed the button creation code, but only changed the loop for the "Execute" buttons:

#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>
#include <GuiConstants.au3>
#include <Array.au3>
#include <file.au3>

Opt('MustDeclareVars', 1)

Dim $NewArray ; the array that is read is always +1, because position zero is the number of array items in the file
If Not _FileReadToArray(@ScriptDir & "\system_command-array.txt",$NewArray) Then
   MsgBox(4096,"Error", " Error reading Array    error:" & @error)
   Exit
EndIf

Local $avArray1 = split1($NewArray,1) ; split off first half of array
Local $avArray2 = split1($NewArray,2) ; split off second half of array

Func split1(ByRef $NewArray,$j)
    Local $splitArray[2]
    Local $OneArray[UBound($NewArray)-1] ; set OneArray size
    For $i = 1 to UBound($NewArray)-1
        $splitArray = StringSplit(($NewArray[$i]), ",")
        $OneArray[$i-1] = $splitArray[$j]
    Next
    Return $OneArray
EndFunc

Dim $systemField[UBound($avArray1)]
Dim $commandField[UBound($avArray2)]
Dim $execute[UBound($avArray1)]
Dim $view[UBound($avArray1)]
Dim $checkMark[UBound($avArray1)]

Example()

Func Example()
    Local $msg
    Local $width = 600
    Local $gui
    Local $checks
    Local $sc

    $gui = GUICreate("GUI Dynamic Interface", $width, (UBound($avArray1)*20)+50)  ; dynamically create GUI height based on array size
    GUISetBkColor(0xDBDEFF)

    $checks = GUICreate("", 20, (UBound($avArray1)*15), 570, 37, $WS_POPUP, BitOR($WS_EX_LAYERED, $WS_EX_MDICHILD), $gui)
    GUISetBkColor(0xDBDEFF)

    For $i = 0 to UBound($avArray1) -1
        $checkMark[$i] = GUICtrlCreatePic(@ScriptDir & "\green_check_mark.gif", 0, $i*20, 0, 0)
        GUICtrlSetState($checkMark[$i],$GUI_HIDE)
    Next

    GUISetState(@SW_SHOW, $checks)
    GUISetState(@SW_SHOW, $gui)

    GUISetFont(8.5, 800, 0, "Arial")
    GUICtrlCreateLabel("System", 13, 25)
    GUISetFont(8.5, 400, 0, "Arial")

    GUISetFont(8.5, 800, 0, "Arial")
    GUICtrlCreateLabel("Command", 173, 25)
    GUISetFont(8.5, 400, 0, "Arial")

    For $i = 0 to UBound($avArray1) -1
        If StringInStr($avArray1[$i],"system") =0 Then
            $systemField[$i] = GUICtrlCreateInput($avArray1[$i], 10, $i*20+40, 150, 20)
            If StringInStr($avArray2[1], "command") Then
                GUICtrlSetState(-1, $GUI_HIDE)
            Else
                GUICtrlSetState(-1, $GUI_DISABLE)
            EndIf
            $commandField[$i] = GUICtrlCreateInput($avArray2[$i], 170, $i*20+40, 280, 20)
            If StringInStr($avArray2[1], "command") Then
                GUICtrlSetState(-1, $GUI_HIDE)
            Else
                GUICtrlSetState(-1, $GUI_DISABLE)
            EndIf
        EndIf
    Next

    For $i = 0 to UBound($avArray1) -1
        If StringInStr($avArray1[$i],"system") =0 Then
            $execute[$i] = GUICtrlCreateButton("Execute", 470, $i*20+40, 55, 20)
            If StringInStr($avArray2[1], "command") Then
                GUICtrlSetState(-1, $GUI_HIDE)
            EndIf
        EndIf
    Next

    For $i = 0 to UBound($avArray1) -1
        If StringInStr($avArray1[$i],"system") =0 Then
            $view[$i] = GUICtrlCreateButton("View", 530, $i*20+40, 35, 20)
            If StringInStr($avArray2[1], "command") Then
                GUICtrlSetState(-1, $GUI_HIDE)
            Else
                GUICtrlSetState(-1, $GUI_DISABLE)
            EndIf
        EndIf
    Next

    While 1
        Global $iMsg = GUIGetMsg()
        Switch $iMsg
            Case $GUI_EVENT_CLOSE
                Exit
            Case $execute[0] To $execute[UBound($avArray1) - 1]
                Global $iIndex = $iMsg - $execute[0]

                ConsoleWrite($iIndex & @CRLF)

                $sc = 0
                If $avArray2[$sc] = "pcAnywhere connection" Then
                    MsgBox(0,"","pcAnywhere connection")
                    GUICtrlSetState($checkMark[$sc],$GUI_SHOW)
                    GUICtrlSetState($view[$sc],$GUI_ENABLE)
                ElseIf StringInStr($avArray2[$sc], "command") Then
                    Msgbox(0,"","For future use")
                Else
                    IF WinExists("[CLASS:PuTTY]") Then
                        MsgBox(0,"","[CLASS:PuTTY]")
                        GUICtrlSetState($checkMark[$sc],$GUI_SHOW)
                        GUICtrlSetState($view[$sc],$GUI_ENABLE)
                    EndIf
                EndIf
            Case $view[0]
                $sc = 0
                If $avArray2[$sc] = "pcAnywhere connection" Then
                    MsgBox(0,"","pcAnywhere connection")
                ElseIf StringInStr($avArray2[$sc], "command") Then
                    Msgbox(0,"","For future use")
                Else
                    IF WinExists("[CLASS:PuTTY]") Then
                        MsgBox(0,"","[CLASS:PuTTY]")
                    EndIf
                EndIf
        EndSwitch
    WEnd
EndFunc

You can use the $iIndex variable to pull whatever information you need from the various arrays into the code that will be executed.

I hope that is a help. :(

M23.

P.S. Try to avoid Dim. it is better to use Global or Local and specify the scope you require, rather than relying on Autoit to make its best guess! :)


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Melba23,

ABSOLUTELY BRILLIANT!!! I understand what you mean about seperating the execute and view buttons for successive ControlIDs. I really like that. And with some minor modifications to the Switch/Case responses I was able to get it to work perfectly with the execute and view buttons. This is going to save thousands of lines of code (In another program I wrote.)

Again, Thanks.

Here is the final code.

#include <WindowsConstants.au3>
#include <GUIConstantsEx.au3>
#include <GuiConstants.au3>
#include <Array.au3>
#include <file.au3>

Opt('MustDeclareVars', 1)

Local $NewArray ; the array that is read is always +1, because position zero is the number of array items in the file
If Not _FileReadToArray(@ScriptDir & "\system_command-array.txt",$NewArray) Then
   MsgBox(4096,"Error", " Error reading Array    error:" & @error)
   Exit
EndIf

Local $avArray1 = split1($NewArray,1) ; split off first half of array
Local $avArray2 = split1($NewArray,2) ; split off second half of array

Func split1(ByRef $NewArray,$j)
    Local $splitArray[2]
    Local $OneArray[UBound($NewArray)-1] ; set OneArray size
    For $i = 1 to UBound($NewArray)-1
        $splitArray = StringSplit(($NewArray[$i]), ",")
        $OneArray[$i-1] = $splitArray[$j]
    Next
    Return $OneArray
EndFunc

Global $systemField[UBound($avArray1)]
Global $commandField[UBound($avArray2)]
Global $execute[UBound($avArray1)]
Global $view[UBound($avArray1)]
Global $checkMark[UBound($avArray1)]

Example()

Func Example()
    Local $msg
    Local $width = 600
    Local $gui
    Local $checks
    Local $sc

    $gui = GUICreate("GUI Dynamic Interface", $width, (UBound($avArray1)*20)+50)  ; dynamically create GUI height based on array size
    GUISetBkColor(0xDBDEFF)

    $checks = GUICreate("", 20, (UBound($avArray1)*20), 570, 37, $WS_POPUP, BitOR($WS_EX_LAYERED, $WS_EX_MDICHILD), $gui)
    GUISetBkColor(0xDBDEFF)

    For $i = 0 to UBound($avArray1) -1
        $checkMark[$i] = GUICtrlCreatePic(@ScriptDir & "\green_check_mark.gif", 0, $i*20, 0, 0)
        GUICtrlSetState($checkMark[$i],$GUI_HIDE)
    Next

    GUISetState(@SW_SHOW, $checks)
    GUISetState(@SW_SHOW, $gui)

    GUISetFont(8.5, 800, 0, "Arial")
    GUICtrlCreateLabel("System", 13, 25)
    GUISetFont(8.5, 400, 0, "Arial")

    GUISetFont(8.5, 800, 0, "Arial")
    GUICtrlCreateLabel("Command", 173, 25)
    GUISetFont(8.5, 400, 0, "Arial")

    For $i = 0 to UBound($avArray1) -1
        If StringInStr($avArray1[$i],"system") =0 Then
            $systemField[$i] = GUICtrlCreateInput($avArray1[$i], 10, $i*20+40, 150, 20)
            If StringInStr($avArray2[1], "command") Then
                GUICtrlSetState(-1, $GUI_HIDE)
            Else
                GUICtrlSetState(-1, $GUI_DISABLE)
            EndIf
            $commandField[$i] = GUICtrlCreateInput($avArray2[$i], 170, $i*20+40, 280, 20)
            If StringInStr($avArray2[1], "command") Then
                GUICtrlSetState(-1, $GUI_HIDE)
            Else
                GUICtrlSetState(-1, $GUI_DISABLE)
            EndIf
        EndIf
    Next

    For $i = 0 to UBound($avArray1) -1
        If StringInStr($avArray1[$i],"system") =0 Then
            $execute[$i] = GUICtrlCreateButton("Execute", 470, $i*20+40, 55, 20)
            If StringInStr($avArray2[1], "command") Then
                GUICtrlSetState(-1, $GUI_HIDE)
            EndIf
        EndIf
    Next

    For $i = 0 to UBound($avArray1) -1
        If StringInStr($avArray1[$i],"system") =0 Then
            $view[$i] = GUICtrlCreateButton("View", 530, $i*20+40, 35, 20)
            If StringInStr($avArray2[1], "command") Then
                GUICtrlSetState(-1, $GUI_HIDE)
            Else
                GUICtrlSetState(-1, $GUI_DISABLE)
            EndIf
        EndIf
    Next

    While 1
        Global $iMsg = GUIGetMsg()
        Switch $iMsg
            Case $GUI_EVENT_CLOSE
                Exit
            Case $execute[0] To $execute[UBound($avArray1) - 1]
                Global $iIndex = $iMsg - $execute[0]

                ConsoleWrite($iIndex & @CRLF)

                If $avArray2[$iIndex] = "pcAnywhere connection" Then
                    MsgBox(0,"","pcAnywhere connection")
                    GUICtrlSetState($checkMark[$iIndex],$GUI_SHOW)
                    GUICtrlSetState($view[$iIndex],$GUI_ENABLE)
                ElseIf StringInStr($avArray2[$iIndex], "command") Then
                    Msgbox(0,"","For future use")
                Else
                    IF WinExists("[CLASS:PuTTY]") Then
                        MsgBox(0,"","[CLASS:PuTTY]")
                        GUICtrlSetState($checkMark[$iIndex],$GUI_SHOW)
                        GUICtrlSetState($view[$iIndex],$GUI_ENABLE)
                    EndIf
                EndIf
            Case $view[0] To $view[UBound($avArray2) - 1]
                Global $iIndex = $iMsg - $view[0]

                ConsoleWrite($iIndex & @CRLF)

                If $avArray2[$iIndex] = "pcAnywhere connection" Then
                    MsgBox(0,"","pcAnywhere connection")
                ElseIf StringInStr($avArray2[$iIndex], "command") Then
                    Msgbox(0,"","For future use")
                Else
                    IF WinExists("[CLASS:PuTTY]") Then
                        MsgBox(0,"","[CLASS:PuTTY]")
                    EndIf
                EndIf
        EndSwitch
    WEnd
EndFunc
Edited by klynch1969

Share this post


Link to post
Share on other sites

klynch1969,

ABSOLUTELY BRILLIANT!!!

The developers, not me. :(

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

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  
Followers 0