Understanding GuiControl properties

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
potscrubber
Posts: 36
Joined: 09 Sep 2017, 01:51
Location: Aotearoa
Contact:

Understanding GuiControl properties

13 Jan 2024, 20:41

Hi

Wondering if someone can help me with understanding the rules about GuiControl properties. In v1, I am a big user of g labels and GuiControlGet, so I am trying to build similar functionality in v2.

For the code below, it works fine as is. But if I un-comment the OnEvent for the checkbox control, I get Error: This value of type "String" has no property named "value".

I would really like to understand why. Thanks for any advice.

Code: Select all

;#Requires AutoHotkey v2.0

MyGui := Gui()
MyGui.OnEvent("Close", MyGui_Close)
MyGui.SetFont("s12", "Consolas")
MyCheckbox := MyGui.Add("Checkbox","y+12", " MyCheckbox") ;.OnEvent("Click", GetCtrlValue)    ;Error: This value of type "String" has no property named "value".
MyGui.Add("Button",, "Get Value").OnEvent("Click", GetCtrlValue)
MyGui.Show()
return

;End of Autoexec

GetCtrlValue(*) {
    MsgBox("MyCheckBox value is " MyCheckbox.value)
}

MyGui_Close(*) {
    ExitApp
}
User avatar
boiler
Posts: 17279
Joined: 21 Dec 2014, 02:44

Re: Understanding GuiControl properties

13 Jan 2024, 21:08

That’s because you’re not assigning the checkbox GuiControl object to MyCheckbox, you’re assigning the result of the OnEvent method call to it. The script below assigns the GuiControl object to MyCheckbox, then it calls its OnEvent method on the next line.

Code: Select all

#Requires AutoHotkey v2.0

MyGui := Gui()
MyGui.OnEvent("Close", MyGui_Close)
MyGui.SetFont("s12", "Consolas")
MyCheckbox := MyGui.Add("Checkbox","y+12", " MyCheckbox")
MyCheckbox.OnEvent("Click", GetCtrlValue)
MyGui.Add("Button",, "Get Value").OnEvent("Click", GetCtrlValue)
MyGui.Show()
return

GetCtrlValue(*) {
    MsgBox("MyCheckBox value is " MyCheckbox.value)
}

MyGui_Close(*) {
    ExitApp
}
potscrubber
Posts: 36
Joined: 09 Sep 2017, 01:51
Location: Aotearoa
Contact:

Re: Understanding GuiControl properties

13 Jan 2024, 23:08

OK - I think I grok that. Thank you boiler! This tells me that I need to learn more about classes which have always been a bit mysterious to me.

Thanks again.
william_ahk
Posts: 502
Joined: 03 Dec 2018, 20:02

Re: Understanding GuiControl properties

13 Jan 2024, 23:27

In v2, variable assignment is not a replacement for v in options, you can still use vCtrlName, and it's the recommended way.

Code: Select all

#Requires AutoHotkey v2.0

MyGui := Gui()
MyGui.OnEvent("Close", MyGui_Close)
MyGui.SetFont("s12", "Consolas")
MyGui.Add("Checkbox","y+12 vMyCheckBox", " MyCheckbox").OnEvent("Click", GetCtrlValue)
MyGui.Add("Button",, "Get Value").OnEvent("Click", GetCtrlValue)
MyGui.Show()
return

;End of Autoexec

GetCtrlValue(*) {
    MsgBox("MyCheckBox value is " MyGui["MyCheckBox"].value)
}

MyGui_Close(*) {
    ExitApp
}
potscrubber
Posts: 36
Joined: 09 Sep 2017, 01:51
Location: Aotearoa
Contact:

Re: Understanding GuiControl properties

13 Jan 2024, 23:36

william_ahk wrote:
13 Jan 2024, 23:27
In v2, variable assignment is not a replacement for v in options, you can still use vCtrlName, and it's the recommended way.
I see. I didn't know I could reach the v option var like that without using MyGui.Submit() first.

Thank you too.
william_ahk
Posts: 502
Joined: 03 Dec 2018, 20:02

Re: Understanding GuiControl properties

15 Jan 2024, 01:46

potscrubber wrote:
13 Jan 2024, 23:36
I see. I didn't know I could reach the v option var like that without using MyGui.Submit() first.
That's not the same. When you call Gui.Submit() the values are extracted into a new object as {Name: Value} from the Gui controls. This is getting the value by reaching the GuiControl from the Gui, analogous to GuiControlGet in v1, and equivalent to assigning the GuiControl to a variable but with less of these extra variables floating around.
User avatar
boiler
Posts: 17279
Joined: 21 Dec 2014, 02:44

Re: Understanding GuiControl properties

15 Jan 2024, 02:43

Seems like six of one, half dozen of the other in that respect. Instead of extra variables floating around, there are a bunch of v names floating around. And MyCheckbox.value is cleaner than MyGui["MyCheckBox"].value, both visually and in ease of typing. And v names just seems more archaic to me — more like a holdover from v1 than how it might be designed from scratch.
potscrubber
Posts: 36
Joined: 09 Sep 2017, 01:51
Location: Aotearoa
Contact:

Re: Understanding GuiControl properties

15 Jan 2024, 03:37

Hmm. I kind of agree with everybody.

In my real world app, the GUI is a function, and it's very convenient to make it with global MyGui := Gui(), then I can read and write to any v variable for the small typing overhead of MyGui["MyCheckBox"].value, which is also descriptive.

On the other hand, the other way is also more elegant, I guess.

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: TAC109 and 38 guests