Jump to content

Which button called function


JohnOne
 Share

Recommended Posts

Hi all, wondering if there is a way to dertermine which control triggered a function, when multiple controls trip just a single function.

I have some code which is repetative you see, with just one paramater different.

In the case of the code below "Red" and "Green".

So I'm looking for a way to have the data in the label set according to the button which is pressed.

I'm certain I've seen this on the forums, except I cannot find it with my search.

Sorry anoy the gui, I just made it quickly with koda, but it was my hope that GUIGetMsg() would work in some way.

Any pointers appreciated.

Sample code.

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
Opt("GUIOnEventMode", 1)
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 233, 131, 192, 124)
GUISetOnEvent($GUI_EVENT_CLOSE, "Form1Close")
$Button1 = GUICtrlCreateButton("Red", 32, 16, 75, 25, $WS_GROUP)
GUICtrlSetOnEvent($Button1, "Buttonclick")
$Button2 = GUICtrlCreateButton("Green", 128, 16, 75, 25, $WS_GROUP)
GUICtrlSetOnEvent($Button2, "Buttonclick")
$Label1 = GUICtrlCreateLabel("", 68, 56, 92, 20)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    Sleep(100)
WEnd

Func Buttonclick()
    Local $str = ""
    $msg = GUIGetMsg(1)
    Switch $msg[0]
        Case $Button1
            $str = "Red"
        Case $Button2
            $str = "Green"
    EndSwitch
    GUICtrlSetData($Label1,$str)
EndFunc   ;==>Buttonclick

Func Form1Close()
    Exit
EndFunc   ;==>Form1Close

I know I could do something like this

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
Opt("GUIOnEventMode", 1)
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 233, 131, 192, 124)
GUISetOnEvent($GUI_EVENT_CLOSE, "Form1Close")
$Button1 = GUICtrlCreateButton("Red", 32, 16, 75, 25, $WS_GROUP)
GUICtrlSetOnEvent($Button1, "Button1Click")
$Button2 = GUICtrlCreateButton("Green", 128, 16, 75, 25, $WS_GROUP)
GUICtrlSetOnEvent($Button2, "Button2Click")
$Label1 = GUICtrlCreateLabel("", 68, 56, 92, 20)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    Sleep(100)
WEnd

Func Button1Click()
    $str = "Red"
    _Function($str)
EndFunc

Func Button2Click()
    $str = "Green"
    _Function($str)
EndFunc

Func _Function($str)
    GUICtrlSetData($Label1,$str)
EndFunc   ;==>Buttonclick

Func Form1Close()
    Exit
EndFunc   ;==>Form1Close

But I could do without all the functions, as there may be a lot of them.

Plus, I'm curious to know.

Edited by JohnOne

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

  • Moderators

JohnOne,

Mixing OnEvent and MessageLoop modes.... :)

There is a nice macro in OnEvent mode which does just what you want: ;)

Func Buttonclick()
    Local $str = ""
    Switch @GUI_CtrlId ; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
        Case $Button1
            $str = "Red"
        Case $Button2
            $str = "Green"
    EndSwitch
    GUICtrlSetData($Label1,$str)
EndFunc   ;==>Buttonclick

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

 

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