Page 1 of 1

How to change GUI button text (v2) without destroying/recreating new GUI?

Posted: 23 Apr 2024, 04:35
by alawsareps
When I click the button called "OLD", it changes to "NEW", but to achieve that, it first need to destroy the GUI, then create it agan. just an ugly behavior.

Code: Select all

#Requires AutoHotkey v2.0

global btn_name := "OLD"

x::
{
    MyGui := Gui(, "Mouse")
    MyGui.SetFont("s10") 
    MyGui.Add("Button", "w220 h30", btn_name).OnEvent("Click", ButtonClick)

    MyGui.Show("w250 h100")

    ButtonClick(*) {
        global btn_name := "NEW"  
        MyGui.Destroy()
        send "{x}"
    }
}

Re: How to change GUI button text (v2) without destroying/recreating new GUI?  Topic is solved

Posted: 23 Apr 2024, 05:09
by Draken

Code: Select all

#Requires AutoHotkey v2.0

btn_name := "OLD"

x::{
    global
    MyGui := Gui(, "Mouse")
    MyGui.SetFont("s10") 
    MyGui.Add("Button", "w220 h30 vbtn", btn_name).OnEvent("Click", ButtonClick)

    MyGui.Show("w250 h100")

    ButtonClick(*) {
        Mygui["btn"].Text := "NEW"
    }
}

Re: How to change GUI button text (v2) without destroying/recreating new GUI?

Posted: 23 Apr 2024, 06:34
by just me
The first parameter of an event function is the Guicontrol object (similar to A_GuiControl in v1). So you can use:

Code: Select all

    ButtonClick(BtnObj, *) {
        BtnObj.Text := "NEW"
    }
to change the button's caption.