(v2) Encapsulate whole GUI and event handlers inside class?

Get help for the alpha version of AutoHotkey v2 here. Please state the v2 version you are working with in the title when making a new topic.
User avatar
Scoox
Posts: 122
Joined: 11 May 2014, 09:12
Location: China

(v2) Encapsulate whole GUI and event handlers inside class?

22 Apr 2020, 06:18

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 :headwall:

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! :ugeek:
swagfag
Posts: 4143
Joined: 11 Jan 2017, 17:59

Re: (v2) Encapsulate whole GUI and event handlers inside class?

22 Apr 2020, 07:30

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 := ''
	}
}
not impossible to recreate in v1 but probably a lot more annoying and error prone
User avatar
Scoox
Posts: 122
Joined: 11 May 2014, 09:12
Location: China

Re: (v2) Encapsulate whole GUI and event handlers inside class?

22 Apr 2020, 10:25

swagfag wrote:
22 Apr 2020, 07:30

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 := ''
	}
}
not impossible to recreate in v1 but probably a lot more annoying and error prone
Brilliant, just what I needed! :dance: Quick question, what's the difference between single quotes and double quotes?
HotKeyIt
Posts: 2255
Joined: 29 Sep 2013, 18:35
Contact:

Re: (v2) Encapsulate whole GUI and event handlers inside class?

22 Apr 2020, 16:42

Scoox wrote:
22 Apr 2020, 10:25
Quick question, what's the difference between single quotes and double quotes?
AFAIK there is no difference but you can avoid the need to escape the other: Var:=" ' " . ' " '

Return to “AutoHotkey v2 Help”

Who is online

Users browsing this forum: No registered users and 5 guests