GUI help needed Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Naitsirk
Posts: 40
Joined: 15 Feb 2022, 03:50

GUI help needed

Post by Naitsirk » 07 Dec 2022, 09:06

In my use of Autohotkey, we use Autohotkey for gathering certain informations.

I know how to set up a GUI, which responds to the users input.

For instance this very basic one:

Code: Select all

Sleep, 250
Gui, Destroy
Gui, Add, Text,, Age
Gui, Add, Edit, vAge, 
Gui, Add, Text,, Gender:
Gui, Add, DropDownList, vGender, Male|Female|Nonbinary|
Gui, Add, Button, default, OK  
Gui, Show,, Test
return  

GuiClose:
ButtonOK:
Gui, Submit 

Sleep, 250
If (Gender = "Male")
{
Sleep, 250
Gui, 2:Destroy
Gui, 2:Add, Text, xm y+10 w170, How cool are you?
Gui, 2:Add, DropDownList, x+m w168 vC1, Mildly cool|Very cool|
Gui, 2:Add, Button, default, OK
Gui, 2:Show,, Male
return  

2GuiClose:
2ButtonOK:
Gui, 2:Submit 
ExitApp
}

If (Gender = "Female")
{
Sleep, 250
Gui, 3:Destroy
Gui, 3:Add, Text, xm y+10 w170, How cool are you?
Gui, 3:Add, DropDownList, x+m w168 vC2, Mildly cool|Very cool|
Gui, 3:Add, Button, default, OK
Gui, 3:Show,, Female
return  

3GuiClose:
3ButtonOK:
Gui, 3:Submit 
ExitApp
}

If (Gender = "Nonbinary")
{
Sleep, 250
Gui, 4:Destroy
Gui, 4:Add, Text, xm y+10 w170, How cool are you?
Gui, 4:Add, DropDownList, x+m w168 vC3, Mildly cool|Very cool|
Gui, 4:Add, Button, default, OK
Gui, 4:Show,, Nonbinary
return  

4GuiClose:
4ButtonOK:
Gui, 4:Submit 
ExitApp
}
But my trouble comes if I want to add another "layer" a GUI, and wishes the GUI to "respond" to an input again.

For instance in GUI2. The user is asked "How cool are you?" I want the script to respond to this input by opening another GUI - but I can't. Take this example:

Code: Select all

Sleep, 250
Gui, Destroy
Gui, Add, Text,, Age
Gui, Add, Edit, vAge, 
Gui, Add, Text,, Gender:
Gui, Add, DropDownList, vGender, Male|Female|Nonbinary|
Gui, Add, Button, default, OK  
Gui, Show,, Test
return  

GuiClose:
ButtonOK:
Gui, Submit 

Sleep, 250
If (Gender = "Male")
{
Sleep, 250
Gui, 2:Destroy
Gui, 2:Add, Text, xm y+10 w170, How cool are you?
Gui, 2:Add, DropDownList, x+m w168 vC1, Mildly cool|Very cool|
Gui, 2:Add, Button, default, OK
Gui, 2:Show,, Male
return  

2GuiClose:
2ButtonOK:
Gui, 2:Submit 

Sleep, 250
If (C1 = Mildly cool)
{
Sleep, 250
Gui, 5:Destroy
Gui, 5:Add, Text, xm y+10 w170, Are you ok with that?
Gui, 5:Add, DropDownList, x+m w168 vO1, Yes|No|
Gui, 5:Add, Button, default, OK
Gui, 5:Show,, Mildly cool male
return  

5GuiClose:
5ButtonOK:
Gui, 5:Submit 
}
ExitApp
}

If (Gender = "Female")
{
Sleep, 250
Gui, 3:Destroy
Gui, 3:Add, Text, xm y+10 w170, How cool are you?
Gui, 3:Add, DropDownList, x+m w168 vC2, Mildly cool|Very cool|
Gui, 3:Add, Button, default, OK
Gui, 3:Show,, Female
return  

3GuiClose:
3ButtonOK:
Gui, 3:Submit 
ExitApp
}

