Jump to content

Detecting buttons in MyArray()


DickG
 Share

Recommended Posts

I made a custom _ArrayDisplay() by copying the one in MyArray.au3. I renamed the function and call it from a script.

But _ArrayDisplay() has only one button: Copy Selected.

I changed the Copy Selected button to a Cancel button, then added an OK button. But I can't figure out how to detect which button is clicked and pass that to the calling script.

I tried adding GUICtrlSetOnEvent() for each button, then adding a simple function for each button to show a msg when it is clicked, but the button action is not being transmitted to the function, so my script doesn't know which button was pressed.

Any ideas on what I can do?

PS: I'm using AutoIt v3.2.12.1 on WinXP SP3.

Dick

Link to comment
Share on other sites

Do you want to use MessageLoop mode or OnEvent mode? You need to set option Opt(GUIOnEventMode, 1) for GUICtrlSetOnEvent() to work. _ArrayDisplay sets this option to "disable" in the beginning of function, and then resets it to the previous mode at the end of the function.

Link to comment
Share on other sites

I made a custom _ArrayDisplay() by copying the one in MyArray.au3. I renamed the function and call it from a script.

But _ArrayDisplay() has only one button: Copy Selected.

I changed the Copy Selected button to a Cancel button, then added an OK button. But I can't figure out how to detect which button is clicked and pass that to the calling script.

I tried adding GUICtrlSetOnEvent() for each button, then adding a simple function for each button to show a msg when it is clicked, but the button action is not being transmitted to the function, so my script doesn't know which button was pressed.

Any ideas on what I can do?

PS: I'm using AutoIt v3.2.12.1 on WinXP SP3.

Dick

If you use on event mode then the ID of the control which caused the event if given by the macro @GUI_CTRLID, so in your function you can loop through the elements of your array for buttons untill you find the one which has that ID.

For $n = 0 to $NumberOfBtns - 1
 if $arrayOfBtns[$n] = @GUI_CTRLID then
   $btnpressed = $n
   exitloop
 endif
next

Or you might only need the id so you could do something like this

switch @GUI_CTRLID
  Case $ArrayOfBtns[0]
        ;something to do
 Case $ArrayOfBtns[1]
     ;etc
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

OK, I changed GUIOnEventMode to 1, which allowed the two functions inside MyArray.au3 to run when a button is clicked. So I can detect if the Cancel or OK button is pressed.

But I still can't figure out how to pass the @GUI_CTRLID value from that script to my calling script.

I have a test script called MyTest.au3. In that script, I call the _ArrayDisplay_Test() function in MyArray.au3.

How can I pass the value of @GUI_CTRLID from _ArrayDisplay_Test() to MyTest.au3?

Dick

If you use on event mode then the ID of the control which caused the event if given by the macro @GUI_CTRLID, so in your function you can loop through the elements of your array for buttons untill you find the one which has that ID.

For $n = 0 to $NumberOfBtns - 1
 if $arrayOfBtns[$n] = @GUI_CTRLID then
   $btnpressed = $n
   exitloop
 endif
next

Or you might only need the id so you could do something like this

switch @GUI_CTRLID
  Case $ArrayOfBtns[0]
    ;something to do
 Case $ArrayOfBtns[1]
    ;etc
Link to comment
Share on other sites

I am not sure I understand. You now have two functions for two different buttons, so you can put different code to be executed when different buttons are pressed.

If you want to check for the buttons inside the _ArrayDisplay_Test() function, and then return the button ID to the main script, then inside the function you assign the button ID to a global variable, e.g., in the main program (MyTest.au3) declare Global $btn, and in MyArray.au3 write $btn=@GUI_CTRLID when the button is clicked.

Alternatively, you can make _ArrayDisplay_Test() return the button ID, e.g., save button ID in a local $btn variable in MyArray.au3 ($btn=@GUI_CTRLID) and then write Return $btn at the end of the function. In the main program, write $button = _ArrayDisplay_Test(); $button will have the button ID.

Link to comment
Share on other sites

Yes, I want to return @GUI_CTRLID from MyArray.au3 to MyTest.au3.

