AHK V2 How to call a function on the click event for a button - function

I want to call a function whenever a button on my Gui is pressed, the code is as follows:
The ultimate idea of this is to open the gui when a window appears
Add Check Boxes
Add Button
User Checks Boxes and clicks button
text in array for checkboxes in array gets typed out int a field in the window.
`
MyGui:= Gui()
myGui.Title := "Test"; ;Gui Name
MyBtn_Click() ;Function to be called when button pressed
{
msgBox("Pressed Button")
}
myBtn := MyGui.AddButton(,"OOK") ;Adds an ok Button to the Gui
myBtn.OnEvent("Click", MyBtn_Click(*)) ;Call the function
myGui.Show() ;Shows Gui
`
Now the problem is when I try to call the function my button click it gives me a syntax error. I've tried to make sense of how to define a function and how to call it, but I cannot figure it out.

You need to put the asterisk in the function definition (see variadic functions) and then remove the parentheses in the second OnEvent parameter.
#Requires AutoHotkey v2.0
MyGui:= Gui()
myGui.Title := "Test" ;Gui Name
MyBtn_Click(*) ;Function to be called when button pressed
{
msgBox("Pressed Button")
}
myBtn := MyGui.AddButton(,"OOK") ;Adds an ok Button to the Gui
myBtn.OnEvent("Click", MyBtn_Click) ;Call the function
myGui.Show() ;Shows Gui

Related

How to get the reason for onUnloadEvent getting triggered

In Html page when I am closing the browser I am showing message using
window.onbeforeunload = confirmClose;
function confirmClose(event){}
But the same event is getting triggered for the following conditions also
http://msdn.microsoft.com/en-in/library/ie/ms536973%28v=vs.85%29.aspx
Close the current window.
Navigate to another location by entering a new address or selecting a Favorite.
Click the Back, Forward, Refresh, or Home button.
Click an anchor that refers the browser to another document.
Invoke the anchor.click method.
Invoke the document.write method.
Invoke the document.open method.
Invoke the document.close method.
Invoke the window.close method.
Invoke the window.open method, providing the possible value _self for the window name.
Invoke the window.navigate or NavigateAndFind method.
Invoke the location.replace method.
Invoke the location.reload method.
Specify a new value for the location.href property.
Submit a form to the address specified in the ACTION attribute via the INPUT type=submit control, or invoke the form.submit method.
How can i get what is the reason for triggering the method?
No, generally you can't. The closest you can get is, if the action that triggers the close event also triggers another event, such as clicking on a link, then you can catch that event and set a flag. For example, for clicking on a link (using jQuery):
var linkHasBeenClicked = false;
$('a').click(function() {
linkHasBeenClicked = true;
});
function confirmClose(event){
if (linkHasBeenClicked) {
// ...
} else {
// ...
}
linkHasBeenClicked = false;
}

Removing an EventListener from an object decided via an Array

I am making a simple calculator for an assignment in my IT-Class. It has 3 textboxes where the user can add his numbers, and the boxes start with a "0" inside of them, to show that the user is supposed to write numbers here. What I wanna do, is have this zero go away as the user puts focus on the box.
Since I have 3 boxes, I wanted to make the EventListener call up a function that removes the text and the Eventlistener, instead of writing the same code 3 times.
Using an array containing the different textboxes I managed to call them up, and change the text as I wanted, but the EventListener isn't being removed, so the text the user writes in is being removed when they focus on the textbox again
///////////////////////////////////////////////////////////////////////////////////
//The Array containing all the TextFields
var textFieldArr:Array = new Array(txtNumber1,txtNumber2,txtNumber2)
function onFocus(i:int){
return function (evt:FocusEvent){
textFieldArr[i].text = "";
textFieldArr[i].removeEventListener(FocusEvent.FOCUS_IN, onFocus(i))
}
}
//Calls up the onFocus function and declares variable i
txtNumber1.addEventListener(FocusEvent.FOCUS_IN, onFocus(0));
txtNumber2.addEventListener(FocusEvent.FOCUS_IN, onFocus(1));
txtNumber3.addEventListener(FocusEvent.FOCUS_IN, onFocus(2));
///////////////////////////////////////////////////////////////////////////////
Your event listener isn't being removed, because when you call onFocus(i) within the removeEventListener param, you are being returned a new function, and not the function that was originally being triggered by the event. The other problem with your code is that 'i' does not exist in the listener function you have declared -- you need to use only the properties of evt to figure out which text field to target, and the correct event listener to remove.
The common/correct way to do this is to use a event-accepting function, and listen to each text field with it. You don't even need the array anymore unless you have some other use for it.
function onFocusIn(evt:FocusEvent):void{
trace("onFocusIn("+evt.target+")");
var focusedField:TextField = TextField(evt.target);
if(focusedField){
focusedField.text = "";
focusedField.removeEventListener(FocusEvent.FOCUS_IN, onFocusIn);
}
}
txtNumber1.addEventListener(FocusEvent.FOCUS_IN, onFocusIn);
txtNumber2.addEventListener(FocusEvent.FOCUS_IN, onFocusIn);
txtNumber3.addEventListener(FocusEvent.FOCUS_IN, onFocusIn);
Listener is not removed, because onFocus returns on each invocation new Function instance.
You might try use target property of FocusEvent class:
var textFieldArr:Array = new Array(txtNumber1,txtNumber2,txtNumber2)
function onFocus(evt:FocusEvent){
TextField(evt.target).text = "";
TextField(evt.target).removeEventListener(FocusEvent.FOCUS_IN, onFocus)
}
//Calls up the onFocus function and declares variable i
txtNumber1.addEventListener(FocusEvent.FOCUS_IN, onFocus);
txtNumber2.addEventListener(FocusEvent.FOCUS_IN, onFocus);
txtNumber3.addEventListener(FocusEvent.FOCUS_IN, onFocus);