If (Gender = "Nonbinary")
{
Sleep, 250
Gui, 4:Destroy
Gui, 4:Add, Text, xm y+10 w170, How cool are you?
Gui, 4:Add, DropDownList, x+m w168 vC3, Mildly cool|Very cool|
Gui, 4:Add, Button, default, OK
Gui, 4:Show,, Nonbinary
return  

4GuiClose:
4ButtonOK:
Gui, 4:Submit 
ExitApp
}
In line 31 I've written "If (C1 = Mildly cool)", but why does the GUI not recognize this and lead the user to GUI 5?

Let it be known that the above examples are not related to the use we have of Autohotkey, but just an example to help me understand, what I am doing wrong... :D

Any help will be much appreciated.

User avatar
mikeyww
Posts: 26931
Joined: 09 Sep 2014, 18:38

Re: GUI help needed

Post by mikeyww » 07 Dec 2022, 09:11

You quoted your literal strings in other expressions but not there. Why the difference?

Naitsirk
Posts: 40
Joined: 15 Feb 2022, 03:50

Re: GUI help needed

Post by Naitsirk » 07 Dec 2022, 09:18

mikeyww wrote:
07 Dec 2022, 09:11
You quoted your literal strings in other expressions but not there. Why the difference?
Sorry, I dont understand what you mean?

User avatar
mikeyww
Posts: 26931
Joined: 09 Sep 2014, 18:38

Re: GUI help needed

Post by mikeyww » 07 Dec 2022, 11:10

When I say, "quote", I mean, "to put quotation marks around [a section of text]".

You can see differences in your script as follows.

Code: Select all

(Gender = "Female")
vs.

Code: Select all

(C1 = Mildly cool)
Explained: Expressions
Literal strings must be enclosed in double quotes to distinguish them from variables.

CarlosPeredaMieses
Posts: 2
Joined: 09 Nov 2022, 01:21

Re: GUI help needed

Post by CarlosPeredaMieses » 07 Dec 2022, 13:02

Hi, Naitsirk

Everything you put within parentheses in a if condition is an expression. That means that ahk tries to interpret unquoted text as variables, not strings. Since Mildly and cool are treated as undefined variables, ahk assigns them an empty value: "". Therefore, at line 34 ahk is trying to compare C1 with "" which is going to be false.

You can make GUI 5 work if you don't use parentheses (because the condition is not an expression anymore and unquoted text is treated as strings).

Code: Select all

If C1=Mildly cool
or better yet, you could wrap Mydly cool with quotes whenever you are using parenthesis (expressions).

Code: Select all

If (C1 = "Mildly cool")
This is something that ahkv1 always confused me. Fortunately, ahk v2 uses a more consistent and restrictive syntax that eliminates this confusion.

Leafk
Posts: 30
Joined: 11 Nov 2022, 22:09

Re: GUI help needed  Topic is solved

Post by Leafk » 07 Dec 2022, 13:32

From what I can see your code is doing some weird jumps because of the placement of the return and g-label (label need to be out of the block).

Code: Select all

