GUI within a loop, controlled by a SetTimer.

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
wetware05
Posts: 750
Joined: 04 Dec 2020, 16:09

GUI within a loop, controlled by a SetTimer.

Post by wetware05 » 14 Aug 2022, 09:52

In Spain we say "I go where they don't call me" (me meto donde no me llaman). Since I gave a partial solution to aliztori, I've been working on how my script or solution could be integrated into a blucle (it may not be what aliztori wants), but I'm intrigued on how to fix it. I have not found a solution. In the script below, how to make the data entry appear ten times GUI, Show?, for example. I mean, how to integrate a loop into this script?

(The SetTimer was added because I didn't want an OK button, but instead wanted a value to be saved just by entering it into the dialog.)

Code: Select all

Settimer, CtrlGet, 250

gui, add, edit, w100 vValue
gui, show,, Edit Text
return

CtrlGet:
  guiControlGet, txtVar,, Value
  If (txtVar !="")
  {
  Msgbox, Your variable is saved with the name of txtVar: %txtVar%
  Settimer, CtrlGet, off
  gui, destroy
  }
return

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

Re: GUI within a loop, controlled by a SetTimer.

Post by mikeyww » 14 Aug 2022, 10:09

Code: Select all

Loop, 3 {
 Gui, Font, s10
 Gui, Add, Edit, w230 vtxtVar gGet
 Gui, Show, x100, Text
 WinWaitClose, Text ahk_class AutoHotkeyGUI
}
Return
Get:
Gui, Submit, NoHide
Msgbox, Your variable is saved with the name of txtVar: %txtVar%
Gui, Destroy
Return

wetware05
Posts: 750
Joined: 04 Dec 2020, 16:09

Re: GUI within a loop, controlled by a SetTimer.

Post by wetware05 » 14 Aug 2022, 10:22

ole!, the mastery of mikeyww. :bravo: I consider my mission fulfilled on this matter. Let aliztori try to integrate mikeyww's script into his own script.

mikeyww why does it work? The Gui closes when putting a single data, why? How should the hyphen be changed for a three letter/number input?

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

Re: GUI within a loop, controlled by a SetTimer.

Post by mikeyww » 14 Aug 2022, 10:25

Routine is run whenever edit control is changed. See https://www.autohotkey.com/docs/commands/GuiControls.htm#Edit
A g-label such as gMySubroutine may be listed in the control's options. This would cause the MySubroutine label to be launched automatically whenever the user or the script changes the contents of the control.

Code: Select all

Loop, 3 {
 Gui, Font, s10
 Gui, Add, Edit, w230 vtxtVar gGet
 Gui, Show, x100, Text
 WinWaitClose, Text ahk_class AutoHotkeyGUI
}
Return
Get:
Gui, Submit, NoHide
If StrLen(txtVar) < 3
 Return
Msgbox, Your variable is saved with the name of txtVar: %txtVar%
Gui, Destroy
Return
In the loop, the script waits until the GUI is closed. This occurs when the GUI is destroyed. This occurs after the message box is resolved.

wetware05
Posts: 750
Joined: 04 Dec 2020, 16:09

Re: GUI within a loop, controlled by a SetTimer.

Post by wetware05 » 14 Aug 2022, 10:47

Of course! I never would have thought that a "Gui, Add, Edit" could have a call to a subroutine. The typical OK button is not needed, with this solution.

(I don't want to open another thread to resolve the following, since part of the answer here has already been given.)

I'm working on my own script:

Code: Select all

Gui, -border -caption +toolwindow +LastFound
hI := DllCall( "LoadLibrary", Str,"RichEd20.dll" ), Gui1 := WinExist(),  W:=500, H:=30
RE1 := RichE( Gui1, 5, 15, W, H, hI )
Gui, Add, Button, x350 y9 w150 h20 gRetriveText, OK
Gui, Show, URL ; w800 h640
Return

RetriveText:
Gui +OwnDialogs
ControlGetText, RET1,, ahk_id %RE1%
Return
Data entry is "drag and drop" type. How to integrate your script within this possibility, from what I see it is not possible. The button is required. Is there any other way to integrate a drag and drop into a dialog window?

Well!, I have the line ControlGetText, RET1,, ahk_id %RE1%, maybe I can integrate it with the solution of the first post and through a SetTimer


SOTE
Posts: 1426
Joined: 15 Jun 2015, 06:21

Re: GUI within a loop, controlled by a SetTimer.

Post by SOTE » 15 Aug 2022, 03:23

mikeyww wrote:
14 Aug 2022, 10:25
In the loop, the script waits until the GUI is closed. This occurs when the GUI is destroyed. This occurs after the message box is resolved.
Good work, as usual, Mike. Something I would add, though minor, is an alternative way is to keep the labels in the same function or subroutine. Kind of like nested functions, but AHK v1 doesn't have those, so nested subroutines. The reasoning for this is because for very long programs, finding or tracking all the labels could get a bit troublesome. So if everything pertaining to a task was in one place, it helps a bit with organization. But again, only really relevant for long scripts, with say lots of lines and functions.

Edit: In this particular case, code will cause a "bug" where MsgBox shows twice. Corrected version is below in response to Mike.

Code: Select all

Loop, 3
{
	Gui, Font, s10
	Gui, Add, Edit, w230 vtxtVar gGet
	Gui, Show, x100, Text
	WinWaitClose, Text ahk_class AutoHotkeyGUI
	Get:
	{
		Gui, Submit, NoHide
		If StrLen(txtVar) >= 3
		{
			Msgbox, Your variable is saved with the name of txtVar: %txtVar%
			Gui, Destroy
		}
		Continue
	}
}
ExitApp
Last edited by SOTE on 15 Aug 2022, 11:59, edited 1 time in total.

wetware05
Posts: 750
Joined: 04 Dec 2020, 16:09

Re: GUI within a loop, controlled by a SetTimer.

Post by wetware05 » 15 Aug 2022, 05:18

SOTE, but that way you can't call the "GET" subroutine from other parts of the script, since it's nested. Isn't it?

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

Re: GUI within a loop, controlled by a SetTimer.

Post by mikeyww » 15 Aug 2022, 07:04

SOTE, thank you very much for the suggestion. I would suggest that your approach introduces a bug into the script. With your script, when the control is altered, the Get routine runs. It then runs a second time after the GUI is closed. This is unwanted behavior.

My script has no user-defined functions. Having a GUI control that calls a subroutine somewhere in the script is common. It just so happens that this call occurs inside a loop. With AHK v2, there may be some other preferred way to approach this. In my script, I have placed the Get routine immediately under the Loop. The proximity may help the user. I have eliminated a blank line between the sections to enhance the understanding that the sections are linked (though may also be confusing to some!).

I will also suggest that your script adds extraneous braces that serve no purpose.
Last edited by mikeyww on 15 Aug 2022, 07:10, edited 1 time in total.

SOTE
Posts: 1426
Joined: 15 Jun 2015, 06:21

Re: GUI within a loop, controlled by a SetTimer.

Post by SOTE » 15 Aug 2022, 07:09

wetware05 wrote:
15 Aug 2022, 05:18
SOTE, but that way you can't call the "GET" subroutine from other parts of the script, since it's nested. Isn't it?
Yes and no.

If you nest a label inside of a function, then other parts of the script won't be able to call it. It should not be a problem, because you control whether or not you want to nest a label in that way or not.

With that typed, usage of SetTimer is a bit different because it can jump to a label even if it's inside of a function. However, using SetTimer in such a way is usually not recommended, and is somewhat like careless usage of Goto.

Example of non-recommended usage:

Code: Select all

#Persistent

SetTimer, Get, 5000 ; will bypass loop
; Test() ; will execute loop
Return

Test()
{
	Loop, 3
	{
		MsgBox,,Inside Test Loop, Loop Count: %A_Index% , 2
		If (A_Index > 2)
		{
			Get:
			{
				Msgbox,, Inside Get Label, We are being naughty!
				SetTimer, Get, Off 
				ExitApp
			}
		}
	}
	Return
}

SOTE
Posts: 1426
Joined: 15 Jun 2015, 06:21

Re: GUI within a loop, controlled by a SetTimer.

Post by SOTE » 15 Aug 2022, 12:54

I will also suggest that your script adds extraneous braces that serve no purpose.
Yes, technically you are correct. However, I do that for visual recognition that a label is in there and for sometimes when I'm translating code to a different language (or AHK v2), where I know that will become a nested function. Furthermore, since you (and others) are doing K&R style all day, sorry but the extra braces are staying. :lol:
mikeyww wrote:
15 Aug 2022, 07:04
I would suggest that your approach introduces a bug into the script. With your script, when the control is altered, the Get routine runs. It then runs a second time after the GUI is closed. This is unwanted behavior.
Mike, nothing wrong with the concept of nested labels inside of functions or subroutines for AHK v1. But, you are of course right about the "bug" (MsgBox popping up twice), so the corrected version that doesn't have it is below. You have the eyes of an AHK eagle, so of course such didn't get past you.

Code: Select all

#SingleInstance, force

Loop, 3
{
	Gui, Font, s10
	Gui, Add, Edit, w230 vtxtVar gGet
	Gui, Show, x100, Text
	WinWaitClose, Text ahk_class AutoHotkeyGUI
	Get:
	{
		Gui, Submit, NoHide
		If StrLen(txtVar) >= 3
		{
			Gui, Destroy
			If A_Index > 0
			{
				Msgbox,,, The contents of txtVar is: %txtVar% `nLoop Count: %A_Index%, 5
			}
		}
		Continue
	}
}
ExitApp

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

Re: GUI within a loop, controlled by a SetTimer.

Post by mikeyww » 15 Aug 2022, 20:27

Code: Select all

Well {
 It's a different style,
 Thanks for the alternative
} Else Thanks anyway.

Post Reply

Return to “Ask for Help (v1)”