Coco wrote:You need to use GuiControl to associate a function object, see:
GuiControl(Functor)
Example:
Code: Select all
Gui, Add, Checkbox, vMyCheckbox, Sample checkbox
method := ObjBindMethod(MyClass, "MyMethod")
; or method := MyClass.MyMethod.Bind(MyClass)
GuiControl +g, MyCheckbox, %method%
Thanks for the working code, Coco. To be honest i don't understand a single word that the docs say on this matter. Is the
method some kind of reference to the MyMethod() of the MyClass()? Isn't that the same thing as creating an instance as the
this keyword works properly?
Here is the full code:
Code: Select all
Class MyClass {
static MyAttr := "sometext"
MyMethod() {
MsgBox, % this.MyAttr
}
}
Gui, Add, Tab2,, First Tab|Second Tab|Third Tab ; Tab2 vs. Tab requires v1.0.47.05.
Gui, Add, Checkbox, vMyCheckbox, Sample checkbox
method := ObjBindMethod(MyClass, "MyMethod")
; or method := MyClass.MyMethod.Bind(MyClass)
GuiControl +g, MyCheckbox, %method%
Gui, Tab, 2
Gui, Add, Radio, vMyRadio, Sample radio1
Gui, Add, Radio,, Sample radio2
Gui, Tab, 3
Gui, Add, Edit, vMyEdit r5
Gui, Tab
Gui, Add, Button, default xm, OK
Gui, Show
return
ButtonOK:
GuiClose:
GuiEscape:
ExitApp
wdmodlin wrote:For clarity you might also use your class name instead of "this" inside the methods when you are referring to values defined as static in the base class itself. So I would code it as:
Code: Select all
MyMethod() {
MsgBox, % MyClass.MyAttr
}
reserving "this" for things that are actually specific to a particular instance.
Lexikos will correct me if I am wrong.
Yeah, you are right, wdmodlin,
this keyword should only work for instances, never for the class itself. It was just my silly attempt to hammer the code.
Thank you.
Here is the working code:
Code: Select all
Class MyClass {
static MyAttr := "sometext"
MyMethod() {
MsgBox, % MyClass.MyAttr
}
}
Gui, Add, Tab2,, First Tab|Second Tab|Third Tab ; Tab2 vs. Tab requires v1.0.47.05.
Gui, Add, Checkbox, vMyCheckbox gMyClass.MyMethod, Sample checkbox
Gui, Tab, 2
Gui, Add, Radio, vMyRadio, Sample radio1
Gui, Add, Radio,, Sample radio2
Gui, Tab, 3
Gui, Add, Edit, vMyEdit r5
Gui, Tab
Gui, Add, Button, default xm, OK
Gui, Show
return
ButtonOK:
GuiClose:
GuiEscape:
ExitApp
Is there any way to avoid the need to use class name itself? Something like the following?
Code: Select all
MyMethod() {
MsgBox, % self.MyAttr
}