Sign in to follow this  
Followers 0
creeping

call functions from button ID in array

6 posts in this topic

Anyway to do this? line in question = ;hmmm????????????????

Want to call a function based on the ID stored in the array.

eg.

User clicks on but[5][0] I want to call the function But5()

If not is there a work around?

#include <GUIConstants.au3>

Opt("WinWaitDelay", 800)  

#region global variables
;icon path
$sIconPath = @ScriptDir & '\icons\'
$iIconDim = 40
$iNumRows = 4
$iNumCols = 10
$iNumButs = $iNumRows * $iNumCols
Dim $aBut[$iNumButs][2]
Enum $E_BUT_ID, $E_BUT_TIP
$iX = 0
$iY = 0

$aBut[0][$E_BUT_TIP] = 'Check all fixed disks on next reboot'
$aBut[1][$E_BUT_TIP] = ''
$aBut[2][$E_BUT_TIP] = ''
$aBut[3][$E_BUT_TIP] = ''
$aBut[4][$E_BUT_TIP] = ''
$aBut[5][$E_BUT_TIP] = ''
$aBut[6][$E_BUT_TIP] = ''
$aBut[7][$E_BUT_TIP] = ''
$aBut[8][$E_BUT_TIP] = ''
$aBut[9][$E_BUT_TIP] = ''
$aBut[10][$E_BUT_TIP] = ''
$aBut[11][$E_BUT_TIP] = ''
$aBut[12][$E_BUT_TIP] = ''
$aBut[13][$E_BUT_TIP] = ''
$aBut[14][$E_BUT_TIP] = ''
$aBut[15][$E_BUT_TIP] = ''
$aBut[16][$E_BUT_TIP] = ''
$aBut[17][$E_BUT_TIP] = ''
$aBut[18][$E_BUT_TIP] = ''
$aBut[19][$E_BUT_TIP] = ''
$aBut[20][$E_BUT_TIP] = ''
$aBut[21][$E_BUT_TIP] = ''
$aBut[22][$E_BUT_TIP] = ''
$aBut[23][$E_BUT_TIP] = ''
$aBut[24][$E_BUT_TIP] = ''
$aBut[25][$E_BUT_TIP] = ''
$aBut[26][$E_BUT_TIP] = ''
$aBut[27][$E_BUT_TIP] = ''
$aBut[28][$E_BUT_TIP] = ''
$aBut[29][$E_BUT_TIP] = ''
$aBut[30][$E_BUT_TIP] = ''
$aBut[31][$E_BUT_TIP] = ''
$aBut[32][$E_BUT_TIP] = ''
$aBut[33][$E_BUT_TIP] = ''
$aBut[34][$E_BUT_TIP] = ''
$aBut[35][$E_BUT_TIP] = ''
$aBut[36][$E_BUT_TIP] = ''
$aBut[37][$E_BUT_TIP] = ''
$aBut[38][$E_BUT_TIP] = ''
$aBut[39][$E_BUT_TIP] = ''
#endregion

#region create GUI
$GUI = GUICreate('', $iIconDim * $iNumCols, $iIconDim * $iNumRows)

;display buttons
For $i = 0 To $iNumButs - 1 Step 1
    If $aBut[$i][$E_BUT_TIP] <> '' Then
        $aBut[$i][0] = GUICtrlCreateButton('', $iX, $iY, $iIconDim, $iIconDim, $BS_ICON)
        GUICtrlSetImage(-1, $sIconPath & $i & '.ico')
        GUICtrlSetTip(-1, $aBut[$i][$E_BUT_TIP])
    EndIf
    
    If mod($i, 10) = 9 Then     
        $iY += $iIconDim    
        $iX = 0
    Else
        $iX += $iIconDim
    EndIf   
Next

GUISetState(@SW_SHOW)

While 1
    $msg = GUIGetMsg()
    
    For $i = 0 To $iNumButs - 1 Step 1
        If $msg = $aBut[$i][0] Then
            ;hmmm????????????????       
        EndIf
    Next
    
    If $msg = $GUI_EVENT_CLOSE Then ExitLoop
Wend


Func But0()
EndFunc

Func But1()
EndFunc

Func But2()
EndFunc

Func But3()
EndFunc

Func But4()
EndFunc

Share this post


Link to post
Share on other sites



Could do, didn't not really want to do that way as I wanted to make the script flexible as possible and not hard code that stuff in.

