Multiple GUI Windows, and pre-declaring variables.

Get help with using AutoHotkey and its commands and hotkeys
User avatar
PaulLeavitt
Posts: 58
Joined: 21 Oct 2014, 10:04

Multiple GUI Windows, and pre-declaring variables.

29 Oct 2014, 16:00

Hello all again!
So I'm in a conundrum, I need to have a Gui window with a couple input fields come up, store both inputs as variables, and when OK is pressed, come up with a confirmation dialog with the variables in the text. But I'm stuck in two places.

Firstly, if I put all of the "GUI, Add" lines in the auto execute section, as I understand is the proper way to do it, I can't figure out how to fill the variables. Here's what I have:

Code: Select all

;GUI Box Playground.
Gui, Add, Text,, Vendor ID:
Gui, Add, Text,, Manufacturer:
Gui, Add, Edit, ym vVendor
Gui, Add, Edit, vMan
Gui, Add, Button, Default, OK

Gui, 2:Add, Text,, Vendor:"%Vendor%".`n Manufacturer:"%Man%" `n`n Is this Correct?
Gui, 2:Add, Button, Default gYes, Yes
Gui, 2:Add, Button,gNo,No

;Begin Script
#k::

Gui, Show,, Input Variables. ;The First window
Return

ButtonOK: ;Submit data, and show the second window
Gui, Submit
Gui, 2:Show,,Confirm Entry.
Return

;Subroutine for a yes
Yes:
	WinClose
	Msgbox Yea! "%Vendor%" "%Man%"
	Return

;Subroutine for a no.
No:
	WinClose
	Msgbox stincky!
	Return
Return
My Second question is this. When a window has more than one button, do all of the subsequent actions resulting from pressing one of those buttons have to go into a subroutine?

Thanks!
Paul
User avatar
trismarck
Posts: 506
Joined: 30 Sep 2013, 01:48
Location: Poland

Re: Multiple GUI Windows, and pre-declaring variables.

29 Oct 2014, 17:15

Greetings!
PaulLeavitt wrote:Firstly, if I put all of the "GUI, Add" lines in the auto execute section, as I understand is the proper way to do it, I can't figure out how to fill the variables.
The contents of the control are stored in the gui variable after doing Gui, Submit, vs at the time the control is created (at the time of doing: Gui, 2:Add, Text,, ...).
To change the text of the text control, there is GuiControl.
Related quote:
GuiControlGet ([url=http://ahkscript.org/docs/commands/GuiControlGet.htm]link[/url]) wrote:As with any other ControlID, Sub-command must also include the name or number of the GUI if it is not the default window. For example, GuiControlGet Value, 2:, %Hwnd%.
PaulLeavitt wrote:My Second question is this. When a window has more than one button, do all of the subsequent actions resulting from pressing one of those buttons have to go into a subroutine
I guess it's impossible to change the subroutine associated with a gui control once a particular subroutine was assigned to the control.

HTH.
lblb
Posts: 129
Joined: 30 Sep 2013, 11:31

Re: Multiple GUI Windows, and pre-declaring variables.

29 Oct 2014, 22:46

Hi trismarck,

You can change the subroutine associated to a control using GuiControl. For example:

Code: Select all

Gui, Add, Button, vButtonOne gHello, One
Gui, Add, Button, gSwitch_One_to_World, Switch One
Gui, Add, Button, gRemove_g_from_One, Remove One
Gui, Show
Return

Hello:
Msgbox, Hello
Return

World:
Msgbox, World!
Return

Switch_One_to_World:
GuiControl, +gWorld, ButtonOne
Return

Remove_g_from_One:
GuiControl, -g, ButtonOne
Return
User avatar
PaulLeavitt
Posts: 58
Joined: 21 Oct 2014, 10:04

Re: Multiple GUI Windows, and pre-declaring variables.

30 Oct 2014, 14:26

That makes sense Trismarck about Gui, Submit. I did some more altering, but I still can't get it to work, and I think it's because I'm not calling the variable in a MsgBox at the time of submission, but rather am calling the variable when I create the GuiBox in the autoexe section. So perhaps it isn't getting updated when Gui, Submit goes through because it's in the autoexe section? Is there a way to insert the variables inline with the text, instead of in the autoexe section?

Thanks!
Paul
User avatar
trismarck
Posts: 506
Joined: 30 Sep 2013, 01:48
Location: Poland

Re: Multiple GUI Windows, and pre-declaring variables.

30 Oct 2014, 16:00

PaulLeavitt wrote:I think it's because I'm not calling the variable in a MsgBox at the time of submission, but rather am calling the variable when I create the GuiBox in the autoexe section. So perhaps it [the variable] isn't getting updated when Gui, Submit goes through because it's in the autoexe section?
Lets suppose we're the interpreter and we execute commands in the auto-execute section. If that's the case, then when the interpreter executes Gui, Add, Edit, ym vVendor, a global variable Vendor is created dynamically and is associated with the Edit control. When this is executed: Gui, 2:Add, Text,, Vendor:"%Vendor%".`n Manufacturer:"%Man%" `n`n Is this Correct?, the contents of the variable Vendor are put between the double quotes (an empty string) and the last parameter of the Gui command then becomes a 'static' text of the text control.
Lets now suppose we've pressed Win+k, typed something into the Vendor edit box, pressed OK and as a consequence, Gui, Submit was executed. If that's the case, then _now_, vendor contains the value we've typed. So to put that value as the text of the Text control of Gui 2, GuiControl has to be used, to set a new 'static' text of the Text control in Gui 2.

Also, a variable can't 'be called'; in msgbox, %vendor% %%'s mean: the contents of the variable vendor become the first parameter of msgbox.
PaulLeavitt wrote:Is there a way to insert the variables inline with the text, instead of in the autoexe section?
Yes, with Transform,, Deref (but only in non-expression mode) and this is probably not what you want.

Code: Select all

var := "aaa"
Transform, output, Deref, % "The value of the variable is: %var%"
msgbox % output
//edit: actually, in the example, variables (the values of the variables) _are_ inserted 'inline' with the text, it's just that the evaluation of those variables happens when the auto-execute section is executed, not when Gui, Submit is executed.
lexikos
Posts: 6653
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: Multiple GUI Windows, and pre-declaring variables.

30 Oct 2014, 20:14

PaulLeavitt wrote:When a window has more than one button, do all of the subsequent actions resulting from pressing one of those buttons have to go into a subroutine?
It doesn't matter how many buttons you have. To define what a button should do, you must use a subroutine. You can use one subroutine for multiple buttons (using A_GuiControl to distinguish between them), multiple subroutine labels pointing at the same code (using A_ThisLabel to distinguish between them), or multiple distinct subroutines. You can assign a label name with the +g option, or let the button be assigned a default label name based on its text.

Return to “Ask For Help”

Who is online

Users browsing this forum: flyingDman, Gully, TheMainEvent and 68 guests