...
	return
}
GuiClose: ;This is a g-label
ButtonOK:  ;This is a g-label
Gui, Submit 
Here a different aproach, to the same 3 gui workflow (with less repeated gui's) see if it helps.
I've thrown some new commands (Switch ,OR ,and gui title using variables), in your code you can pick whatever you think is easier to write and read in your version.

Code: Select all

Gui, Destroy
Gui, Add, Text,, Age
Gui, Add, Edit, vAge, 
Gui, Add, Text,, Gender:
Gui, Add, DropDownList, vGender, Male|Female|Nonbinary|
Gui, Add, Button, default, OK  
Gui, Show,, Test
return ;stop the code

;Resume from button click on Gui from here
GuiClose:
ButtonOK:
Gui, Submit 

Gui, 2:Destroy
Gui, 2:Add, Text, xm y+10 w170, How cool are you? ;Creating shared element on Gui 2 for all options
Switch Gender { ;Other than switch, you can do also with simple ifs
	Case "Male","Female":
		Gui, 2:Add, DropDownList, x+m w168 vC1, Mildly cool|Very cool|
		Gui, 2:Add, Button, default, OK
	Case "Nonbinary":
		Gui, 2:Add, DropDownList, x+m w168 vC1, Mildly cool|Very cool|
		Gui, 2:Add, Button, default, OK
	Default:
		ignoreGui2 := true
}
if (!ignoreGui2)
	Gui, 2:Show, , %Gender% ;Gui title = Gender (Male/Female/Nonbinary)
return   ;stop the code

;Resume from button click on Gui, 2 from here
2GuiClose:
2ButtonOK:
Gui, 2:Submit 

If ((C1 == "Mildly cool") or (C1 == "Very cool")) {
	Gui, 5:Destroy
	Gui, 5:Add, Text, xm y+10 w170, Are you ok with that?
	Gui, 5:Add, DropDownList, x+m w168 vO1, Yes|No|
	Gui, 5:Add, Button, default, OK
	StringLower, GenderLower ,Gender
	Gui, 5:Show,, % C1 " " GenderLower
}
return  ;stop the code

;Resume from button click on Gui, 5 from here
5GuiClose:
5ButtonOK:
Gui, 5:Submit 


MsgBox, End of code
return

Naitsirk
Posts: 40
Joined: 15 Feb 2022, 03:50

Re: GUI help needed

Post by Naitsirk » 07 Dec 2022, 13:45

Leafk wrote:
07 Dec 2022, 13:32
From what I can see your code is doing some weird jumps because of the placement of the return and g-label (label need to be out of the block).

Code: Select all

...
	return
}
GuiClose: ;This is a g-label
ButtonOK:  ;This is a g-label
Gui, Submit 
Here a different aproach, to the same 3 gui workflow (with less repeated gui's) see if it helps.
I've thrown some new commands (Switch ,OR ,and gui title using variables), in your code you can pick whatever you think is easier to write and read in your version.

Code: Select all

Gui, Destroy
Gui, Add, Text,, Age
Gui, Add, Edit, vAge, 
Gui, Add, Text,, Gender:
Gui, Add, DropDownList, vGender, Male|Female|Nonbinary|
Gui, Add, Button, default, OK  
Gui, Show,, Test
return ;stop the code

;Resume from button click on Gui from here
GuiClose:
ButtonOK:
Gui, Submit 

Gui, 2:Destroy
Gui, 2:Add, Text, xm y+10 w170, How cool are you? ;Creating shared element on Gui 2 for all options
Switch Gender { ;Other than switch, you can do also with simple ifs
	Case "Male","Female":
		Gui, 2:Add, DropDownList, x+m w168 vC1, Mildly cool|Very cool|
		Gui, 2:Add, Button, default, OK
	Case "Nonbinary":
		Gui, 2:Add, DropDownList, x+m w168 vC1, Mildly cool|Very cool|
		Gui, 2:Add, Button, default, OK
	Default:
		ignoreGui2 := true
}
if (!ignoreGui2)
	Gui, 2:Show, , %Gender% ;Gui title = Gender (Male/Female/Nonbinary)
return   ;stop the code

;Resume from button click on Gui, 2 from here
2GuiClose:
2ButtonOK:
Gui, 2:Submit 

If ((C1 == "Mildly cool") or (C1 == "Very cool")) {
	Gui, 5:Destroy
	Gui, 5:Add, Text, xm y+10 w170, Are you ok with that?
	Gui, 5:Add, DropDownList, x+m w168 vO1, Yes|No|
	Gui, 5:Add, Button, default, OK
	StringLower, GenderLower ,Gender
	Gui, 5:Show,, % C1 " " GenderLower
}
return  ;stop the code

;Resume from button click on Gui, 5 from here
5GuiClose:
5ButtonOK:
Gui, 5:Submit 


MsgBox, End of code
return
Thanks for your input, which I will definitely look at that.

I guess my issue is, that the questions will vary, depending in what input in giving in the initial GUI's, so this simply GUI was just an example.

Again, this is nothing to do with gender, but something else. But in this sense, the male would receive one set of questions, not given to the female of binary, and the same for female and binary ... hope is makes sence :)

