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.
How can I link GUI vVariables with elements of an object
Re: How can I link GUI vVariables with elements of an object
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:
Some basics are even covered in the Tutorial...
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
Re: How can I link GUI vVariables with elements of an object
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?
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
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++
}
cheers
Steve
Re: How can I link GUI vVariables with elements of an object
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).
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).
Re: How can I link GUI vVariables with elements of an object
Thanks, Lexicos, that sounds hopeful. I'm not familiar with HWNDVarName but I'll take a look at it.
- SpeedMaster
- Posts: 494
- Joined: 12 Nov 2016, 16:09
Re: How can I link GUI vVariables with elements of an object
Of course you can but you are messing up your syntax.ahkwipper wrote:Can I somehow construct the variable names in a loop?
Not a bad idea at allahkwipper wrote:In fact I started off calling them C1R1 (Column1 Row1) etc
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:
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,
Re: How can I link GUI vVariables with elements of an object
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.
Who is online
Users browsing this forum: No registered users and 343 guests