Jump to content

[solved] How to interact with dynamically created buttons?


NDog
 Share

Recommended Posts

Hi there.

My incomplete GUI below I am trying to generate a list of exe files in a given folder then test each one with a different argument for the purpose of generating a silent batch file.

At the moment my question is how do I make the generated 'test' buttons run a command like such $exe & " /" & $switch

Once I know this, I should be able to figure out how to delete unwanted rows, and then how to reorder the rows, but for now, one question at a time.

Thanks for your advice and input

; -- Created with ISN Form Studio 2 for ISN AutoIt Studio -- ;
#include <StaticConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#Include <GuiButton.au3>
#include <ComboConstants.au3>

#include <File.au3>

; Main GUI header
$hGUI     = GUICreate("Silent Batch Maker",838,569,-1,-1,-1,-1)
$idBrowse = GUICtrlCreateButton("Browse",9,15,100,30,-1,-1)
$idSave   = GUICtrlCreateButton("Save",622,15,100,30,-1,-1)
$idLoad   = GUICtrlCreateButton("Load",728,15,100,30,-1,-1)
$lbFolder = GUICtrlCreateLabel("Browse for a folder",120,15,487,28,$SS_CENTERIMAGE,-1)
GUICtrlSetBkColor(-1,"-2")

; First row
GUICtrlCreateLabel("[1]",9,60,26,22,BitOr($SS_CENTER,$SS_CENTERIMAGE),$WS_EX_CLIENTEDGE)
GUICtrlSetState(-1,BitOr($GUI_CHECKED,$GUI_SHOW,$GUI_ENABLE))
GUICtrlSetFont(-1,10,400,0,"MS Sans Serif")
GUICtrlSetBkColor(-1,"-2")

GUICtrlCreateButton("Up",35,60,39,21,-1,-1)
GUICtrlCreateButton("Down",74,60,39,21,-1,-1)

;$idComboPath = GUICtrlCreateCombo("",113,60,393,21,-1,-1)
;GUICtrlSetData(-1,"exe")

;$idComboSwitch = GUICtrlCreateCombo("",527,60,107,21,-1,-1)
;GUICtrlSetData(-1, "/s|/q|/silent|/S|/qb|/passive|/?","/?")

;$idIcon = GUICtrlCreateIcon("notepad.exe",-1,506,60,21,21,-1,-1)

;$idTest = GUICtrlCreateButton("Test",634,60,55,21,-1,-1)
;$idDelete = GUICtrlCreateButton("Delete",789,60,39,21,-1,-1)

GUICtrlCreateRadio("Lock",694,60,42,21,-1,-1)
GUICtrlCreateRadio("Unlock",736,60,53,21,-1,-1)

GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Select
        Case $nMsg = $GUI_EVENT_CLOSE
            Exit
        Case $nMsg = $idBrowse
            Local $sDir = Load()
            If Not $sDir = False Then
                GUICtrlSetData($lbFolder, $sDir)

                $aFiles = _FileListToArrayRec($sDir, '*.msi;*.exe', $FLTAR_FILES, $FLTAR_NORECUR, $FLTAR_SORT, $FLTAR_NOPATH)
                If Not @error Then
                    ;Dim $idButton[($aFiles[0] + 1)]
                    Dim $idComboPath[($aFiles[0] + 1)]
                    Dim $idIcon[($aFiles[0] + 1)]
                    Dim $idComboSwitch[($aFiles[0] + 1)]
                    Dim $idTest[($aFiles[0] + 1)]
                    Dim $idDelete[($aFiles[0] + 1)]

                    $Btn_Start = GUICtrlCreateDummy()
                    For $i = 1 To $aFiles[0]
                        $x = $i * 30 + 30
                        ;$idButton[$i]    = GUICtrlCreateButton($aFiles[$i], 5,$x, 180, 30)
                        $idComboPath[$i]   = GUICtrlCreateCombo($aFiles[$i],113,$x,393,21)
                        $idIcon[$i]        = GUICtrlCreateIcon($aFiles[$i],-1,506,$x,21,21)
                        GUISetIcon(-1)
                        $idComboSwitch[$i] = GUICtrlCreateCombo("",527,$x,107,21)
                        GUICtrlSetData(-1, "/s|/q|/silent|/S|/qb|/passive|/?","/s")
                        $idTest[$i]        = GUICtrlCreateButton("Test",634,$x,55,21,-1,-1)
                        $idDelete[$i]      = GUICtrlCreateButton("Delete",789,$x,39,21,-1,-1)
                    Next
                    $Btn_End = GUICtrlCreateDummy()
                EndIf
            EndIf
