Page 1 of 1

Call class method with GUI g-label

Posted: 19 Apr 2021, 10:43
by Pepineros
Hello scripters!

I'm working on a new little script which involves creating a number of GUIs inside a class. Running through some concepts and I seem to be unable to reference the class methods with a GUI g-label.

As expected - not using classes at all - this works:

Code: Select all

gui, add, text, ghello, click me
hello() {
	msgbox, Hello, world!
}
gui, show
Correctly shows the GUI and triggers the msgbox when the text is clicked.

I found that this also works:

Code: Select all

class guiconstructor {
	createGUI() {
		gui, add, text, ghello, click me
	}
}
hello() {
	msgbox, Hello, world!
}
guiconstructor.createGUI()
gui, show
With hello() defined as a function outside the class, it is recognised as a valid label and triggered when the text is clicked.

However, with hello() defined as a class method, I was not able to find a way to reference it with the g-label:

Code: Select all

class guiconstructor {
	createGUI() {
		gui, add, text, ghello, click me
	}
	hello() {
		msgbox, Hello, world!
	}
}
guiconstructor.createGUI()
gui, show
This results in an error at runtime, "target label does not exist, specifically ghello". I tried creating a reference to the hello() method by writing createGUI() like this:

Code: Select all

createGUI() {
	hello := guiconstructor.hello  ; this.hello has the same behaviour
	gui, add, text, g%hello%, click me
}
Which returns no errors at runtime but the messagebox does not appear when clicked; or like this:

Code: Select all

createGUI() {
	hello := func("guiconstructor.hello")
	gui, add, text, g%hello%, click me
}
Which has the same behaviour.
Using func("this.hello") instead results in runtime error "Target label does not exist, specifically g0" which I think is AHK's way of telling me that func("this.hello") returned an empty function object.

My workaround at the moment is to build my GUIs inside class methods and put the g-label routines in the same module outside of the class. This is functional but if there's a way to use the g-label routines as class methods I would prefer to do that instead.

If there's an existing script or module that implements this that would be great - I'm sure I could figure it out just looking at an example of what I'm trying to do.

Thanks!


- Pep

Re: Call class method with GUI g-label

Posted: 19 Apr 2021, 12:47
by Xtra