Jump to content

How to identify which button was pressed


Go to solution Solved by Nine,

Recommended Posts

Hi. I'm stuck on a part of the code. Probably trivial and yet I'm stuck.

In the array "$aListPlikow" I keep a list of files located in a given directory. In the array "$aListFileNames" I hold the abbreviated names of those files. Using the code below, I generate a window with buttons, the number of which depends on the number of values in the "$aListPlikow" array.

#include <ComboConstants.au3>
#include <EditConstants.au3>
#include <WindowsConstants.au3>
#include <Constants.au3>
#include <File.au3>
#Include <Date.au3>
#include <FontConstants.au3>
#include <Array.au3>
#include <String.au3>
#include <MsgBoxConstants.au3>
#include <StringConstants.au3>
#include <Excel.au3>
#include <GUIConstantsEx.au3>
#include <Date.au3>
#Include <GuiButton.au3>

Global $hButtons[10]

Local $aListaPlikow[3] = ["2","AAAAAA_Temp1.xls","YYYYY_Temp2.xls"]
Local $aListaPlikowNazwy[3] = ["2","Temp1","Temp2"]

If $aListaPlikow[0] = 1 Then
    $hGUI = GUICreate("Wypelnij tresc pola 12", 1014, 400)
    Else

    $hGUI = GUICreate("WYBÓR TEMPLATE", 400, 80+90*$aListaPlikow[0])
    $i = 0
    While $i < $aListaPlikow[0]
        $hButtons[$i] = GUICtrlCreateButton($aListaPlikowNazwy[$i+1],20,120+($i*60),360,50)
        $i = $i+1
    WEnd
    GUISetState()
EndIf

Local $iMsg, $iButton
While True
  $iMsg = GUIGetMsg()
  Switch $iMsg
    Case $GUI_EVENT_CLOSE
      ExitLoop
    Case $hButtons[0] To $hButtons[$aListaPlikow[0]]
      $iButton = $iMsg - $hButtons[0]
      ConsoleWrite("button " & $iButton & " was pushed" & @CRLF)
  EndSwitch
WEnd

How can I tell which button has been pressed? Each of them is supposed to trigger a different action.

Edited by szyMarek
Link to post
Share on other sites
  • Solution

Something like this (untested) :

Local $iMsg, $iButton
While True
  $iMsg = GUIGetMsg()
  Switch $iMsg
    Case $GUI_EVENT_CLOSE
      ExitLoop
    Case $aButtons[0] To $aButtons[UBound($aButtons) - 1]
      $iButton = $iMsg - $aButtons[0]
      ConsoleWrite("button " & $iButton & " was pushed" & @CRLF)
  EndSwitch
WEnd

Next time, provide a runable script so we do not have to start from scratch writing our suggestions...

Link to post
Share on other sites
26 minutes ago, Nine said:

Something like this (untested) :

Local $iMsg, $iButton
While True
  $iMsg = GUIGetMsg()
  Switch $iMsg
    Case $GUI_EVENT_CLOSE
      ExitLoop
    Case $aButtons[0] To $aButtons[UBound($aButtons) - 1]
      $iButton = $iMsg - $aButtons[0]
      ConsoleWrite("button " & $iButton & " was pushed" & @CRLF)
  EndSwitch
WEnd

Next time, provide a runable script so we do not have to start from scratch writing our suggestions...

Nope 😕

Link to post
Share on other sites
3 minutes ago, szyMarek said:

Nope

Really ?  You will not make the effort to provide something for us, but you still ask for help ?

That is not very polite...

Link to post
Share on other sites
1 minute ago, Nine said:

Really ?  You will not make the effort to provide something for us, but you still ask for help ?

That is not very polite...

I'm sorry. Actually I should have mentioned that I've updated the code and you can run it now, so you can copy it and test it.

Unfortunately, the solution you proposed doesn't work and I can't get it to work. 

Again, I apologize for being rude.

Link to post
Share on other sites

Try it this way:

#include <ComboConstants.au3>
#include <EditConstants.au3>
#include <WindowsConstants.au3>
#include <Constants.au3>
#include <File.au3>
#Include <Date.au3>
#include <FontConstants.au3>
#include <Array.au3>
#include <String.au3>
#include <MsgBoxConstants.au3>
#include <StringConstants.au3>
#include <Excel.au3>
#include <GUIConstantsEx.au3>
#include <Date.au3>
#Include <GuiButton.au3>

Global $hButtons[10]

Global $aListaPlikow[3] = ["2","AAAAAA_Temp1.xls","YYYYY_Temp2.xls"]
Global $aListaPlikowNazwy[3] = ["2","Temp1","Temp2"]

If $aListaPlikow[0] = 1 Then
    $hGUI = GUICreate("Wypelnij tresc pola 12", 1014, 400)
    Else

    $hGUI = GUICreate("WYBÓR TEMPLATE", 400, 80+90*$aListaPlikow[0])
    $i = 1
    While $i < $aListaPlikow[0] +1
        $hButtons[$i] = GUICtrlCreateButton($aListaPlikowNazwy[$i],20,60+($i*60),360,50)
        $hButtons[0] = $i   ;$hButtons[0] remembers last Index with ButtonId
        $i = $i+1
    WEnd
    GUISetState()
EndIf

Global $iMsg, $iButton
While True
  $iMsg = GUIGetMsg()
  Switch $iMsg
    Case $GUI_EVENT_CLOSE
      ExitLoop
    Case $hButtons[1] To $hButtons[$hButtons[0]]
      ConsoleWrite(GUICtrlRead($iMsg)  & " was pushed" & @CRLF)
      ConsoleWrite('The Button ID = ' & $iMsg & @CRLF)
      ConsoleWrite('Associated File = ' & $aListaPlikow[$iMsg - $hButtons[0]] & @CRLF)
  EndSwitch
WEnd

mfg (auto)Bert

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
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...