Jump to content
Sign in to follow this  
Slipk

Work case for multipe buttons with same variable

Recommended Posts

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 615, 437, 192, 124)
$Button = GUICtrlCreateButton("Button1", 40, 48, 75, 25)
$Button = GUICtrlCreateButton("Button2", 152, 48, 75, 25)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit

        Case $Button
            MsgBox(0,"","test")

    EndSwitch
WEnd

Hello all,

I have 2 buttons with the same variable, could be possible to work both, normal only the  last one added in the code will work.

I need this because I create from the gui multiple items that have the same variable and have to run the same function all of them, this number of items can be dynamic.

 

Sorry for my bad english,

Thank you!

Share this post


Link to post
Share on other sites

Slipk,

I would store the button ControlIDs in an array like this:

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

$iNumButtons = 5 ; You can have up to 16 at the moment - the algorithm for creating the buttons will need to be changed if you want more

Global $aButtons[$iNumButtons]

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

; Create the buttons
For $i = 0 To $iNumButtons - 1
    ; Use an algorithm to locate the buttons - here we simply increase the Y value for each one
    $aButtons[$i] = GUICtrlCreateButton("Button " & $i + 1, 10, 10 + (30 * $i), 75, 25)
Next

GUISetState(@SW_SHOW)

While 1
    $iMsg = GUIGetMsg()
    Switch $iMsg
        Case $GUI_EVENT_CLOSE
            Exit

        Case Else
            ; Was it a button
            For $i = 0 To $iNumButtons - 1
                ; loop through the button ControlIDs
                If $iMsg = $aButtons[$i] Then
                    ; Found it
                    MsgBox($MB_SYSTEMMODAL, "Button pressed", $i + 1)
                    ; No point in looking further
                    ExitLoop
                EndIf
            Next
    EndSwitch
WEnd

Please ask if you have any questions.

M23


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

 

Share this post


Link to post
Share on other sites

Slipk,

Or you could use OnEvent mode like this:

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

; Use OnEvent mode
Opt("GUIOnEventMode", 1)

$iNumButtons = 16 ; You can have up to 16 at the moment - the algorithm for creating the buttons will need to be changed if you want more

$hGUI = GUICreate("Test", 500, 500)
GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit") ; Allow GUI to close

; Get the start of the button ControlID sequence
$iStartCID = GUICtrlCreateDummy()
; Create buttons
For $i = 0 To $iNumButtons - 1
    ; Use an algorithm to locate the buttons - here we simply increase the Y value for each one
    GUICtrlCreateButton("Button " & $i + 1, 10, 10 + (30 * $i), 75, 25)
    ; Each button will fire the same function
    GUICtrlSetOnEvent(-1, "_Action")
Next

GUISetState(@SW_SHOW)

While 1
    Sleep(10)
WEnd

Func _Exit()
    Exit
EndFunc

Func _Action()
    ; And this is the function fired by a button press
    MsgBox($MB_SYSTEMMODAL, "Button pressed", @GUI_CtrlId - $iStartCID) ; We can tell the button pressed like this
EndFunc

Again, ask if you have any questions.

M23


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

 

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  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...