Share this post


Link to post
Share on other sites

Maybe :

#include <GUIConstants.au3>

Opt("WinWaitDelay", 800) 

#region global variables
;icon path
$sIconPath = @ScriptDir & '\icons\'
$iIconDim = 40
$iNumRows = 4
$iNumCols = 10
$iNumButs = $iNumRows * $iNumCols
Dim $aBut[$iNumButs][2]
Enum $E_BUT_ID, $E_BUT_TIP
$iX = 0
$iY = 0

$aBut[0][$E_BUT_TIP] = 'Check all fixed disks on next reboot'
$aBut[1][$E_BUT_TIP] = ''
$aBut[2][$E_BUT_TIP] = ''
$aBut[3][$E_BUT_TIP] = ''
$aBut[4][$E_BUT_TIP] = ''
$aBut[5][$E_BUT_TIP] = ''
$aBut[6][$E_BUT_TIP] = ''
$aBut[7][$E_BUT_TIP] = ''
$aBut[8][$E_BUT_TIP] = ''
$aBut[9][$E_BUT_TIP] = ''
$aBut[10][$E_BUT_TIP] = ''
$aBut[11][$E_BUT_TIP] = ''
$aBut[12][$E_BUT_TIP] = ''
$aBut[13][$E_BUT_TIP] = ''
$aBut[14][$E_BUT_TIP] = ''
$aBut[15][$E_BUT_TIP] = ''
$aBut[16][$E_BUT_TIP] = ''
$aBut[17][$E_BUT_TIP] = ''
$aBut[18][$E_BUT_TIP] = ''
$aBut[19][$E_BUT_TIP] = ''
$aBut[20][$E_BUT_TIP] = ''
$aBut[21][$E_BUT_TIP] = ''
$aBut[22][$E_BUT_TIP] = ''
$aBut[23][$E_BUT_TIP] = ''
$aBut[24][$E_BUT_TIP] = ''
$aBut[25][$E_BUT_TIP] = ''
$aBut[26][$E_BUT_TIP] = ''
$aBut[27][$E_BUT_TIP] = ''
$aBut[28][$E_BUT_TIP] = ''
$aBut[29][$E_BUT_TIP] = ''
$aBut[30][$E_BUT_TIP] = ''
$aBut[31][$E_BUT_TIP] = ''
$aBut[32][$E_BUT_TIP] = ''
$aBut[33][$E_BUT_TIP] = ''
$aBut[34][$E_BUT_TIP] = ''
$aBut[35][$E_BUT_TIP] = ''
$aBut[36][$E_BUT_TIP] = ''
$aBut[37][$E_BUT_TIP] = ''
$aBut[38][$E_BUT_TIP] = ''
$aBut[39][$E_BUT_TIP] = ''
#endregion

#region create GUI
$GUI = GUICreate('', $iIconDim * $iNumCols, $iIconDim * $iNumRows)

;display buttons
For $i = 0 To $iNumButs - 1 Step 1
    If $aBut[$i][$E_BUT_TIP] <> '' Then
        $aBut[$i][0] = GUICtrlCreateButton('', $iX , $iY, $iIconDim, $iIconDim, $BS_ICON)
        GUICtrlSetImage(-1, $sIconPath & $i & '.ico')
        GUICtrlSetTip(-1, $aBut[$i][$E_BUT_TIP])
    EndIf
   
    If mod($i, 10) = 9 Then  
        $iY += $iIconDim   
        $iX = 0
    Else
        $iX += $iIconDim
    EndIf   
Next

GUISetState(@SW_SHOW)

While 1
    $msg = GUIGetMsg()
   
    For $i = 0 To $iNumButs - 1 Step 1
        If $msg = $aBut[$i][0] Then
            Call("But" &$i)
        EndIf
    Next
   
    If $msg = $GUI_EVENT_CLOSE Then ExitLoop
Wend


Func But0()
    MsgBox(0,"TEST","TEST")
EndFunc

Func But1()
EndFunc

Func But2()
EndFunc

Func But3()
EndFunc

Func But4()
EndFunc

Share this post


Link to post
Share on other sites

Great :)

So much easier and compact. Did not realize I could call a function using literal text.

Thanks

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

You can use the function Call()

#include <GUIConstants.au3>

Opt("WinWaitDelay", 800)  

