[V2] Gui inside a class - strange this

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
AHK_user
Posts: 515
Joined: 04 Dec 2015, 14:52
Location: Belgium

[V2] Gui inside a class - strange this

Post by AHK_user » 25 Mar 2023, 15:06

I was testing out how to define a gui as a class, like axlefublr advices, but encountered an unexpected behaviour.
When accessing this in the MenuHandler method, it does not returns the class, but the string "Option1".

Is this normal? I was expecting the class.

What is the best advice to manage this properly?
- Changing this.MenuHandler in this.MenuHandler.bind(this) (this works, but seems like a lot of extra code...)
- Moving MenuHandler to inside the __New method would also work, but then it is not accessible anymore...

Code: Select all

#Requires AutoHotKey v2.0

GuiClass()

Class GuiClass extends Gui{

	__New(FilePath := "") {
		super.__New("+Resize")	; Create the gui

        MyMenuBar := MenuBar()
        MyMenuBar.Add("Option1", this.MenuHandler)
        this.MenuBar := MyMenuBar

        this.Edit := this.AddEdit()
        this.Show("w300 h100")
    }

    MenuHandler(*){
        MsgBox(this)
    }
}

ntepa
Posts: 408
Joined: 19 Oct 2022, 20:52

Re: [V2] Gui inside a class - strange this

Post by ntepa » 25 Mar 2023, 21:08

You can use an event sink for guictrl OnEvent, but menu doesn't have that option.

viewtopic.php?p=484850#p484850
lexikos wrote:this is just a parameter; when the Menu calls the "method", it passes the item name as the first parameter. So this is just a string, not an object

teadrinker
Posts: 4311
Joined: 29 Mar 2015, 09:41
Contact:

Re: [V2] Gui inside a class - strange this

Post by teadrinker » 26 Mar 2023, 06:45

AHK_user wrote: Moving MenuHandler to inside the __New method would also work, but then it is not accessible anymore...
What do you mean?

Code: Select all

GuiClass()

Class GuiClass extends Gui{

	__New(FilePath := "") {
		super.__New("+Resize")	; Create the gui

        MyMenuBar := MenuBar()
        MyMenuBar.Add("Option1", MenuHandler)
        this.MenuBar := MyMenuBar

        this.Edit := this.AddEdit()
        this.Show("w300 h100")

        MenuHandler(ItemName, *){
            MsgBox(Type(this) . '`n' . ItemName)
        }
    }
}

ntepa
Posts: 408
Joined: 19 Oct 2022, 20:52

Re: [V2] Gui inside a class - strange this

Post by ntepa » 26 Mar 2023, 07:23

How about this?

Code: Select all

GuiClass()

Class GuiClass extends Gui{

    __New(FilePath := "") {
        super.__New("+Resize")	; Create the gui

        this.MenuHandler := MenuHandler
        this.CreateMenu()

        MenuHandler(ItemName, *){
            MsgBox(Type(this) . '`n' . ItemName)
        }
    }

    CreateMenu() {
        MyMenuBar := MenuBar()
        MyMenuBar.Add("Option1", this.MenuHandler)
        this.MenuBar := MyMenuBar

        this.Edit := this.AddEdit()
        this.Show("w300 h100")
    }
}

AHK_user
Posts: 515
Joined: 04 Dec 2015, 14:52
Location: Belgium

Re: [V2] Gui inside a class - strange this

Post by AHK_user » 26 Mar 2023, 08:10

ntepa wrote:
26 Mar 2023, 07:23
How about this?

Code: Select all

GuiClass()

Class GuiClass extends Gui{

    __New(FilePath := "") {
        super.__New("+Resize")	; Create the gui

        this.MenuHandler := MenuHandler
        this.CreateMenu()

        MenuHandler(ItemName, *){
            MsgBox(Type(this) . '`n' . ItemName)
        }
    }

    CreateMenu() {
        MyMenuBar := MenuBar()
        MyMenuBar.Add("Option1", this.MenuHandler)
        this.MenuBar := MyMenuBar

        this.Edit := this.AddEdit()
        this.Show("w300 h100")
    }
}
In that case the MenuHandler is still not accesible from outside the class.
I want to be able to call oGui.MenuHandler().
I think the most transparent way is to use a small fat arrow function, but I find it confusing that "this" in a method is not the class but the Itemname.

Code: Select all

#Requires AutoHotKey v2.0
oGui := GuiClass()
oGui.MenuHandler()

Class GuiClass extends Gui{

	__New(FilePath := "") {
		super.__New("+Resize")	; Create the gui

        MyMenuBar := MenuBar()
        MyMenuBar.Add("Option1", (*) => (this.MenuHandler()))
        this.MenuBar := MyMenuBar

        this.Edit := this.AddEdit()
        this.Show("w300 h100")
    }

    MenuHandler(*){
        MsgBox(this.Edit.text)
    }
}

teadrinker
Posts: 4311
Joined: 29 Mar 2015, 09:41
Contact:

Re: [V2] Gui inside a class - strange this

Post by teadrinker » 26 Mar 2023, 09:05

AHK_user wrote: In that case the MenuHandler is still not accesible from outside the class.

Code: Select all

oGui := GuiClass()
oGui.MenuHandler()
oGui.Method()

Class GuiClass extends Gui{

	__New(FilePath := "") {
		super.__New("+Resize")	; Create the gui

        MyMenuBar := MenuBar()
        MyMenuBar.Add("Option1", this.MenuHandler := MenuHandler)
        this.MenuBar := MyMenuBar

        this.Edit := this.AddEdit(, 'edit text')
        this.Show("w300 h100")

        MenuHandler(*){
            MsgBox(this.Edit.text)
        }
    }

    Method() {
        this.MenuHandler()
    }
}

Post Reply

Return to “Ask for Help (v2)”