But when I declare $Button as a global in MyTest.au3, and set $Button = @GUI_CTRLID in MyArray.au3, it is not returned to MyTest.au3.

This is MyTest.au3:

#Region: INCLUDE
    #include <Constants.au3>
    #include <Array.au3>
    #include <MyArray.au3>
#endregion

#Region: DECLARE VARS
    Dim $Filelist[10][2]
    Global $Button
#endregion

;Populate dummy array for testing.
$Filelist[0][0] = "R1C1"
$Filelist[1][0] = "R2C1"
$Filelist[2][0] = "R3C1"
$Filelist[3][0] = "R4C1"
$Filelist[4][0] = "R5C1"
$Filelist[5][0] = "R6C1"

$Filelist[0][1] = "R1C2"
$Filelist[1][1] = "R2C2"
$Filelist[2][1] = "R3C2"
$Filelist[3][1] = "R4C2"
$Filelist[4][1] = "R5C2"
$Filelist[5][1] = "R6C2"

_ArrayDisplay_Rename($Filelist, "Preview")

MsgBox(0, "Button #", $Button)oÝ÷ Øì×È
ëk&®Þ)íà+­¬Êek$^©~éܶ*'"ëjëh×6;Cancel button [L,T,W,H]
$hndButton_Close = GUICtrlCreateButton('Cancel', 20, $GUI_H - 75, 70, 24)
GUICtrlSetOnEvent($hndButton_Close, "Buttonclicked")
        
;OK button [L,T,W,H]
$hndButton_OK = GUICtrlCreateButton('OK', ($GUI_W - 70)/2, $GUI_H - 75, 70, 24)
GUICtrlSetOnEvent($hndButton_OK, "Buttonclicked")oÝ÷ Ù©ÝjÛayº-¶¨|
ëk â²Zɧjg«Þjëh×6Func Buttonclicked()
    $Button = @GUI_CtrlId
    Exit
EndFunc

I hope this explains what I am trying to do.

Thanks for your help.

Dick

I am not sure I understand. You now have two functions for two different buttons, so you can put different code to be executed when different buttons are pressed.

If you want to check for the buttons inside the _ArrayDisplay_Test() function, and then return the button ID to the main script, then inside the function you assign the button ID to a global variable, e.g., in the main program (MyTest.au3) declare Global $btn, and in MyArray.au3 write $btn=@GUI_CTRLID when the button is clicked.

Alternatively, you can make _ArrayDisplay_Test() return the button ID, e.g., save button ID in a local $btn variable in MyArray.au3 ($btn=@GUI_CTRLID) and then write Return $btn at the end of the function. In the main program, write $button = _ArrayDisplay_Test(); $button will have the button ID.

Link to comment
Share on other sites

Yes, I want to return @GUI_CTRLID from MyArray.au3 to MyTest.au3.

But when I declare $Button as a global in MyTest.au3, and set $Button = @GUI_CTRLID in MyArray.au3, it is not returned to MyTest.au3.

This is MyTest.au3:

#Region: INCLUDE
    #include <Constants.au3>
    #include <Array.au3>
    #include <MyArray.au3>
#endregion

#Region: DECLARE VARS
    Dim $Filelist[10][2]
    Global $Button
#endregion

;Populate dummy array for testing.
$Filelist[0][0] = "R1C1"
$Filelist[1][0] = "R2C1"
$Filelist[2][0] = "R3C1"
$Filelist[3][0] = "R4C1"
$Filelist[4][0] = "R5C1"
$Filelist[5][0] = "R6C1"

$Filelist[0][1] = "R1C2"
$Filelist[1][1] = "R2C2"
$Filelist[2][1] = "R3C2"
$Filelist[3][1] = "R4C2"
$Filelist[4][1] = "R5C2"
$Filelist[5][1] = "R6C2"

_ArrayDisplay_Rename($Filelist, "Preview")