AS3/Flash: Calling a function MouseEvent from a datagrid click

I'm calling a function by clicking a button:
searchBT.addEventListener(MouseEvent.CLICK,searchXML);
function searchXML(Event:MouseEvent)
I want to call the same function from a datagrid.
Right now, when clicking on the datagrid several dynamic text fields are filled with the data of the clicked row.
I nead to perform the function (searchXML) at the same time. This and other calls result in errors:
fullList.dataGrid.addEventListener(ListEvent.ITEM_CLICK, clickGrid);
function clickGrid(e:ListEvent):void
{
searchXML(Event);
...
}
Any idea?
Cheers.
It is because the searchXML method waits for argument of MouseEvent type. You can redefine this method like this:
function searchXML(Event:MouseEvent = null)
And call it simple:
function clickGrid(e:ListEvent):void
{
searchXML();
// ...
}

Validation in flex input text field

i have a program in flex wherein i check if a particular field is validated then the submit button is enabled. I am trying something like this:
public function init():void
{
submit.addEventListener(Event.CHANGE,enableSubmit);
}
public function enableSubmit(event:TextInput):void
{
//some code to enable the button
}
I can call init in creation complete to add event listener to submit! is this the correct way to do it ? Please help!
Yes, you can call the init in the creationComplete.
The only thing you need to change in your code is the parameter of the enableSubmit from TextInput to Event because the event is passed by parameter, like the code below:
public function enableSubmit(event:Event):void
{
//some code to enable the button
}
Also the event listener need to be added to the TextInput, so I am assuming that submit control is the TextInput submit.addEventListener(Event.CHANGE,enableSubmit);

ComboBox Bug in ActionScript

I was trying to filter a combo box dataprovider based on the values in the text boxes . When the contents of the dataprovider changes Combo box automatically calls change event method . Please find the sample code below.
Filter Utility Function:
private function filterLocations(event:FocusEvent):void {
locationsList1.filterFunction = filterUtility;
locationsList1.refresh();
}
public function filterUtility(item:Object):Boolean {
// pass back whether the location square foot is with in the range specified
if((item.SQUARE_FOOTAGE >= rangeText1.text) && (item.SQUARE_FOOTAGE rangeText2.text))
return item.SQUARE_FOOTAGE;
}
// THIS WOULD BE CALLED WHEN COMBO BOX SELECTION IS DONE
private function selectLocationsReports(event:ListEvent):void {
selectedItem =(event.currentTarget as ComboBox).selectedItem.LOCATION_ID;
}
When the DataProvider gets refreshed its automatically calls change method and was throwing Null Pointer function because its prematurely calling the above selectLocationsReports method and its throwing error.
Can somebody let me know how to stop the CHANGE event from propogation when the dataprovider is refreshed.
You can't stop a CHANGE event, just don't add an event listener unless you are prepared to get the event. I don't see where your event listener for Event.CHANGE is in the code above.
Just be sure that you don't addEventListener(Event.CHANGE, selectLocationsReports) until your ComboBox is ready for it.
The other thing to do (on top of Kekoa's response) is put an if statement in the event handler, and check to make sure the data is there before you begin working with it.
A handy syntax I use frequently for this is
if(dataprovidername) {
}