Jump to content

Looping a switch statement?


Recommended Posts

Hello everyone,  hope you are having a great day. I have been trying to figure this out for awhile, but no luck yet so I thought I would ask for some help. Any help is much appreciated. I have a For Loop that creates GUICtrlCreateButton and adds them to array ($enclaveLessButtons). This works great, but the issue I am having now is working with the Switch statement and GUIGetMsg(). If I manual set the switch statement using $enclaveLessButtons[1], $enclaveLessButtons[2]. etc... it works. I can capture the button being clicked and perform whatever action I need. The thing is the array size of the $enclaveLessButtons can change at anytime and I really do not want to write every single Case statement for each variable in the array. How can I loop the Case statements using the $enclaveLessButtons array.   The entire code is below, if you look at the While loop at the bottom, you can see a couple of Case examples I wrote out. This code will not run without other .au3 scripts I made that hold various functions, etc... Let me know if you have any questions and thank you for your time.

#include "lib/load.au3"

$enclaveURL = 'https://github.com/antfuentes87/enclave/tree/master/less'
$enclaveResponseText = _HTTP_ResponseText($enclaveURL)
$enclaveStartString = '<a href="'
$enclaveEndString = '"'
$enclaveStringInStr = 'blob'

$enclaveFiles = _HTTP_ResponseTextStringBetweenArray($enclaveResponseText, $enclaveStartString, $enclaveEndString)
$enclaveFiles =  _Array_StringInStr($enclaveFiles, $enclaveStringInStr)

Dim $enclaveLessButtons[1]
Dim $enclaveLessContentArray[1]

enclave()

Func enclave()
   GUICreate("Enclave", 1400, 400)

   GUISetBkColor(0x00E0FFFF)
   GUISetFont(9, 300)

   GUICtrlCreateTab(0, 0, 1400, 300)

   For $enclaveFile In $enclaveFiles
      $enclaveFileUrl = $enclaveFile
      $enclaveFile = _StringExplode($enclaveFile, '/')
      $enclaveFileArraySize = UBound($enclaveFile)-1
      $enclaveFile = $enclaveFile[$enclaveFileArraySize]
      If $enclaveFile <> 'enclave.less' And $enclaveFile <> 'vars.less' Then
         GUICtrlCreateTabItem($enclaveFile)
         $enclaveLessUrl = 'https://raw.githubusercontent.com' & $enclaveFileUrl
         $enclaveLessUrl = StringReplace($enclaveLessUrl, "blob/", "")
         $enclaveLessResponseText = _HTTP_ResponseText($enclaveLessUrl)
         $enclaveLessStartString = '.enc-'
         $enclaveLessEndString = ')'
         $enclaveLess = _HTTP_ResponseTextStringBetweenArray($enclaveLessResponseText, $enclaveLessStartString, $enclaveLessEndString)
         If IsArray($enclaveLess) Then
            $x = 10
            $y = 26
            For $enclaveLessContent In $enclaveLess
               $enclaveLessContentOrg = $enclaveLessContent
               $enclaveLessContent = _StringExplode($enclaveLessContent, "(")
               _ArrayAdd($enclaveLessButtons, GUICtrlCreateButton($enclaveLessContent[0], $x, $y, 200, 25))
               _ArrayAdd($enclaveLessContentArray, $enclaveLessContentOrg)
               $y = $y + 26
            Next
         EndIf
      EndIf
   Next
   GUICtrlCreateTabItem("")

   GUISetState(@SW_SHOW)

   Local $idMsg
   While 1
      Switch GUIGetMsg()
         Case $enclaveLessButtons[1]
            MsgBox(1, "Content", $enclaveLessContentArray[1])
         Case $enclaveLessButtons[2]
            MsgBox(1, "Content", $enclaveLessContentArray[2])
         Case $enclaveLessButtons[3]
            MsgBox(1, "Content", $enclaveLessContentArray[3])
         Case $enclaveLessButtons[4]
            MsgBox(1, "Content", $enclaveLessContentArray[4])
         Case $GUI_EVENT_CLOSE, $idClose
            ExitLoop
      EndSwitch
   WEnd
EndFunc

 

Link to comment
Share on other sites

  • Moderators

antfuentes87,

In future, posting runnable code would be a good idea if you require help - a short reproducer script is probably best.

What I would normally do in the case of an indeterminate number of controls is to save their ControlIDs to an array and then loop through that array to check if GUIGetMsg has returned an event from one of them. Once you have the index of the control in the array you can easily arrange for other functions to run. Give me a moment and I will provide an example script.

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

Another solution that you could use is Dummy controls.

Here is an example:

GUICreate("test")
Local $enclaveLessButtons[16], $x = 25, $y = 25
Local $iStart = GUICtrlCreateDummy()
For $i = 1 To 15 Step 1
    $enclaveLessButtons[$i] = GUICtrlCreateButton($i, $x, $y)
    $x += 25
    $y += 25
Next
Local $iEnd = GUICtrlCreateDummy()
GUISetState()
While 1
    Switch GUIGetMsg()
        Case $iStart To $iEnd
            MsgBox(0, "", "hi")
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd

 

Snips & Scripts


My Snips: graphCPUTemp ~ getENVvars
My Scripts: Short-Order Encrypter - message and file encryption V1.6.1 ~ AuPad - Notepad written entirely in AutoIt V1.9.4

Feel free to use any of my code for your own use.                                                                                                                                                           Forum FAQ

 

Link to comment
Share on other sites

  • Moderators

antfuentes87,

Here you go:

#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>