;~      Case $Btn_Start To $Btn_End
;~          ; Run test command with argument
;~          If FileExists(GUICtrlRead($idComboPath[$nMsg])) Then
;~              ShellExecute(GUICtrlRead($idComboPath[$nMsg]) & " " & GUICtrlRead($idComboSwitch[$nMsg]))
;~          EndIf
    EndSelect
WEnd


Func Load()
    ; Display an open dialog to select a file.
    Local Const $sMessage = "Select a folder"
    Local $sFileSelectFolder = FileSelectFolder($sMessage, @WindowsDir)
    If @error Then
        Return False
    Else
        Return $sFileSelectFolder
    EndIf
EndFunc   ;==>Load


Func Save()
    ; take all the exe files and switches and generate a batch file in the same order
EndFunc   ;==>Save

 

Edited by NDog
Link to comment
Share on other sites

Do a Case Else and loop through the array holding the button ids and check that $nMsg = $aArray[n][0]

#include <GUIConstants.au3>

Global $hMain = GUICreate("Example", 100, 390)
Global $aButtons[0][2]
Global $iY = 10

For $i = 0 to Random(1, 14)
    ReDim $aButtons[$i + 1][2]
    $aButtons[$i][0] = GUICtrlCreateButton("Example Btn " & $i, 10, $iY, 80, 20)
    $iY += 25
Next

GUISetState(@SW_SHOW, $hMain)

While (True)
    Local $nMsg = GUIGetMsg()
    Switch ($nMsg)
        Case $GUI_EVENT_CLOSE
            Exit 0
        Case Else
            For $i = 0 to UBound($aButtons) - 1
                If ($nMsg = $aButtons[$i][0]) Then
                    MsgBox("", "", "Button " & GUICtrlRead($aButtons[$i][0]) & " was pressed!")
                    ExitLoop    ; Exit because we found the button, no need to keep searching
                EndIf
            Next
    EndSwitch
WEnd

 

Link to comment
Share on other sites

InunoTaishou

Thank you. I managed to figure it out from your advice.

; -- Created with ISN Form Studio 2 for ISN AutoIt Studio -- ;
#include <StaticConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#Include <GuiButton.au3>
#include <ComboConstants.au3>

#include <File.au3>

Global $aRow[0][5]
Global $iY = 10

; Main GUI header
$hGUI     = GUICreate("Silent Batch Maker",838,569,-1,-1,-1,-1)
$idBrowse = GUICtrlCreateButton("Browse",9,15,100,30,-1,-1)
$idSave   = GUICtrlCreateButton("Save",622,15,100,30,-1,-1)
$idLoad   = GUICtrlCreateButton("Load",728,15,100,30,-1,-1)
$lbFolder = GUICtrlCreateLabel("Browse for a folder",120,15,487,28,$SS_CENTERIMAGE,-1)
GUICtrlSetBkColor(-1,"-2")

; First row
GUICtrlCreateLabel("[1]",9,60,26,22,BitOr($SS_CENTER,$SS_CENTERIMAGE),$WS_EX_CLIENTEDGE)
GUICtrlSetState(-1,BitOr($GUI_CHECKED,$GUI_SHOW,$GUI_ENABLE))
GUICtrlSetFont(-1,10,400,0,"MS Sans Serif")
GUICtrlSetBkColor(-1,"-2")

GUICtrlCreateButton("Up",35,60,39,21,-1,-1)
GUICtrlCreateButton("Down",74,60,39,21,-1,-1)

