Skyrimfus

How to detect wich button was pressed on GUI

11 posts in this topic

#1 ·  Posted (edited)

How can i determine wich button was pressed when I use more than one button, without delcaring the button in a variable.

Example:

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 600, 600, 192, 124)
GUISetState(@SW_SHOW)

For $i=0 to 5
     GUICtrlCreateButton("Button"&$i,0,$i*50,60,30)
Next



#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case 3 to 8
            MsgBox(0,0,"You pressed button:")
    EndSwitch
WEnd

 

Edited by Skyrimfus

Share this post


Link to post
Share on other sites



#2 ·  Posted

Store them in an array. Alternatively you can just use event mode and set a function per button

For $i = 0 to 5
    GUICtrlCreateButton("Button" & $i, 10, 10 + ($i * 25), 100, 20)
    GUICtrlSetOnEvent(-1, "ButtonFunc" & $i)
Next

Func ButtonFunc0()

EndFunc

Func ButtonFunc1()

EndFunc

Func ButtonFunc2()

EndFunc

Func ButtonFunc3()

EndFunc

Func ButtonFunc4()

EndFunc

Func ButtonFunc5()

EndFunc

 

Share this post


Link to post
Share on other sites

#3 ·  Posted

I don't know how many buttons I will have, and I dont want to create a lot of functions. I simply need to know the control ID of the button that was pressed so I can use it in 1 function to calculate something

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

One option could be:

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 600, 600, 192, 124)
GUISetState(@SW_SHOW)
Global $controlpressed[100]

For $i = 0 To 5
    $hCtrl = GUICtrlCreateButton("Button" & $i, 0, $i * 50, 60, 30)
    $controlpressed[$hCtrl] = "Button" & $i
Next

#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case 3 To 8
            MsgBox(0, 0, "You pressed button:" & $controlpressed[$nMsg])
    EndSwitch
WEnd

Jos

Edited by Jos
1 person likes this

Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

@Jos oooooh I didn't realise that $nMsg gives you the controID when you click it! Thanks man!

Edited by Skyrimfus

Share this post


Link to post
Share on other sites

#6 ·  Posted

I recommend you up the way of InunoTaishou.
Or declare variables for buttons!

 

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Opt("GUIOnEventMode", 1)

Global $Form1 = GUICreate("Form1", 600, 600, -1, -1)
GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")


For $i = 0 To 5
    GUICtrlCreateButton("Button" & $i, 0, $i * 50, 60, 30)
    GUICtrlSetOnEvent(-1, "ButtonFunc" & $i)
Next
GUISetState(@SW_SHOW)


While 1 ; Keep program running!
    Sleep(100)
WEnd


Func ButtonFunc0()
    MsgBox(0, 0, "You pressed button: 0")
EndFunc   ;==>ButtonFunc0

Func ButtonFunc1()
    MsgBox(0, 0, "You pressed button: 1")
EndFunc   ;==>ButtonFunc1

Func ButtonFunc2()
    MsgBox(0, 0, "You pressed button: 2")
EndFunc   ;==>ButtonFunc2

Func ButtonFunc3()
    MsgBox(0, 0, "You pressed button: 3")
EndFunc   ;==>ButtonFunc3

Func ButtonFunc4()
    MsgBox(0, 0, "You pressed button: 4")
EndFunc   ;==>ButtonFunc4

Func ButtonFunc5()
    MsgBox(0, 0, "You pressed button: 5")
EndFunc   ;==>ButtonFunc5

Func _Exit()
    MsgBox(0, 0, "EXIT", 1)
    Exit 
EndFunc   ;==>_Exit

 


Regards,
 

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

9 minutes ago, VIP said:

Or declare variables for buttons!

Not sure why you insist, but you can easily put them in an Array as well and loop through that array when a button is pressed.

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 600, 600, 192, 124)
GUISetState(@SW_SHOW)
Global $hCtrl[100]
Global $NBottons = 5

For $i = 0 To $NBottons
    $hCtrl[$i] = GUICtrlCreateButton("Button" & $i, 0, $i * 50, 60, 30)
Next

#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case Else
            For $i = 0 To $NBottons
                If $hCtrl[$i] = $nMsg Then
                    MsgBox(0, 0, "You pressed button:" & "Button" & $i)
                EndIf
            Next
    EndSwitch
WEnd

 

Edited by Jos

Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

#8 ·  Posted


Declare variables way like your script!

I'm not sure what he wants to do but this script might be needed for him!

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

#Region ### START GUI section ### Form=
Global $hGUI = GUICreate("Form1", 600, 300, -1, -1)

Global Const $nBottons = 5
Global $hCtrl[$nBottons+1]

For $i = 0 To $nBottons
    $hCtrl[$i] = GUICtrlCreateButton("Button" & $i, 0, $i * 50, 60, 30)
Next

GUISetState(@SW_SHOW)
#EndRegion ### END  GUI section ###

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $hCtrl[0]
            MsgBox(0, 0, "You pressed button: 0")
        ;==>Button 0
        Case $hCtrl[1]
            MsgBox(0, 0, "You pressed button: 1")
        ;==>Button 1
        Case $hCtrl[2]
            MsgBox(0, 0, "You pressed button: 2")
        ;==>Button 2
        Case $hCtrl[3]
            MsgBox(0, 0, "You pressed button: 3")
        ;==>Button 3
        Case $hCtrl[4]
            MsgBox(0, 0, "You pressed button: 4")
        ;==>Button 4
        Case $hCtrl[5]
            MsgBox(0, 0, "You pressed button: 5")
        ;==>Button 5
    EndSwitch