MsgBox(0, "Button #", $Button)oÝ÷ Øì×È
ëk&®Þ)íà+­¬Êek$^©~éܶ*'"ëjëh×6;Cancel button [L,T,W,H]
$hndButton_Close = GUICtrlCreateButton('Cancel', 20, $GUI_H - 75, 70, 24)
GUICtrlSetOnEvent($hndButton_Close, "Buttonclicked")
        
;OK button [L,T,W,H]
$hndButton_OK = GUICtrlCreateButton('OK', ($GUI_W - 70)/2, $GUI_H - 75, 70, 24)
GUICtrlSetOnEvent($hndButton_OK, "Buttonclicked")oÝ÷ Ù©ÝjÛayº-¶¨|
ëk â²Zɧjg«Þjëh×6Func Buttonclicked()
    $Button = @GUI_CtrlId
    Exit
EndFunc

I hope this explains what I am trying to do.

Thanks for your help.

Dick

It doesn't explain it to me I'm afraid. What is it that you want to happen when the buttons are pressed? I assume you want to run the script MyTest.au3. If so you could call it with a parameter.

Func Buttonclicked()
 Local $button
    if @GUI_CtrlId = $hnd_Button_ok then $button = "ok"
    if @GUI_CtrlId = $hnd_Button_close then $button = "close"
    Run("MyTest.exe " & $button)
    Exit;?? should this be return?
EndFunc
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

Yes. I want to close the called script (MyArray.au3) and pass the value of $Button to the calling script (MyTest.au3). MyTest.au3 only needs to display that value. It's a test.

I tried deleting Exit, but the called script stays open and does nothing. I need to close it to continue with the calling script, whcih is supposed to display the value of $Button.

Let me try to explain another way:

MyTest.au3

Global $Button

<populate $Filelist array with dummy data>

;Display array.

_ArrayDisplay_Rename($Filelist)

;Show button value.

MsgBox(0, "Button #", $Button)

MyArray.au3

< blah blah blah>

<create Cancel and OK buttons. Each calls Buttonclicked() function.

Func Buttonclicked()

$Button = @GUI_CtrlId

EndFunc

When I run MyTest, it does not display the value of $Button.

If I remove "Exit" from Buttonclicked function, the array is displayed, but I can't close it. If I include Exit, the array closes after I click a button, but no Button value is returned to the calling script.

I want to display the array from MyTest, detect which button is clicked frm the displayed array (OK or Close), then display that value. Of course, the array should close after I click any button.

Dick

Sorry Exit means terminate script? You need to remove this keyword

It doesn't explain it to me I'm afraid. What is it that you want to happen when the buttons are pressed? I assume you want to run the script MyTest.au3. If so you could call it with a parameter.

Func Buttonclicked()
 Local $button
    if @GUI_CtrlId = $hnd_Button_ok then $button = "ok"
    if @GUI_CtrlId = $hnd_Button_close then $button = "close"
    Run("MyTest.exe " & $button)
    Exit;?? should this be return?
EndFunc
Link to comment
Share on other sites

When I run MyTest, it does not display the value of $Button.

If I remove "Exit" from Buttonclicked function, the array is displayed, but I can't close it. If I include Exit, the array closes after I click a button, but no Button value is returned to the calling script.

I want to display the array from MyTest, detect which button is clicked frm the displayed array (OK or Close), then display that value. Of course, the array should close after I click any button.

If I understand it correctly, you run MyTest, and from MyTest you call a function _ArrayDisplay_Rename(). This function creates a GUI which shows the array, plus two buttons. When the buttons are pressed, you want to read the button ID, delete the GUI with array, and return to the main script. So if you are in the OnEvent mode, your function could be:

Func Buttonclicked()
    $button = @GUI_CtrlId
EndFunc

The main loop in the _ArrayDisplay_Rename() could be:

$button = ""
While 1
  Sleep(100)
  If $button = "" Then ContinueLoop; keep idling while buttons are not pressed
  GUIDelete($whater your gui handle is); close the GUI if a button has been pressed
  Return; return back to the main script
Wend

Actually, this will be better in the MessageLoop mode, e.g., you will not have any on event functions. Instead, in the main loop you will have:

While 1
  Sleep(100)
  $button = GUIGetMsg(); see if there are any messages; save the ID of the button pressed as $button is a global variable
  Switch $button
     Case $btn1, $btn2; if 1st or 2nd button are pressed
         GUIDelete($whater your gui handle is); close the GUI
         Return; return back to the main script
  EndSwitch
Wend
Link to comment
Share on other sites

Yes, you're close, but still missing an important element: when I return to the main script, it needs to know the button ID.

I can read the button ID from the called script fine. It's getting it passed to the main script that is the problem.

I am using your code, and both work fine "in the called script." But neither one returns the button ID to the calling script. That is my problem, and has been since I first posted this.

Note that _ArrayDisplay_Rename() is nothing more than a copy of _ArrayDisplay() from Array.au3, but with the Copy Selected button disabled and two new buttons added (OK and Cancel).

Also note that in the original code for _ArrayDisplay(), the Copy Selected button does not close the array. If you replace _ArrayDisplay_Rename() with _ArrayDisplay(), you will see that the array has one button called Copy Selected, which does not close the array. The only way the array will close is if I put GUIDelete() in the function that responds to the button event, not in the While loop. I showed the code for the buttons earlier, showing an event for each button. Each event calls the same function: Buttonclicked().

But closing the array is not my problem: passing the button ID to the calling script is. So I still need to know how to pass the button ID from the called script to the calling script. Any ideas?

If I understand it correctly, you run MyTest, and from MyTest you call a function _ArrayDisplay_Rename(). This function creates a GUI which shows the array, plus two buttons. When the buttons are pressed, you want to read the button ID, delete the GUI with array, and return to the main script. So if you are in the OnEvent mode, your function could be:

Func Buttonclicked()
    $button = @GUI_CtrlId
EndFunc

The main loop in the _ArrayDisplay_Rename() could be:

$button = ""
While 1
  Sleep(100)
  If $button = "" Then ContinueLoop; keep idling while buttons are not pressed
  GUIDelete($whater your gui handle is); close the GUI if a button has been pressed
  Return; return back to the main script
Wend

Actually, this will be better in the MessageLoop mode, e.g., you will not have any on event functions. Instead, in the main loop you will have:

While 1
  Sleep(100)
  $button = GUIGetMsg(); see if there are any messages; save the ID of the button pressed as $button is a global variable
  Switch $button
     Case $btn1, $btn2; if 1st or 2nd button are pressed
         GUIDelete($whater your gui handle is); close the GUI
         Return; return back to the main script
  EndSwitch
Wend
Link to comment
Share on other sites

Well, just after I clicked Add Reply, I tried something else, and I got it to work.

I'm not sure why, but here is what I have now:

In _ArrayDisplay_Rename() function inside MyArray.au3, I have:

$Button = ""
While 1
    If $Button = "" Then ContinueLoop
    GUIDelete($hGUI)
    Return
WEnd


Func Buttonclicked()
    $Button = @GUI_CtrlId
EndFuncoÝ÷ Ø Ýs2Më-jíÈ«Þ±©r^jÆÞ~Þjëh×6#Region: INCLUDE
    #include <Constants.au3>
    #include <Array.au3>
    #include <MyArray.au3>
#endregion

#Region: DECLARE VARS
    Dim $Filelist[10][2]
    Dim $Button
#endregion

;Populate dummy array for tesing.
$Filelist[0][0] = "R1C1"
$Filelist[1][0] = "R2C1"
$Filelist[2][0] = "R3C1"
$Filelist[3][0] = "R4C1"
$Filelist[4][0] = "R5C1"
$Filelist[5][0] = "R6C1"

$Filelist[0][1] = "R1C2"
$Filelist[1][1] = "R2C2"
$Filelist[2][1] = "R3C2"
$Filelist[3][1] = "R4C2"
$Filelist[4][1] = "R5C2"
$Filelist[5][1] = "R6C2"

_ArrayDisplay_Rename($Filelist, "Preview")

MsgBox(0, "Button ID", $Button)

So MyTest.au3 finally shows the button ID that is passed from the array function.

Thanks much for getting me on the right track.

Dick

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