#region global variables
;icon path
$sIconPath = @ScriptDir & '\icons\'
$iIconDim = 40
$iNumRows = 4
$iNumCols = 10
$iNumButs = $iNumRows * $iNumCols
Dim $aBut[$iNumButs][2]
Enum $E_BUT_ID, $E_BUT_TIP
$iX = 0
$iY = 0

$aBut[0][$E_BUT_TIP] = 'Check all fixed disks on next reboot'
$aBut[1][$E_BUT_TIP] = 'Button 1'
$aBut[2][$E_BUT_TIP] = 'Button 2'
$aBut[3][$E_BUT_TIP] = 'Button 3'
$aBut[4][$E_BUT_TIP] = 'Button 4'
$aBut[5][$E_BUT_TIP] = ''
$aBut[6][$E_BUT_TIP] = ''
$aBut[7][$E_BUT_TIP] = ''
$aBut[8][$E_BUT_TIP] = ''
$aBut[9][$E_BUT_TIP] = ''
$aBut[10][$E_BUT_TIP] = ''
$aBut[11][$E_BUT_TIP] = ''
$aBut[12][$E_BUT_TIP] = ''
$aBut[13][$E_BUT_TIP] = ''
$aBut[14][$E_BUT_TIP] = ''
$aBut[15][$E_BUT_TIP] = ''
$aBut[16][$E_BUT_TIP] = ''
$aBut[17][$E_BUT_TIP] = ''
$aBut[18][$E_BUT_TIP] = ''
$aBut[19][$E_BUT_TIP] = ''
$aBut[20][$E_BUT_TIP] = ''
$aBut[21][$E_BUT_TIP] = ''
$aBut[22][$E_BUT_TIP] = ''
$aBut[23][$E_BUT_TIP] = ''
$aBut[24][$E_BUT_TIP] = ''
$aBut[25][$E_BUT_TIP] = ''
$aBut[26][$E_BUT_TIP] = ''
$aBut[27][$E_BUT_TIP] = ''
$aBut[28][$E_BUT_TIP] = ''
$aBut[29][$E_BUT_TIP] = ''
$aBut[30][$E_BUT_TIP] = ''
$aBut[31][$E_BUT_TIP] = ''
$aBut[32][$E_BUT_TIP] = ''
$aBut[33][$E_BUT_TIP] = ''
$aBut[34][$E_BUT_TIP] = ''
$aBut[35][$E_BUT_TIP] = ''
$aBut[36][$E_BUT_TIP] = ''
$aBut[37][$E_BUT_TIP] = ''
$aBut[38][$E_BUT_TIP] = ''
$aBut[39][$E_BUT_TIP] = ''
#endregion

#region create GUI
$GUI = GUICreate('', $iIconDim * $iNumCols, $iIconDim * $iNumRows)

;display buttons
For $i = 0 To UBound($aBut) - 1
    If $aBut[$i][$E_BUT_TIP] <> '' Then
        $aBut[$i][0] = GUICtrlCreateButton('', $iX, $iY, $iIconDim, $iIconDim, $BS_ICON)
        GUICtrlSetImage(-1, $sIconPath & $i & '.ico')
        GUICtrlSetTip(-1, $aBut[$i][$E_BUT_TIP])
    EndIf
    
    If mod($i, 10) = 9 Then  
        $iY += $iIconDim    
        $iX = 0
    Else
        $iX += $iIconDim
    EndIf   
Next

GUISetState(@SW_SHOW)

While 1
    $msg = GUIGetMsg()
    
    For $i = 0 To UBound($aBut) - 1
        If $msg = $aBut[$i][0] And $aBut[$i][0] <> '' Then
            Call('But' & $i)
        EndIf
    Next
    
    If $msg = $GUI_EVENT_CLOSE Then ExitLoop
Wend


Func But0()
    MsgBox(0, 'Button 0', 'You press button 0.')
EndFunc

Func But1()
        MsgBox(0, 'Button 1', 'You press button 1.')
EndFunc

Func But2()
    MsgBox(0, 'Button 2', 'You press button 2.')
EndFunc

Func But3()
    MsgBox(0, 'Button 3', 'You press button 3.')
EndFunc

Func But4()
    MsgBox(0, 'Button 4', 'You press button 4.')
EndFunc
Edit: I type too slooowww.... :"> Edited by Danny35d

AutoIt Scripts:NetPrinter - Network Printer UtilityRobocopyGUI - GUI interface for M$ robocopy command line

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