Jump to content
Sign in to follow this  
hannes08

GUIOnEvent and Variable number of Controls

Recommended Posts

hannes08

Hi Folks,

maybe this is a stupid question, but anyway here it goes.

I have a Script in GUI mode running on event mode.

I also have a variable number of buttons the user may press.

This is an example of how I'm doing it right now:

Dim $aButtons[4]

Opt("GUIOnEventMode", 1)
GUICreate("MyGUI", 800, 600, -1, -1, BitOr($GUI_SS_DEFAULT_GUI, $WS_SIZEBOX, $WS_THICKFRAME, $WS_MAXIMIZEBOX))
GuiSetOnEvent($GUI_EVENT_CLOSE, "ClickClose")
_CreateButtons(4)
GUISetState(@SW_SHOW)

While 1
    Sleep(1000)
WEnd

Func _CreateButtons($iAmount)
    Local $x, $y
    For $i = 1 To $iAmount
        $aButtons[$i] = GUICtrlCreateButton("Button " & $i,  10 + $i * 50, 30 + $y, 40, 40)
        GuiCtrlSetOnEvent(-1, "ClickButton" & $i)
        If $i >= $iAmount / 2 Then $y = 70
    Next
EndFunc

Func ClickClose()
    Exit
EndFunc

Func ClickButton1()
    _ClickedButton(1)
EndFunc

Func ClickButton2()
    _ClickedButton(2)
EndFunc

Func ClickButton3()
    _ClickedButton(3)
EndFunc

Func ClickButton4()
    _ClickedButton(4)
EndFunc

Func _clickedButton($id)
    ConsoleWrite("Well, now you've clicked on " & $id & @CRLF)
EndFunc

The "problem" is that I need to create a function for each button, which I believe is suboptimal. :)

The amount of available buttons is configurable over an ini file, so it's not hard-coded as in the example.

Any hint is appreciated.


Regards,Hannes[spoiler]If you can't convince them, confuse them![/spoiler]

Share this post


Link to post
Share on other sites
Warsaw

Try changing it like this:

GuiCtrlSetOnEvent(-1, "ClickButton")

Func ClickButton()
For $i = 1 To $iAmount
  If @GUI_CtrlId = $aButtons[$i] Then _clickedButton($i)
Next
EndFunc

I use this method for something I made and it works well.

Edited by Warsaw

Share this post


Link to post
Share on other sites
hannes08

Hi Warsaw,

I'll look into this tomorrow. Thanks for the hint! :)

Edit: Works like a charm :D

Thanks a lot this saves me many lines of code :D

Edited by hannes08

