Gui puzzle similar to the postman rings always twice Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
science2002
Posts: 21
Joined: 29 Oct 2016, 05:18

Gui puzzle similar to the postman rings always twice

Post by science2002 » 07 Jul 2022, 05:08

I still use v. 1.048, and I prefer - being a randomly user - long understandable code.
The following is not a big deal but I am somewhat puzzled. Using the code below, the feedback text I get back on the GUI (or to be precise, just the Variable content), appears just when I submit the button twice. This remembers the famous title of the movie! The puzzle is underlined by the fact that a message (that I added) gives the feedback text correctly the first time after submission, not the second one.
=== Functionality of the code ====
This code is an excerpt of a bigger script. It opens a window (here it is simplified) accepting an input number. Then there is a check against a Master number (in fact I have other code that checks occurrences against other text strings). I would like that when there is no coincidence between Number inserted and Master number, in the same AHK GUI appears in a text field the Master number. This script does so, but not at the first submission, just at the second.
===Question: ====
Why? And how can it be avoided the need of the double submission?
Thank you.

Code: Select all

Gui, Add, Text, x30 y5 w170 h20 , Num_1. Occurences:
	Gui, Add, Text, x2 y25 w25 h20 , Art.1
	Gui, Add, Edit, x32 y25 w150 h20 vNum_1, Insert_N
MyLabelRewrite:
	MsgBox, The Master num %Var_MasterN%  ; for checking - when the script uses the goto, the msg shows the Master Number not so the GUI
	Gui, Add, Text, x185 y5 w300 h20 , n_ %Var_MasterN%.  ; appears just if Num e MasterNum are different. Now the Master variable number appears only the second submission - I cannot understand why
	Gui, Add, Button, x340 y25 w30 h70 gsubmitBtn, OK
	Gui, Show, x131 y91 h100 w380, New GUI Window
	Sleep, 500
	Return
submitBtn:
Gui, Submit, NoHide
Var_Num := Num_1
Var_MasterN = 1
If (Var_MasterN == Var_Num)
{
Msgbox, Ok the two numbers are the same
Gui, Destroy
}
Else
{
Goto MyLabelRewrite
}
ExitApp

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

Re: Gui puzzle similar to the postman rings always twice  Topic is solved

Post by mikeyww » 07 Jul 2022, 06:10

Trouble with those Gotos!

Per your request, I have provided you with some long and understandable code.

Code: Select all

master = 5
Gui, Font, s10
Gui, Add, Edit  , w250 vn
Gui, Add, Text  , wp   vresponse
Gui, Add, Button, wp   Default, OK
Gui, Show,, Number
Return

ButtonOK:
Gui, Submit, NoHide
If (n = master)
 ExitApp
GuiControl,, n
GuiControl,, response, Answer: %master%
GuiControl, Focus, n
Return

; More commands
; More commands
; More commands
; More commands
; More commands
; More commands
; More commands
; More commands
; More commands
; More commands
; More commands
; More commands
; More commands
; More commands
; More commands
; More commands
; More commands
; More commands
; More commands
; More commands

science2002
Posts: 21
Joined: 29 Oct 2016, 05:18

Re: Gui puzzle similar to the postman rings always twice

Post by science2002 » 07 Jul 2022, 06:34

@mikeyww many thanks!
I understand there are problems with Goto, yet I did not understand why the msgbox put on the label of the goto returns the master number correctly while the GUI doesn't, i.e. it does just the second time.

Your code works. I needed to make two changes.
1. I moved The Master_number variable below the submit label because its value is conditional to the number inserted: as I said there is other code for checking. But after moving it still works.
2. I removed at the bottom the GuiControl,, n, because it clears up the number previously inserted, while it should be left inserted to make a correction (there are more digits in the original code, so no need to redial all of them.)

Thanks again.

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

Re: Gui puzzle similar to the postman rings always twice

Post by mikeyww » 07 Jul 2022, 07:21

I did not reproduce your original problem, but glad that you have things working.

science2002
Posts: 21
Joined: 29 Oct 2016, 05:18

Re: Gui puzzle similar to the postman rings always twice

Post by science2002 » 07 Jul 2022, 08:08

Yes, and as you noticed with good humor on the second part of your code, it saves "some" lines.

Probably if you are not able to replicate the problem, it is connected with my version 1.048 of AHK (32bit) on a win10 64bit machine.
Just to answer my second question (how can be avoided) I could verify that if the following line is moved inside the "Else" part of the code, it works the first time.

Code: Select all

Gui, Add, Text, x185 y5 w300 h20 , n_ %Var_MasterN%.
I still do not know why where it was placed before it did not so.

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

Re: Gui puzzle similar to the postman rings always twice

Post by mikeyww » 07 Jul 2022, 09:35

Perhaps:
Your Goto loop continues to add new controls instead of changing existing ones-- usually not what one intends to do with a GUI. If your variable of interest is not defined for the first iteration, then nothing would appear. I do not know whether that explains it, but it could be contributing to the problem.

Post Reply

Return to “Ask for Help (v1)”