Page 1 of 1

Array Variables in GUI's

Posted: 23 Nov 2015, 18:14
by Peabianjay
I'd like to be able to:

Code: Select all

gui,Add, Edit, vitem.count, % item.count
Ya, I know it can be done with pseudo-arrays. Pretty sure it can't be with actual arrays.
If it can, perhaps it should be documented?
Currently, I work around it by copying an array into a pseudo-array, then back again after:

Code: Select all

for each, item in items
    guicontrol, ItemBox:, fakeit_%A_Index%, % item.count

Gui, ItemBox:Show, x1000 y300, Items
winwaitclose, Items

for each, item in items
    item.count := fakeit_%A_Index%

Re: Array Variables in GUI's

Posted: 23 Nov 2015, 19:15
by lexikos
The Gui command requires "v" followed by the name of a variable.

"item" is the name of a variable. "item.count" is not the name of a variable; in the right context, it is an expression which returns a value from an array.

The Gui command does not support pseudo-arrays. A pseudo-array is just a collection of individual variables. The Gui command accepts individual variables, by name. The name can be constructed using a variable, as with fakeit_%A_Index%, but the command sees only a variable name (e.g. "fakeit_1").

I have no plan for Gui to support arrays, but perhaps you would be interested in fincs' proposed v2 GUI API or one of the class-based GUI scripts that can be found on the forum.

Re: Array Variables in GUI's

Posted: 23 Nov 2015, 20:53
by Peabianjay
Understood.

I just don't like it the way it is.

"....[item.count] is an expression which returns a value from an array..." An important distinction. (I thought it was a direct reference to the value.)
contrasted with "[fakeit_%A_Index%] which is simply a variable name."

To be clear, my "Wish" wasn't (in either case) to refer to more than one variable/element in any one GUI command....

(1) a valid reference to a single pseudo-array variable as is currently allowed
(2) (my wish) a reference to a single element of a "real" array

Code: Select all

dog := {name:"Spot",colour:"Plaid"}

gui, add, edit, vdog.name, % vdog.name
gui, add, edit, vdog.colour, % vdog.colour
Which, as you noted, is the use of an expression rather than a variable.

So, possibly not possible.

I still wish it. :-)


[Edit to add: I'll check out that reference you provided. I'm an amateur-AHKer, but enthusiastic. :-) ]

Re: Array Variables in GUI's

Posted: 22 Dec 2015, 12:56
by TheDewd
I may not be understanding correctly...

Code: Select all

Dog := {Name: "Spot", Color: "Plaid"}

Gui, Add, Edit, % "x" 10 " y" 10 " w" 200 " h" 21 " v" Dog.Name, % "This is a test..." ; <-- Using "Dog.Name" as variable
Gui, Add, Button, % "x" 10 " y" 41 " w" 200 " h" 23 " gLabel1", % "Click Me"
Gui, Show, % "w" 220 " h" 74, % "Example"
return ; End automatic execution

Label1:
	Gui, Submit, NoHide
	MsgBox,, Result, % Spot ; <-- Using array value "Spot"
return

Re: Array Variables in GUI's

Posted: 22 Dec 2015, 14:16
by HotKeyIt
You can try AutoHotkey_H, I have recently added object support for Gui:

Code: Select all

Variable:=[]
Gui, +ObjectVariable
Gui, Add, Edit, w200 vName, AutoHotkey
Gui, Add, Button,w200 gLabel, Click Me
Gui, Show
return
GuiClose:
ExitApp
Label:
	Gui, Submit, NoHide
	MsgBox,, Result, % Variable.Name 
return
EDIT:
You can also launch a function instead of Label:

Code: Select all

Variable:=[]
Variable.MyFunc:=Func("MyFunc").Bind(Variable)
Gui, +ObjectVariable
Gui, Add, Edit, w200 vName, AutoHotkey
Gui, Add, Button,w200 gMyFunc, Click Me
Gui, Show
return
GuiClose:
ExitApp
MyFunc(this){
	Gui, SubMit, NoHide
	MsgBox % this.Name
}

Re: Array Variables in GUI's

