How can I link GUI vVariables with elements of an object

Get help with using AutoHotkey and its commands and hotkeys
ahkwipper
Posts: 10
Joined: 30 Jun 2018, 14:51

How can I link GUI vVariables with elements of an object

14 Jul 2018, 12:51

Hi, I have a large number of edit field variables in a GUI and I don’t really want to call them things like Var1, Var2… etc and then have to set Var1, Var2 etc in the rest of the script. I would really like to have an object myVars in the script and link the GUI edits via myVars[1], myVars[2] etc so I can use a loop to set them up and modify them.

This syntax generates an illegal character error. Searching for help, I find this was a problem back in 2006 – has it been solved yet or is there a workaround? Thanks if you can help.
User avatar
gregster
Posts: 1491
Joined: 30 Sep 2013, 06:48

Re: How can I link GUI vVariables with elements of an object

14 Jul 2018, 14:28

I think linear and associative arrays were introduced in 2009... see for example https://autohotkey.com/docs/Objects.htm and https://autohotkey.com/docs/objects/Object.htm
Some syntax examples:

Code: Select all

myVars := ["hello", 100, "world", 200 ]
msgbox % myVars[3]

for nr, item in myVars
	msgbox % nr " : " item

Obj := { 123 : "hello" , var1 : "world" , "var2" : 234 }
msgbox % obj.123 " - " obj["var1"]

for key, val in obj
	msgbox % key " : " val
Some basics are even covered in the Tutorial...
ahkwipper
Posts: 10
Joined: 30 Jun 2018, 14:51

Re: How can I link GUI vVariables with elements of an object

14 Jul 2018, 16:13

Well thanks, for that, gregster, and it all works, but I haven't been using Autohotkey very long and at the moment I'm struggling to see how it helps me with my GUI.

In my script, I have all my values placed in an object, myValues, say, by a loop and there are no keys. I have no experience of using key, value pairs. The values are just indexed by their numeric position 1 up to whatever eg

["37.2", "54", "22.5","42.6"...................]

There may be 50 numbers and I want them to go into a table of edits. As far as I can see, I must give these edit variables specific names of some sort eg

Gui, Add, Edit, x140 y50 w100 h22 cBlue vVariableName1 ReadOnlyLeft,

and so on up to VariableName50. In fact I started off calling them C1R1 (Column1 Row1) etc but decided against it

Can I somehow construct the variable names in a loop? Something like this?

Code: Select all

i := 51
j := 1
While j < i
{
n := VariableName
n := n . j
GuiControl,, n, myValues[j]
j++
}
I can try that, but I'm doubtful that is what you are trying to guide me towards. If I have to write out the names VariableName1 up to VariableName50 it will be defeating the object, as it were!

cheers

Steve
lexikos
Posts: 6207
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: How can I link GUI vVariables with elements of an object

14 Jul 2018, 19:19

Array elements are not "variables" and cannot be assigned to GUI controls.

It is not necessary to name each Edit control. You can refer to it by ClassNN or HWND. For instance, use the HWNDVarName option when creating the control to get its HWND and then copy the value of VarName into an array element. If there are no other Edit controls on the GUI, the ClassNN would be just Edit followed by the sequence number of the Edit control (Edit1 instead of VariableName1).

However, if you don't associate variables you cannot use Gui Submit; you can use GuiControlGet instead (in a loop if needed).
ahkwipper
Posts: 10
Joined: 30 Jun 2018, 14:51

Re: How can I link GUI vVariables with elements of an object

15 Jul 2018, 07:22

Thanks, Lexicos, that sounds hopeful. I'm not familiar with HWNDVarName but I'll take a look at it.
User avatar
SpeedMaster
Posts: 235
Joined: 12 Nov 2016, 16:09

Re: How can I link GUI vVariables with elements of an object

15 Jul 2018, 09:29

ahkwipper wrote:Can I somehow construct the variable names in a loop?
Of course you can but you are messing up your syntax.
ahkwipper wrote:In fact I started off calling them C1R1 (Column1 Row1) etc
Not a bad idea at all 8-) :idea: :happybday:
Don't give up, you are on the right track for linking a 2d array to controls

Here is an example on how to link 1d array to edit controls: :yawn:

Code: Select all

myvalues:=["37.2", "54", "22.5","42.6"]

VariableName:="myedit"

loop, % myvalues.length()
gui, add, edit, % "w100 v" VariableName . a_index

gui, show, autosize

for key, val in myvalues 
guicontrol,, % VariableName . key, % val

Cheers,
ahkwipper
Posts: 10
Joined: 30 Jun 2018, 14:51

Re: How can I link GUI vVariables with elements of an object

20 Jul 2018, 19:05

Thanks, Speedmaster, Sorry for the lack of reaction. I've been sidetracked by a more urgent programming task but I appreciate your input. I'll have to come back to it later.

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], Google [Bot], Hatsuko, kczx3, kentpachi and 34 guests