Function to create GUI with arbitrary number of EDIT controls Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
freespacing
Posts: 150
Joined: 28 Sep 2016, 11:14
Contact:

Function to create GUI with arbitrary number of EDIT controls

Post by freespacing » 15 Jan 2020, 20:56

Hi all,

Here is a theoretical question
It's straightforward to made a function that creates GUIs with arbitrary numbers of EDIT boxes from one to ten (picking ten as an example).

Because it's a function, the variables that receive the input must be declared global both within the function and within the OK button's handler (also a function).

The beginning might look like this (copying from a working example I made today):

Code: Select all

gui_EditOnly( title, edit_options, optional_params) {
   global GUI_EditOnly_1, GUI_EditOnly_2, GUI_EditOnly_3, GUI_EditOnly_4, GUI_EditOnly_5
           , GUI_EditOnly_6, GUI_EditOnly_7, GUI_EditOnly_8, GUI_EditOnly_9, GUI_EditOnly_10
I'm almost sure I'll ever need more than ten fields. Even so, out of curiosity, I wonder: is there a trick to make a function that creates an arbitrary number of Edit fields? It must be a function.

As I understand, the problem is that the variable GUI_EditOnly_%field_counter% on the line below must be a global, and not an object member.

Code: Select all

Gui, EditOnly:Add, Edit, x%x_value% y%y_value% w%edit_width% vGUI_EditOnly_%field_counter%, %edit_field_default%

Just curious!
Thanks in advance for any thoughts.

swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Function to create GUI with arbitrary number of EDIT controls  Topic is solved

Post by swagfag » 15 Jan 2020, 21:16

Code: Select all

class EditControl
{
	__New(guiNameOrHwnd, options, text) {
		Gui %guiNameOrHwnd%: Add, Edit, % options " HwndhEdit", % text
		this.hwnd := hEdit
	}

	Value[] {
		get {
			GuiControlGet editText, , % this.hwnd
			return editText
		}

		set {
			GuiControl, , % this.hwnd, % value
			return value
		}
	}
}

Gui EditOnly: New
MyEdit := new EditControl("EditOnly", "", "edit default text")
Gui Show

MsgBox % MyEdit.Value
MyEdit.Value := "non default text"
MsgBox % MyEdit.Value
ExitApp
most sane approach
u can do it with functions alone too, but ull have to make GuiControl wrappers and raw handle hwnds

freespacing
Posts: 150
Joined: 28 Sep 2016, 11:14
Contact:

Re: Function to create GUI with arbitrary number of EDIT controls

Post by freespacing » 15 Jan 2020, 22:42

Wow, @swagfag, so fast and so detailed… you are a legend.

Your code is way above my level, but I'm glad about that: I will study it carefully and it will without doubt stretch my mind and ahk skills.

Will return if questions about your answer arise, in the meantime marking as solved.
Bows and thanks… wishing you a great day!

Post Reply

Return to “Ask for Help (v1)”