Posted: 22 Dec 2015, 19:50
by joedf
@HotKeyIt
Hahaha AHK_H has fancy stuff!
launching a function instead? that's really cool :+1:

Re: Array Variables in GUI's

Posted: 23 Dec 2015, 09:59
by TheDewd
What if there is a function and label with the same name?

Re: Array Variables in GUI's

Posted: 23 Dec 2015, 10:53
by HotKeyIt
From Ahk_H help:
If object has no key called MyFunc or the value is not an object it will use the label MyFunc if it exists.
So function takes precedence, and it has to be defined as a key in object assigned to Gui, +Object!

Re: Array Variables in GUI's

Posted: 23 Dec 2015, 11:14
by Coco
joedf wrote:launching a function instead? that's really cool :+1:
Btw, AutoHotkey does this as well as of v1.1.20

Re: Array Variables in GUI's

Posted: 23 Dec 2015, 12:03
by joedf
@Coco Oh right! haha, thanks :)

Re: Array Variables in GUI's

Posted: 07 Feb 2016, 13:28
by Peabianjay
@HotKeyit

Very cool. Well, cool-ish. It is gonna be a pain redoing all my work around code.
I've just started looking at it, but anticipate difficulties with global variables that are NOT part of the object.

Still, MUCH MUCH better than pages of "global var1, var2, var3...." followed by copy in & copy back.

Re: Array Variables in GUI's

Posted: 07 Feb 2016, 17:21
by HotKeyIt
Peabianjay wrote:I've just started looking at it, but anticipate difficulties with global variables that are NOT part of the object.
Not sure what you mean, you can simply make the object global.

Code: Select all

global myobj:={button:Func("button").Bind()}
Gui,+Objectmyobj
Gui,Add,Edit,vEdit w200
Gui,Add,Button,gButton Default,test
Gui,Show
return
GuiClose:
ExitApp
Button(){
  Gui,Submit,NoHide
  MsgBox % myobj.edit
}
But it also works without global variables:

Code: Select all

myobj:=[]
myobj.button:=Func("button").Bind(myobj)
Gui,+Objectmyobj +hwndmyhwnd
myobj.hwnd:=myhwnd
Gui,Add,Edit,vEdit w200
Gui,Add,Button,gButton Default,test
Gui,Show
MsgBox % myobj.hwnd
return
GuiClose:
ExitApp
Button(this){
  Gui % this.hwnd ":Submit",NoHide
  MsgBox % this.edit
}

Re: Array Variables in GUI's

Posted: 07 Feb 2016, 22:41
by Peabianjay
If I understand correctly:
I can use an object.
I can even change objects (by use of Gui, -Object).
Or choose to not use objects (i.e. "the old way")
But, I can't do both (according the documentation, -Object needs to be followed by +Object)

That is, if I want to use you +Object with ONE control, then I must use it (or at least SOME object) with all controls. If I try to use an externally defined global, it'll be treated as a key within the active object.

Here's a snippet from my code "the old way"

Code: Select all

	
	loop, 8
             Gui, PlayerBox:Add, Checkbox, vbit%A_Index% , % bit%A_Index%description

	Gui, PlayerBox:Add, Edit, vPlayerlevel, % Playerlevel
	
It would be nice, if I could have all the variable (my pseudo-array) bit1, bit2....bit8 and bit1description, bit2description.... in an array. Actually, I do, but I copy them in/out of the variables before/after the GUI, which is rather clunky.

Now, I think if I can use Gui, +ObjectMyBits to solve that problem.....but won't that mess with the global variable "Playerlevel"? (That is, I don't want it to be a part of MyBits. )

And....finally....don't stress yourself out too much addressing my questions. I don't have AutoHotkey_H, yet....so haven't played around with it. Just working off this thread & the documentation. But, hey, if you're bored.....happy to hear your thoughts. :-)

Re: Array Variables in GUI's

Posted: 08 Feb 2016, 05:17
by HotKeyIt
Correct, you won't be able to assign Playerlevel to global variable if Gui uses +ObjectMyBits, Playerlevel will be a key in the object so it would need to be: Gui, PlayerBox:Add, Edit, vPlayerlevel, % MyBits.Playerlevel