Copy link to clipboard
Copied
Hi
I´m now studying and adventuring myself in the Script UI creation. I´m enjoying it !!!
I´m having the following trouble: I added a button to a dialog box and would like to tell script to run some commands when users click on the button. So this is a simple (poor example):
var dlg = new Window ("dialog", "My dialog", [0,0,0,0])
dlg.size = [500,500]
dlg.location = [300,300]
var b = dlg.add("button", [0,0,0,0], "Run")
b.location = [20, 180]
b.size = [80, 30]
dlg.show()
b.onClick = test () //the problem is here
function test(){
alert("teste")
}
-------
Everything is right. But the onClick event does not execute the function test I´m passing! What Am I missing?
I know if I use
b.onClick = function (){
...
}
It works...but I´d like the function to be called instead of directlly created when calling the onClick. Do not know if I´m able to explain it better.
Thank you for the help
Best Regards
Gustavo.
1 Correct answer
The norm would be ...
var dlg = new Window ("dialog", "My dialog", [0,0,0,0])
dlg.size = [500,500]
dlg.location = [300,300]
var b = dlg.add("button", [0,0,0,0], "Run")
b.location = [20, 180]
b.size = [80, 30]
b.onClick = function(){ //the problem is here
test();
}
dlg.show()function test(){
alert("teste")
}
Explore related tutorials & articles
Copy link to clipboard
Copied
P.S: the way I wrote my example....function will execute when I close the dlg dialog.
And if I write the function at beggining it will autoexecute before dialog box be showed.
Looks it´s running the function automatically in the sequence of the commands! Not what I expect to be.
Thank you a lot
Gustavo.
Copy link to clipboard
Copied
The norm would be ...
var dlg = new Window ("dialog", "My dialog", [0,0,0,0])
dlg.size = [500,500]
dlg.location = [300,300]
var b = dlg.add("button", [0,0,0,0], "Run")
b.location = [20, 180]
b.size = [80, 30]
b.onClick = function(){ //the problem is here
test();
}
dlg.show()function test(){
alert("teste")
}
Copy link to clipboard
Copied
Hi Paul
Interesting. So EVERY onClick, onClose, onOpen, onChange ... events must receive a function??
And sure we can call other functions inside them.
Thank you a lot
Gustavo.
Copy link to clipboard
Copied
Interesting. So EVERY onClick, onClose, onOpen, onChange ... events must receive a function??
No, you don't need to create a function for every control or evert type. Some controls have default handlers and you only need to define new ones if you want to change the default action.
Copy link to clipboard
Copied
Thank you Michael. That worked too.
Can you give an example of controls with default handler that I do not need to create a function?
Thank you a lot
Best Regards
Gustavo.
Copy link to clipboard
Copied
You need to define the control's callback before you show the dialog so you should move b.onClick = test; to above the dlg.show() line.
If you want the onClick to use the test function use b.onClick = test;
b.onClick = test() asssigns the results of the test function( if any ) to the onClick so here it's undefined.
I have found that sometimes using alert or $.writeln doesn't work in a dialog control event callback.

