Slipk Posted December 16, 2018 Posted December 16, 2018 #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!
Moderators Melba23 Posted December 16, 2018 Moderators Posted December 16, 2018 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 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area
Moderators Melba23 Posted December 16, 2018 Moderators Posted December 16, 2018 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 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now