Jump to content
Sign in to follow this  
Zithen

Button with dynamic value

Recommended Posts

Zithen

Its been a very long time since I have used Autoit and just kind of getting back into it.

Was creating a basic script that would load the contents of a ini file and create a button next to them where you click on the button and it would delete that corresponding ini entry.

Couldn't get it working so now just trying to get it to where you click the button it will display a msgbox with the value. figure getting that to work will make the next step easier.

However I have kind of gotten lost and the msgbox just appears repeatedly. Had it at one point where it didn't, but the msgboxs never worked or just didn't display any data. 

After a few edits and trying to follow _GUICtrlButton_click help, I have gotten so lost and not sure where to go with it heh.

#include <ButtonConstants.au3>
#include <MsgBoxConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=button.kxf
$Form1_1 = GUICreate("Form1", 414, 426, 192, 124)
$MenuItem1 = GUICtrlCreateMenu("&File")
$MenuItem2 = GUICtrlCreateMenuItem("Exit", $MenuItem1)
$Group1 = GUICtrlCreateGroup("Group", 24, 8, 361, 337, BitOR($GUI_SS_DEFAULT_GROUP,$BS_CENTER))
GUICtrlSetFont(-1, 30, 400, 0, "MS Sans Serif")
GUICtrlCreateGroup("", -99, -99, 1, 1)
$reloadList = GUICtrlCreateButton("Reload List", 128, 360, 99, 33)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

Global $button[20], $aArray, $player

reloadIni()

Func reloadIni()

    Local $buttonY = 40

    ; Read the INI section labelled 'General'. This will return a 2 dimensional array.
    Local $aArray = IniReadSection("random.ini", "Things")

    ; Check if an error occurred.
    If Not @error Then
        ; Enumerate through the array displaying the keys and their respective values.
        For $i = 1 To $aArray[0][0]
            GUISetFont(16, 700, 4, "Times New Roman")
            $button[$i] = GUICtrlCreateButton("X", 30, $buttonY+5, 15, 20)
            GUICtrlCreateLabel($aArray[$i][0], 50, $buttonY, 240, 40)
            GUICtrlCreateLabel($aArray[$i][1], 250, $buttonY, 40, 40)
            $buttonY += 30
        Next
    EndIf

    GUISetState(@SW_SHOW)

    While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $reloadList
            reloadIni()
        Case $button[$i]
            buttonid($button[$i])
            ;MsgBox($MB_SYSTEMMODAL, "", $button[$i])
    EndSwitch
WEnd

EndFunc

Func buttonid($id)
    MsgBox($MB_SYSTEMMODAL, "", $id)
EndFunc

Share this post


Link to post
Share on other sites
Zithen

not sure if i explained well enough of what i was trying to do, so thought i would do it a little simpler.

 

using a for loop through a array to generate buttons. When you click on the button it bring up a message box with the value of its corresponding array value (click button 5 it would display 5).

Have tried a few  different ways and can't seem to get a solution.

latest code snip i have tried.

Global $testingbuttons[10], $starty = 70, $i

for $i=0 To UBound($testingbuttons)-1
    $testingbuttons[$i] = GUICtrlCreateButton("Button "& $i, -1, $starty, 100, 30)
    $starty += 35
Next

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $testingbuttons
            MsgBox($MB_SYSTEMMODAL, "work?", $testingbuttons[$i])
    EndSwitch
WEnd

Share this post


Link to post
Share on other sites
Melba23

Zithen,

This should give some idea of how you might go about it: ;)

#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>

; Create an array to hold the button data and the control Ids
Global $aButtons[5][2]
; Add the data
For $i = 0 To 4
    $aButtons[$i][0] = "Button " & $i & " pressed"
Next

$hGUI = GUICreate("Test", 500, 500)

; Create the buttons and add their ControlIDs to the array
For $i = 0 To UBound($aButtons) - 1
    $aButtons[$i][1] = GUICtrlCreateButton("Button " & $i, 10, 10 + (40 * $i), 80, 30)
Next

GUISetState()

While 1

    $iMsg = GUIGetMsg()
    Switch $iMsg
        Case $GUI_EVENT_CLOSE
            Exit
        ; Was the ControlID a button?
        Case Else
            For $i = 0 To UBound($aButtons) - 1
                If $iMsg = $aButtons[$i][1] Then
                    ; If so then display the data
                    MsgBox($MB_SYSTEMMODAL, "Button Pressed", $aButtons[$i][0])
                    ; No point in looking further
                    ExitLoop
                EndIf
            Next
    EndSwitch

WEnd
Please ask if you have any questions. :)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites
Zithen

Thanks so much. Never thought to use a multi dimensional array for this.

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  

×