Jump to content

Automate (Functional) Button Creation


Recommended Posts

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

Link to comment
Share on other sites

  • Moderators

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: ;)

#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. ;)

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

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

Link to comment
Share on other sites

  • 2 weeks later...

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