WEnd

 

1 person likes this

Regards,
 

Share this post


Link to post
Share on other sites

#10 ·  Posted

I do not know what you mean!
But if possible, please do not care or pay attention to me!
If I'm wrong let me know!
Thank you!

 


Regards,
 

Share this post


Link to post
Share on other sites

#11 ·  Posted

Nearly the same than Jos, but using event mode  :)

#include <GUIConstantsEx.au3>

Opt("GUIOnEventMode", 1)

$Form1 = GUICreate("Form1")
GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")

For $i = 0 To 5
    GUICtrlCreateButton("Button" & $i, 0, $i * 50, 60, 30)
    GUICtrlSetOnEvent(-1, "ButtonFunc")
Next
GUISetState()

While 1
    Sleep(10)
WEnd

Func ButtonFunc()
    MsgBox(0, 0, "You pressed button " & @GUI_CtrlId - 3)
EndFunc 

Func _Exit()
    Exit 
EndFunc

 

1 person likes 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

  • Similar Content

    • Skeletor
      By Skeletor
      MS Splash Screens
      The office 2016/17 office suite has made many people turn heads with its functions and especially their splash screen.
      I now present to you, the MS Style Splash Screen. For Rookies, Novices and anyone who wants a ready made splash screen for your gui application.

      You can now download it and also try it out yourself.
      You have three colors to choose from:
      Blue
      Red
      Green
      All three colours are from the famous programs.
      I also included the KODA form to you can have freedom with this splash screen.
      Note: the KODA form does not have the three colours as well as the correct blue. 

      Have fun and enjoy.
      Note:
      I will update the splash to add the automated 3 dots animation at a later stage. 
       
       
    • Skeletor
      By Skeletor
      A gui splash screen. Themed after the famous MS Office Suite.
      Three colours to choose from, Red, Blue and Green.
      You can change the labels to your own application.

      Also bundled with this is the KODA form. So you can open up and see the basic structure of this splash screen.
       
      Splash Screen GUI.zip
    • BlazerV60
      By BlazerV60
      Hello all,
      I've been trying to figure out how to launch Google Chrome in the background (hidden) but it doesn't seem possible. I've tried the following methods:
      ShellExecute("C:\Program Files (x86)\Google\Chrome\Application\chrome.exe", "", "", "", @SW_HIDE) Also I've tried:
      ShellExecute(@ComSpec, "/c start chrome.exe","","",@SW_HIDE) Lastly I tried:
      RunWait('"'&@ProgramFilesDir&'\Google\Chrome\Application\chrome.exe" --silent-launch',@ScriptDir)  
      But all of them launch my chrome browser without hiding it. Does anyone know a workaround for this or if AutoIT just can't Chrome? 
      Bonus points if you know how to make it launch chrome hidden and make it go to https://www.autoitscript.com
       
      Thank you,
      Brian
    • SteveJM
      By SteveJM
      I have a working program currently driven largely through menu selections. I would like to add a toolbar where most of the tollbar button actions are basically the same as menu items, but quicker to access. I have been wading around in toolbar examples, MSDN pages etc, it's clearly going to be a bit of a slog to get everything right, including tooltips etc. I thought I would start with something simple to prove the principle.
      Using bits from the help file examples I have a small program that successfully displays a toolbar. However, what seemed like the most elegant way to deal with the button commands does not seem to work. My understanding was that a toolbar button fires a WM_COMMAND message, with the command Id set by the second parameter in the call to
      _GUICtrlToolbar_AddButton ( $hWnd, $iID, $iImage)
      so I though it would be a good idea to set this Id to the same value as my menu item Id; then it would run the same task which is what I wanted. This did not work. I am using message loop mode and would like to stick with this because some of my scripts run hardware at the same time as the gui; it is easier if I don't have to worry about code being interrupted with the hardware in an unknown state . So I added a handler for WM_COMMAND, with some cribbed display code to try and see why. The toolbar button defintely fired a WM_COMMAND message and the Id looked the same, so no explanation there.
      I guess the issue is with GUIGetMsg() which may be constructed to ignore all but a limited number of control handles, i.e. those made with the GuiCtrlCreate... commands; this is speculation.
      I would dearly love to find a tidy way to get around this. Having some controls handled in the message loop and some in a WM_COMMAND handler, performing the same task, feels ugly. I would be very grateful for further insight from someone experienced with handling a toolbar. Perhaps I should be trying to fire the menu item. I have attached a code snippet to try and illustrate the issue.
       
       
       
      ToolbarTrial.au3
    • XinYoung
      By XinYoung
      Hello all,
      I've been working on 4 standalone scripts. They each have their own GUI and are now independent .exe's. But instead of having 4 separate icons on my computer, is it possible to put them all together into one .exe?
      What I want is one interface with tabs. Each script's GUI would be on their own respective tab. Is this possible?
      The different scripts don't need to communicate with one another. I just want them all in one .exe.
      I can't find this anywhere... Any help would be greatly appreciated. Thank you!