Leafk
Posts: 30
Joined: 11 Nov 2022, 22:09

Re: GUI help needed

Post by Leafk » 07 Dec 2022, 14:07

Yeah I got it.
But is better (at least to me) to create one gui and insert it's controls diferently based on the gender (this is the idea of switch).
With my logic you have 3 gui's to work with (first=initial gui ,second=gender selection ,third=coolness? selection )

Using the switch you can do your code like that.

Code: Select all

Gui, 2:Destroy
Switch Gender { ;Other than switch, you can do also with simple ifs
	Case "Male":
		Gui, 2:Add, Text, xm y+10 w170, How cool are you?
		Gui, 2:Add, DropDownList, x+m w168 vC1, Mildly cool|Very cool|
		Gui, 2:Add, Button, default, OK
		gui2Title := "Male"
	Case "Female":
		Gui, 2:Add, Text, xm y+10 w170, How cool are you?
		Gui, 2:Add, DropDownList, x+m w168 vC1, Mildly cool|Very cool|
		Gui, 2:Add, Button, default, OK
		gui2Title := "Female"
	Case "Nonbinary":
		Gui, 2:Add, Text, xm y+10 w170, How cool are you?
		Gui, 2:Add, DropDownList, x+m w168 vC1, Mildly cool|Very cool|
		Gui, 2:Add, Button, default, OK
		gui2Title := "Nonbinary"
	Default:
		ignoreGui2 := true
}
if (!ignoreGui2)
	Gui, 2:Show, , %gui2Title%
return   ;stop the code
Btw. A good tool to debug your code workflow (and I do all time) is throwing one messagebox where you want your code to pass, if msgbox never shows your code is failing before it. If it shows and your code is not running as expected, throw the msgbox further away from the start (rinse and repeat).

Naitsirk
Posts: 40
Joined: 15 Feb 2022, 03:50

Re: GUI help needed

Post by Naitsirk » 07 Dec 2022, 14:55

Leafk wrote:
07 Dec 2022, 14:07
Yeah I got it.
But is better (at least to me) to create one gui and insert it's controls diferently based on the gender (this is the idea of switch).
With my logic you have 3 gui's to work with (first=initial gui ,second=gender selection ,third=coolness? selection )

Using the switch you can do your code like that.

Code: Select all

Gui, 2:Destroy
Switch Gender { ;Other than switch, you can do also with simple ifs
	Case "Male":
		Gui, 2:Add, Text, xm y+10 w170, How cool are you?
		Gui, 2:Add, DropDownList, x+m w168 vC1, Mildly cool|Very cool|
		Gui, 2:Add, Button, default, OK
		gui2Title := "Male"
	Case "Female":
		Gui, 2:Add, Text, xm y+10 w170, How cool are you?
		Gui, 2:Add, DropDownList, x+m w168 vC1, Mildly cool|Very cool|
		Gui, 2:Add, Button, default, OK
		gui2Title := "Female"
	Case "Nonbinary":
		Gui, 2:Add, Text, xm y+10 w170, How cool are you?
		Gui, 2:Add, DropDownList, x+m w168 vC1, Mildly cool|Very cool|
		Gui, 2:Add, Button, default, OK
		gui2Title := "Nonbinary"
	Default:
		ignoreGui2 := true
}
if (!ignoreGui2)
	Gui, 2:Show, , %gui2Title%
return   ;stop the code
Btw. A good tool to debug your code workflow (and I do all time) is throwing one messagebox where you want your code to pass, if msgbox never shows your code is failing before it. If it shows and your code is not running as expected, throw the msgbox further away from the start (rinse and repeat).
Good point, will do this also. Thanks man!

Post Reply

Return to “Ask for Help (v1)”