While 1
    $iCount = InputBox("How Many?", "Number of controls ( 1 To 5)")
    Switch $iCount
        Case ""
            Exit
        Case 1 To 5
            ExitLoop
    EndSwitch
WEnd

Global $aButton[$iCount + 1] = [$iCount]

$hGUI = GUICreate("Test", 500, 500)

For $i = 1 To $iCount
        $aButton[$i] = GUICtrlCreateButton("Button " & $i, 10, ($i * 50) - 40, 80, 30)
Next

GUISetState()

While 1
    $iMsg = GUIGetMsg()
    Switch $iMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case Else
            For $i = 1 To $iCount
                If $iMsg = $aButton[$i] Then
                    MsgBox($MB_SYSTEMMODAL, "Pressed", "Button " & $i)
                    ExitLoop
                EndIf
            Next
    EndSwitch
WEnd

Please ask if you have any questions.

M23

Edited by Melba23
Fixed code formatting

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 both! I was able to come up with something from both of your examples. Thank you so much, on to the next task! If any of you guys like LESS, you can check out my LESS framework I am building @ https://github.com/antfuentes87/enclave This is what the AutoIt program is interacting with. Thanks again! Next time I will try to make a better example that works, it just has so many functions, I was unsure how I could include everything to make it work.

#include "lib/load.au3"

$enclaveURL = 'https://github.com/antfuentes87/enclave/tree/master/less'
$enclaveResponseText = _HTTP_ResponseText($enclaveURL)
$enclaveStartString = '<a href="'
$enclaveEndString = '"'
$enclaveStringInStr = 'blob'

$enclaveFiles = _HTTP_ResponseTextStringBetweenArray($enclaveResponseText, $enclaveStartString, $enclaveEndString)
$enclaveFiles =  _Array_StringInStr($enclaveFiles, $enclaveStringInStr)

Dim $enclaveLessButtons[1]
Dim $enclaveLessContentArray[1]
Dim $enclaveLessArray[1]

enclave()

Func enclave()
   GUICreate("Enclave", 1100, 600, -1, -1, $WS_BORDER, $WS_EX_TOPMOST)

   GUISetBkColor(0x00E0FFFF)
   GUISetFont(9, 300)

   GUICtrlCreateTab(0, 0, 1100, 600)

   For $enclaveFile In $enclaveFiles
      $enclaveFileUrl = $enclaveFile
      $enclaveFile = _StringExplode($enclaveFile, '/')
      $enclaveFileArraySize = UBound($enclaveFile)-1
      $enclaveFile = $enclaveFile[$enclaveFileArraySize]
      If $enclaveFile <> 'enclave.less' And $enclaveFile <> 'vars.less' Then
         GUICtrlCreateTabItem($enclaveFile)
         $enclaveLessUrl = 'https://raw.githubusercontent.com' & $enclaveFileUrl
         $enclaveLessUrl = StringReplace($enclaveLessUrl, "blob/", "")
         $enclaveLessResponseText = _HTTP_ResponseText($enclaveLessUrl)
         $enclaveLessStartString = '.enc-'
         $enclaveLessEndString = ')'
         $enclaveLess = _HTTP_ResponseTextStringBetweenArray($enclaveLessResponseText, $enclaveLessStartString, $enclaveLessEndString)
         _ArrayAdd($enclaveLessArray, $enclaveLess)
         If IsArray($enclaveLess) Then
            $x = 10
            $y = 26
            If $enclaveFile = 'button.less' Then
               For $enclaveLessContent In $enclaveLess
                  $enclaveLessContentOrg = $enclaveLessContent
                  $enclaveLessContent = _StringExplode($enclaveLessContent, "(")
                  If $enclaveLessContent[0] <> 'flex-auto' And $enclaveLessContent[0] <> 'create-border' And $enclaveLessContent[0] <> 'background-rgb' Then
                     _ArrayAdd($enclaveLessButtons, GUICtrlCreateButton('enc-' & $enclaveLessContent[0], $x, $y, 200, 25))
                     _ArrayAdd($enclaveLessContentArray, $enclaveLessContentOrg)
                  $y = $y + 26
                  EndIf
               Next
            Else
               For $enclaveLessContent In $enclaveLess
                  $enclaveLessContentOrg = $enclaveLessContent
                  $enclaveLessContent = _StringExplode($enclaveLessContent, "(")
                  _ArrayAdd($enclaveLessButtons, GUICtrlCreateButton('enc-' & $enclaveLessContent[0], $x, $y, 200, 25))
                  _ArrayAdd($enclaveLessContentArray, $enclaveLessContentOrg)
                  $y = $y + 26
               Next
            EndIf
         EndIf
      EndIf
   Next

   $enclaveLessButtonsCount = UBound($enclaveLessButtons, 1) - 1

   GUICtrlCreateTabItem("")

   GUISetState(@SW_SHOW)
   While 1
      $guiGetMsg = GUIGetMsg()
      Switch $guiGetMsg
         Case $GUI_EVENT_CLOSE
            ExitLoop
         Case Else
            For $i = 1 to $enclaveLessButtonsCount Step + 1
               If $guiGetMsg = $enclaveLessButtons[$i] Then
                  ;WinActivate("[CLASS:PX_WINDOW_CLASS]", "")
                  ;Send('.enc-' & $enclaveLessContentArray[$i] & ');')
                  ClipPut('.enc-' & $enclaveLessContentArray[$i] & ');')
                  ExitLoop
               EndIf
            Next
      EndSwitch
   WEnd
EndFunc

 

Edited by antfuentes87
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...