Make click event interact with custom class object Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
User avatar
DatHypnoboi
Posts: 13
Joined: 21 Apr 2022, 21:05
Contact:

Make click event interact with custom class object

Post by DatHypnoboi » 05 Feb 2023, 04:14

I'm using a custom class which contains a GUI object and I want the button to call a class method when pressed. However, as far as I can tell, the click event function has to be outside of the class declaration, and thus cannot interact with the internal methods. Is there any way around this?

Example:

Code: Select all

class MyClassName {
    __New() {
        this.myGui := Gui()
        this.myButton := this.myGui.AddButton(, "Button")
        this.myButton.OnEvent("Click", myButton_Click)
        this.myGui.Show()
    }

    myMethod() {
        ; I want to be able to call this when myButton is clicked
    }
}

myButton_Click(button, info) {
    ; I want to be able to call myMethod here
}


myClass := MyClassName()

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

Re: Make click event interact with custom class object  Topic is solved

Post by ntepa » 05 Feb 2023, 06:33

Code: Select all

class MyClassName {
    __New() {
        this.myGui := Gui(,,this)
        this.myButton := this.myGui.AddButton(, "Button")
        this.myButton.OnEvent("Click", "myMethod")
        this.myGui.Show()
    }

    myMethod(button, info) {
        Msgbox "button clicked"
    }
}

myClass := MyClassName()

User avatar
DatHypnoboi
Posts: 13
Joined: 21 Apr 2022, 21:05
Contact:

Re: Make click event interact with custom class object

Post by DatHypnoboi » 05 Feb 2023, 17:01

ntepa wrote:
05 Feb 2023, 06:33

Code: Select all

class MyClassName {
    __New() {
        this.myGui := Gui(,,this)
        this.myButton := this.myGui.AddButton(, "Button")
        this.myButton.OnEvent("Click", "myMethod")
        this.myGui.Show()
    }

    myMethod(button, info) {
        Msgbox "button clicked"
    }
}

myClass := MyClassName()
Oh, I didn't know that EventObj was a thing, thank you so much!

Post Reply

Return to “Ask for Help (v2)”