;$idComboPath = GUICtrlCreateCombo("",113,60,393,21,-1,-1)
;GUICtrlSetData(-1,"exe")

;$idComboSwitch = GUICtrlCreateCombo("",527,60,107,21,-1,-1)
;GUICtrlSetData(-1, "/s|/q|/silent|/S|/qb|/passive|/?","/?")

;$idIcon = GUICtrlCreateIcon("notepad.exe",-1,506,60,21,21,-1,-1)

;$idTest = GUICtrlCreateButton("Test",634,60,55,21,-1,-1)
;$idDelete = GUICtrlCreateButton("Delete",789,60,39,21,-1,-1)

GUICtrlCreateRadio("Lock",694,60,42,21,-1,-1)
GUICtrlCreateRadio("Unlock",736,60,53,21,-1,-1)

GUISetState(@SW_SHOW)

;$idComboPath   = 0
;$idIcon        = 1
;$idComboSwitch = 2
;$idTest        = 3
;$idDelete      = 4

While True
    $nMsg = GUIGetMsg()
    Select
        Case $nMsg = $GUI_EVENT_CLOSE
            Exit
        Case $nMsg = $idBrowse
            Local $sDir = Load()
            If Not $sDir = False Then
                GUICtrlSetData($lbFolder, $sDir)
                $aFiles = _FileListToArrayRec($sDir, '*.msi;*.exe', $FLTAR_FILES, $FLTAR_NORECUR, $FLTAR_SORT, $FLTAR_NOPATH)
                If Not @error Then
                    Dim $aRow[($aFiles[0] + 1)][5]
                    For $i = 1 To $aFiles[0]
                        $x = $i * 30 + 30
                        $aRow[$i][0] = GUICtrlCreateCombo($aFiles[$i],113,$x,393,21)
                        $aRow[$i][1] = GUICtrlCreateIcon($aFiles[$i],-1,506,$x,21,21)
                        GUISetIcon(-1)
                        $aRow[$i][2] = GUICtrlCreateCombo("",527,$x,107,21)
                        GUICtrlSetData(-1, "/s|/q|/silent|/S|/qb|/passive|/?","/s")
                        $aRow[$i][3] = GUICtrlCreateButton("Test",634,$x,55,21,-1,-1)
                        $aRow[$i][4] = GUICtrlCreateButton("Delete",789,$x,39,21,-1,-1)
                        ;_ArrayDisplay($aRow)
                    Next
                EndIf
            EndIf
        Case Else
            For $i = 1 to UBound($aRow) - 1
                If ($nMsg = $aRow[$i][3]) Then ; Look for Test button pushed
                    ConsoleWrite("Test Button in Row " & $i & " was pressed!" & @CRLF)
                    $path   = GUICtrlRead($aRow[$i][0])
                    $switch = GUICtrlRead($aRow[$i][2])
                    ConsoleWrite("Test(" & $sDir &", " & $path &", " & $switch & ")" & @CRLF)
                    Test($sDir, $path, $switch)
                    ExitLoop    ; Exit because we found the button, no need to keep searching
                ElseIf ($nMsg = $aRow[$i][4]) Then ; Look for Test button pushed
                    ConsoleWrite("Delete Button in Row " & $i & " was pressed!" & @CRLF)
                    ExitLoop    ; Exit because we found the button, no need to keep searching
                EndIf
            Next
    EndSelect
WEnd

Func Load()
    ; Display an open dialog to select a file.
    Local Const $sMessage = "Select a folder"
    Local $sFileSelectFolder = FileSelectFolder($sMessage, @WindowsDir)
    If @error Then
        Return False
    Else
        Return $sFileSelectFolder
    EndIf
EndFunc   ;==>Load

Func Save()
    ; take all the exe files and switches and generate a batch file in the same order
EndFunc   ;==>Save

Func Test($rootdir, $path, $switch)
    ; Run test command with argument
    If FileExists($rootdir & "\" & $path) Then
        Run($rootdir & "\" & $path & " " & $switch)
    EndIf
EndFunc   ;==>Test

 

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