Regards,Hannes[spoiler]If you can't convince them, confuse them![/spoiler]

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  

  • Similar Content

    • xiantez
      By xiantez
      This script used to work on an older version of AutoIT. Currently I am running AutoIT v3.3.14.5 and it's failing.
      Func PublicIP() ;Post public facing IP address Local $url = 'https://www.google.com/search?client=opera&q=what+is+my+ip&sourceid=opera&ie=UTF-8&oe=UTF-8' Local $getIPaddress = BinaryToString(InetRead($url)) Local $sStart = 'clamp:2">' Local $sEnd = '</div>' Local $ipaddress = _StringBetween($getIPaddress, $sStart, $sEnd For $i In $ipaddress MsgBox(0, 'External IP', "Your public IP address is " & $i) Next EndFunc ;==>PublicIP The console output shows:
      "C:\Users\user\Documents\AutoIT\Scripts\WSI Tools.au3" (197) : ==> Variable must be of type "Object".: For $i In $ipaddress For $i In $ipaddress^ ERROR ->14:12:16 AutoIt3.exe ended.rc:1 +>14:12:16 AutoIt3Wrapper Finished. >Exit code: 1 Time: 9.811
    • California
      By California
      Hello,
      I wrote a benchmark script to measure variable declarations
      to find out whether you should focus more on static or global variables
      #cs ---------------------------------------------------------------------------- AutoIt Version: 3.3.14.5 #ce ---------------------------------------------------------------------------- #Region Pre-Setting Local $iTally1 = 0 Local $iTally2 = 0 Local $iTally3 = 0 Local $iTally4 = 0 Local $iTally5 = 0 Local $iTally6 = 0 Local $iTally7 = 0 Global $GLOBALCONST1 = 1 Global $GLOBALCONST2 = 1 Global $GLOBALCONST3 = 1 Global $GLOBALCONST4 = 1 Global $GLOBALCONST5 = 1 #EndRegion Pre-Setting #Region Test Functions Func s1() Static $i = $GLOBALCONST1 Return $i EndFunc Func g2() Return $GLOBALCONST2 EndFunc Func g3() Static $i7 = "gsdgdfegbgbrwefw" Return $GLOBALCONST3 EndFunc Func g4() Static $i1 = 1 Static $i2 = "asd" Static $i3 = 234 Static $i4 = True Static $i5 = [0] Static $i6 = "hgsdg" Static $i7 = 1 Static $i8 = 1 Static $i9 = 1 Static $i0 = 1 Return $GLOBALCONST4 EndFunc Func g5() Local $i = $GLOBALCONST5 Return $i EndFunc Func g6() Local $i = 1 Return $i EndFunc Func g7() Return 1 EndFunc #EndRegion Test Functions #Region Benchmark Loop For $i = 0 To 15 Local $tDelta = TimerInit() Do $iTally1 += s1() Until TimerDiff($tDelta) >= 1000 Local $tDelta = TimerInit() Do $iTally2 += g2() Until TimerDiff($tDelta) >= 1000 Local $tDelta = TimerInit() Do $iTally3 += g3() Until TimerDiff($tDelta) >= 1000 Local $tDelta = TimerInit() Do $iTally4 += g4() Until TimerDiff($tDelta) >= 1000 Local $tDelta = TimerInit() Do $iTally5 += g5() Until TimerDiff($tDelta) >= 1000 Local $tDelta = TimerInit() Do $iTally6 += g6() Until TimerDiff($tDelta) >= 1000 Local $tDelta = TimerInit() Do $iTally7 += g7() Until TimerDiff($tDelta) >= 1000 Next #EndRegion Benchmark Loop ConsoleWrite(@CRLF&"Static1: "&$iTally1&" pkt"&@CRLF&"Global2: "&$iTally2&" pkt"&@CRLF&"Global3: "&$iTally3&" pkt"&@CRLF&"Global4: "&$iTally4&" pkt"&@CRLF&"Local5: "&$iTally5&" pkt"&@CRLF&"Local6: "&$iTally6&" pkt"&@CRLF&"Hardcode7:"&$iTally7&" pkt"&@CRLF) #cs Result Static1: 10291881 pkt global to static Global2: 13977324 pkt only global Global3: 9886169 pkt global and static Global4: 2933051 pkt global and many statics Local5: 9937314 pkt global to local Local6: 10306484 pkt only local Hardcode7: 14835319 pkt no variable #ce Result:
      100% no variable, hardcore value
      94% only global variable use
      69% only local variable use with hardcore value set
      69% only static variable use with global variable value set
      67% declaration of local variable with global variable value set
      66% only global variable use with one static variable beside
      20% only global variable use with ten static variables beside
      My thesis of the result:
      Be careful with declarations, whether local, global or static Note: in my test the global variable performance was better than the local one, but in practice the global one would lose performance due to multiple operations
       
      What is your best practice sharing data between multiple functions?
    • guinness
      By guinness
      #include <Array.au3> #include <ButtonConstants.au3> #include <GUIConstantsEx.au3> ; Proof of concept for using the control id as an index item for an array. I created back on 8th April 2013. Example() Func Example() ; Create the GUI. Local $iHeight = 400, $iWidth = 400 Local $hGUI = GUICreate('', $iWidth, $iHeight) GUISetState(@SW_SHOW, $hGUI) ; Declare variables to be used throughout the example. Local Const $BUTTON_ROWS_COLUMNS = 8 Local Enum $eCTRL_HWND, $eCTRL_VALUE, $eCTRL_MAX Local $aMsg[1][$eCTRL_MAX], _ $iButtonHeight = $iHeight / $BUTTON_ROWS_COLUMNS, _ $iButtonWidth = $iWidth / $BUTTON_ROWS_COLUMNS, _ $iControlID = 0 For $i = 0 To $BUTTON_ROWS_COLUMNS - 1 For $j = 0 To $BUTTON_ROWS_COLUMNS - 1 $iControlID = GUICtrlCreateButton($i & ',' & $j, $i * $iButtonWidth, $j * $iButtonHeight, $iButtonWidth, $iButtonHeight, $BS_CENTER) ; Increase the size of the array if the control id is greater than or equal to the total size of the array. If $iControlID >= UBound($aMsg) Then ReDim $aMsg[Ceiling($iControlID * 1.3)][$eCTRL_MAX] EndIf ; Add to the array. $aMsg[$iControlID][$eCTRL_HWND] = GUICtrlGetHandle($iControlID) $aMsg[$iControlID][$eCTRL_VALUE] = 'Sample string for the control id: ' & $iControlID Next Next ; Clear empty items after the last created control id. ReDim $aMsg[$iControlID + 1][$eCTRL_MAX] ; Display the array created. _ArrayDisplay($aMsg) Local $iMsg = 0 While 1 $iMsg = GUIGetMsg() Switch $iMsg Case $GUI_EVENT_CLOSE ExitLoop Case $aMsg[$eCTRL_HWND][$eCTRL_HWND] To UBound($aMsg) ; If $iMsg is greater than 0 and between the 0th index of $aMsg and the last item then display in the console. If $iMsg > 0 Then ConsoleWrite('Control Hwnd: ' & $aMsg[$iMsg][$eCTRL_HWND] & ', ' & $aMsg[$iMsg][$eCTRL_VALUE] & @CRLF) EndIf EndSwitch WEnd GUIDelete($hGUI) EndFunc ;==>Example  
    • TheAutomator
      By TheAutomator
      Is this better to check a variable before you assign it to a value that could be the same?
      for example:
      local $EmptyLog = false func WriteLog($text) _guictrledit_appendtext($log, ($EmptyLog ? @CRLF : $empty) & $text) If $EmptyLog Then $EmptyLog = False endfunc or does AutoIt behind the scenes already check this?
      i guess overwriting memory with the same value over and over again is not good if you can prevent this with a check?
    • JNutt
      By JNutt
      I see $hWnd used as a local variable in a lot of script examples.  I know the 'h' is used for handles, but what is the 'Wnd' short for?
      Thanks!
×