Jump to content



Photo

Automate (Functional) Button Creation


  • Please log in to reply
4 replies to this topic

#1 Obviator

Obviator

    Seeker

  • Active Members
  • 41 posts

Posted 19 August 2012 - 02:16 AM

i'm creating an automated install routine for about 20 different programs. The first 7 or so are "requireds", if not already installed. i can check that and advise the user they're not available for install. So far, so good. My problem is that i end up typing the same thing for each of those first 7 buttons. i've tried to have the system create the buttons, limiting my retyping many lines of code. However, when i do this i do not get to the function i'm trying to direct the click to execute.
Here's one of the buttons i have to type:
Func _btnName($x1) If _Convert2Number($mNameREG) < _Convert2Number($mNameINI) Then $mbtnName = GUICtrlCreateButton(_CheckInstall($mNameINI), $mCol05, ($mXpos + ($mMultiplier * $x1)) - 4, $mWidth, $mHeight, 1) GUICtrlSetOnEvent(-1, "_NameClick") _SetFont() Else _LabelCreate(_CheckInstall($mNameREG), $mCol05, $mXpos + ($mMultiplier * $x1), $mWidth, $mHeight, 1) EndIf EndFunc

The only variable i have feeding into this function is an offset number and NAME could be anything You want. i further have an .INI file that contains the current available version that i check against the system registry then convert both inputs to numbers (to see which one is greater), finally display the button if necessary. i've tried creating a function that encapsulates this into one function, but on clicking it doesn't go where i point it to.
Is it possible to get a $btnName as i intend or do i have to do this long-hand?

Thanks,
Dave

Func _CreateButton($x1, $x2, $x3, $x4, $x5) ;$x1 = Registry variable ;$x2 = INI variable ;$x3 = Button Name ;$x4 = OnClick Event ;$x5 = Offset Number If _Convert2Number($x1) < _Convert2Number($x2) Then $x3 = GUICtrlCreateButton(_CheckInstall($x2), $mCol05, ($mXpos + ($mMultiplier * $x5)) - 4, $mWidth, $mHeight, 1) GUICtrlSetOnEvent(-1, $x4) _SetFont() Else _labelCreate(_CheckInstall($mX1), $mCol05, $mXpos + ($mMultiplier * $x5), $mWidth, $mHeight, 1) EndIf MsgBox(0, "Vars", "$x1 = " & $x1 & @CRLF & _ "$x2 = " & $x2 & @CRLF & _ "$x3 = " & $x3 & @CRLF & _ "$x4 = " & $x4 & @CRLF & _ "$x5 = " & $x5) EndFunc

The MsgBox shows the variables i send and what i intend is that $x3 should be a $btnNAME. Instead i get a number that bears no relation to anything i'm aware of. (on the first button in my form i get a "7".)







#2 Melba23

Melba23

    Yes, me!

  • Moderators
  • 15,387 posts

Posted 19 August 2012 - 08:43 AM

Obviator,

The number that is returned in $x3 is the ControlID of the button. This is the index of the control in the internal array of the controls created maintained by AutoIt - it allows you to reference the control later. :)

In this case the ControlID is not really relevant and so there is no need to store it. This script shows how I would handle the button creation - I have simulated the data to pass to the function: ;)
AutoIt         
#include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #include <EditConstants.au3> Opt("GUIOnEventMode", 1) Global $iMax_Buttons = 3 ; Simulate the variables ; [Registry variable, INI variable, Button Name, OnClick Event] Global $aVariables[$iMax_Buttons][4] = [[100, 200, "Button 1", "_Button_1"], _                                         [200, 100, "Button 2", "_Button_2"], _                                         [100, 200, "Button 3", "_Button_3"]] ; Button 1 and 3 should display ; Now create the GUI $hGUI = GUICreate("Test", 500, 500) GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit") ; Now loop through the buttons and call the creation function For $i = 0 To $iMax_Buttons - 1     _CreateButton($aVariables[$i][0], $aVariables[$i][1], $aVariables[$i][2], $aVariables[$i][3]) Next GUISetState() While 1     Sleep(10) WEnd Func _CreateButton($iReg_Val, $iIni_Val, $sName, $sFunc)     Local $mCol05 = 10, $mXpos = 10, $mMultiplier = 40, $mWidth = 80, $mHeight = 30     ; Start count for the number of buttons created     Static $iCount = 0     ; Now see if we need to create a button     If $iIni_Val > $iReg_Val Then         ; We create it         GUICtrlCreateButton($sName, $mCol05, ($mXpos + ($mMultiplier * $iCount)) - 4, $mWidth, $mHeight)         ; Set the OnEvent function         GUICtrlSetOnEvent(-1, $sFunc)         ; Increase the count so we move to the next element in the array for the next pass         $iCount += 1     EndIf EndFunc Func _Button_1()     MsgBox(0, "Hi", "Button 1 pressed") EndFunc Func _Button_2()     MsgBox(0, "Hi", "Button 2 pressed") EndFunc Func _Button_3()     MsgBox(0, "Hi", "Button 3 pressed") EndFunc Func _Exit()     Exit EndFunc

I hope the comments are clear enough, but please ask if you have any questions. :)

M23

P.S. Given your ignorance of ControlIDs (and we were all beginners once so please do not take it personally) perhaps this tutorial might be useful reading. ;)
StringSize - Automatically size controls to fit text - ExtMsgBox - A user customisable replacement for MsgBox

Toast - Small GUIs which pop out of the Systray - Marquee - Scrolling tickertape GUIs

Scrollbars - Automatically sized scrollbars with a single command - GUIFrame - Subdivide GUIs into many adjustable frames

GUIExtender - Extend and retract multiple sections within a GUI - NoFocusLines - Remove the dotted focus lines from buttons, sliders, radios and checkboxes

ChooseFileFolder - Single and multiple selections from specified path tree structure - - Notify - Small notifications on the edge of the display

RecFileListToArray - An alternative to _FileListToArray with user-defined include/exclude masks, maximum recursion level, sorting and displayed path options

GUIListViewEx - Insert, delete, move, drag and sort ListView items


#3 Obviator

Obviator

    Seeker

  • Active Members
  • 41 posts

Posted 19 August 2012 - 09:50 PM

Thanks Melba!
While not a beginner, up 'til now i haven't had to deal with them. i'm not really a programmer, i'm just someone trying to make my job easier and quicker, if that means quickly throwing something together that works, i'll give it a try. Elegance in programming constructs is not in me, brute force on the other hand...
i'll read through that tutorial too.
Thanks!

Dave
  • Xandy likes this

#4 enigmaforceiv

enigmaforceiv

    Seeker

  • Active Members
  • 26 posts

Posted 02 September 2012 - 01:05 AM

May I ask why only 1 and 3 show in your coding, Melba?

Edited by enigmaforceiv, 02 September 2012 - 01:06 AM.


#5 wyzzard

wyzzard

    Seeker

  • Active Members
  • 30 posts

Posted 02 September 2012 - 01:32 AM

May I ask why only 1 and 3 show in your coding, Melba?


In their simulated variables array set up they set the Registry Variable to be larger than the INI Variable... in this case you wouldn't want to enable the user to install an older version of the software so that button is disabled/not shown.

Edited by wyzzard, 02 September 2012 - 01:33 AM.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users