I've been wanting to write a script with multiple GUIs each having similar controls. For example, all GUIs will have an OK button and a Cancel button. My usual way of tackling this would be to give each control a unique global associated variable but the code was difficult to maintain and reuse, so I'm wondering if it might be possible to encapsulate all the code for each GUI and its controls inside a class. I'd also like to have multiple instances of some of the GUIs. Yesterday I started to look into AHK v2 and it seems a lot of this stuff might be easier in v2 than in v1, but I've spent the whole morning trying everything I could think of to no avail
If you guys could help me out it'd be awesome. To get me going, I'd need a simple script like this:
Controls: 1 x Edit box, 1 x Text control, 1 x Clear button.
Functionality: When the text in the Edit box changes, the Text control displays the contents of the Edit box. When the Clear button is clicked, the Edit box and the Text control are cleared. When the script runs, it creates two instances of the GUI, each working as just described and independently of the other GUI, that is, if I click Clear on the first GUI, it clears only the first GUI's Edit and Text controls. The important thing is that all of this (or as much as possible) needs to live within a class.
Off-topic question: is it wise for me to start using v2 yet or should I stick with v1, or will it be too much trouble for someone like myself who's not a real programmer? I've skimmed through bits of the documentation and I like how it feels more like a programming language.
Thanks!
(v2) Encapsulate whole GUI and event handlers inside class?
Re: (v2) Encapsulate whole GUI and event handlers inside class?
Code: Select all
Guis := [CustomGui.New(), CustomGui.New()]
class CustomGui
{
__New() {
Gui := GuiCreate('', '', this)
(this.EditCtrl := Gui.Add('Edit', 'w100 h100')).OnEvent('Change', 'EditCtrl_Change')
this.TextCtrl := Gui.Add('Text', 'yp w100 h100')
Gui.Add('Button', 'xm', 'Clear').OnEvent('Click', 'ClearBtn_Click')
Gui.Show()
}
EditCtrl_Change(EditCtrl, Info) {
this.TextCtrl.Value := EditCtrl.Value
}
ClearBtn_Click(ClearBtn, Info) {
this.TextCtrl.Value := this.EditCtrl.Value := ''
}
}
Re: (v2) Encapsulate whole GUI and event handlers inside class?
Brilliant, just what I needed! Quick question, what's the difference between single quotes and double quotes?swagfag wrote: ↑22 Apr 2020, 07:30not impossible to recreate in v1 but probably a lot more annoying and error proneCode: Select all
Guis := [CustomGui.New(), CustomGui.New()] class CustomGui { __New() { Gui := GuiCreate('', '', this) (this.EditCtrl := Gui.Add('Edit', 'w100 h100')).OnEvent('Change', 'EditCtrl_Change') this.TextCtrl := Gui.Add('Text', 'yp w100 h100') Gui.Add('Button', 'xm', 'Clear').OnEvent('Click', 'ClearBtn_Click') Gui.Show() } EditCtrl_Change(EditCtrl, Info) { this.TextCtrl.Value := EditCtrl.Value } ClearBtn_Click(ClearBtn, Info) { this.TextCtrl.Value := this.EditCtrl.Value := '' } }
Re: (v2) Encapsulate whole GUI and event handlers inside class?
AFAIK there is no difference but you can avoid the need to escape the other: Var:=" ' " . ' " '