AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Using GUI Returns within a Loop

 
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
kimballd



Joined: 11 Jan 2005
Posts: 10
Location: Orem, UT

PostPosted: Fri Apr 15, 2005 7:05 pm    Post subject: Using GUI Returns within a Loop Reply with quote

In this code example, the "return" that is used to wait for user input via the GUI also breaks the Loop. It creates an error message when it comes to the "}" stating that there was an "unexpected end-of-block".

Code:
Loop
   {
   Gui, add, text,,loop begun
   Gui, add, button,default, &ADD
   Gui, show
   return
   ;
   ButtonADD:
   Gui, destroy
   }


This issue can be avoided by using GoTo's, but I'd prefer to use a Loop. (I encountered this issue when I tried to create a GUI that would update itself after each "ADD" with the previous Loop's GUI input.)

You also cannot place GUI returns within GoSub subroutines because they are interpreted as the end of the subroutine.

Is the "return" being interpreted incorrectly after a GUI or am I missing something? It seems to me that we would be better off having a separate command that waits for user input after a GUI is displayed.
Back to top
View user's profile Send private message
polyethene



Joined: 11 Aug 2004
Posts: 5248
Location: UK

PostPosted: Fri Apr 15, 2005 7:41 pm    Post subject: Reply with quote

Hmm, not exactly sure what you're trying to achieve. Have you tried the same without loops?
_________________
GitHubScriptsIronAHK Contact by email not private message.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
kimballd



Joined: 11 Jan 2005
Posts: 10
Location: Orem, UT

PostPosted: Fri Apr 15, 2005 8:43 pm    Post subject: Reply with quote

I was worried my previous explanation wouldn't cut it. Here is an expanded example that uses GoTo where I would like to use a Loop.


Code:
;--------------------------------------------------------------
;  Initialize Variables
;
loopcount = 1
EditboxTextCount = 0
;
;--------------------------------------------------------------
;  GUI Loop
;
addloop:
;
IfGreaterorEqual, loopcount, 2
   {
   Gui, Add, Text,, Previously Entered Text:
   Loop, %EditBoxTextCount%
      {
      Transform, EditBoxTextTemp, deref, `%EditBoxText%a_index%`%
      Gui, Add, Text,, %EditBoxTextTemp%
      }
   }
Gui, Add, Text,, Type Anything:
Gui, Add, Edit, r1 w50  vEditboxText%loopcount%
Gui, add, button,default w80, &OK
Gui, add, button,w80, Continue
Gui, show, autosize
return
;
;-----------------------
;  On "OK" button press
;
ButtonOK:
Gui, Submit
Gui, destroy
EnvAdd, loopcount, 1
EnvAdd, EditBoxTextCount, 1
goto, addloop
;
;-----------------------
;  On "Continue" button press
;
ButtonContinue:
;Do something with the array that has been built...
exitapp


The idea is to show the user what they have previously entered in the same GUI where they do the entries, and also to store their entries inside an array for other uses. If I try to replace "addloop:" with "Loop {" and "goto, addloop" with "}", you encounter an error because the GUI's "return" statement breaks the loop.

In other words, the challenge is this: keep the same functionality as the code above but do it without Goto's.
Back to top
View user's profile Send private message
polyethene



Joined: 11 Aug 2004
Posts: 5248
Location: UK

PostPosted: Fri Apr 15, 2005 9:40 pm    Post subject: Reply with quote

kimballd wrote:
If I try to replace "addloop:" with "Loop {" and "goto, addloop" with "}", you encounter an error because the GUI's "return" statement breaks the loop.

Hmm, have you tried SetTimer instead for continous loops.
_________________
GitHubScriptsIronAHK Contact by email not private message.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
DrSamRaza_1



Joined: 15 Apr 2005
Posts: 7
Location: Pakistan

PostPosted: Fri Apr 15, 2005 11:00 pm    Post subject: Reply with quote

Dear Kimballed

Please check your AutoHotkey Version.

I have latest version installed and your script is working just fine.

You may need to Tweak GUI a bit so it looks a little better. Apart from this, it is a neat, Functional script which I do not know when and how to use 'Wink'

Just kidding.
_________________
Intelligence helps, but hard work is the key to Success
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Chris
Site Admin


Joined: 02 Mar 2004
Posts: 10716

PostPosted: Sat Apr 16, 2005 12:33 am    Post subject: Reply with quote

kimballd wrote:
Here is an expanded example that uses GoTo where I would like to use a Loop.
Thanks for the example. In this case, using Goto is not a bad approach due to the unusual nature of the script, which is to repeatedly create and destroy a window for collecting more user input.

However, to avoid Goto you could modularize everything into subroutines as in this revised script, which uses Gosub to Create the GUI window each time rather than a loop:
Code:
loopcount = 1
EditboxTextCount = 0
Gosub CreateGUI
return  ; End of auto-execute section.

CreateGUI:
IfGreaterorEqual, loopcount, 2
{
   Gui, Add, Text,, Previously Entered Text:
   Loop, %EditBoxTextCount%
   {
      Transform, EditBoxTextTemp, deref, `%EditBoxText%a_index%`%
      Gui, Add, Text,, %EditBoxTextTemp%
   }
}
Gui, Add, Text,, Type Anything:
Gui, Add, Edit, r1 w50  vEditboxText%loopcount%
Gui, add, button,default w80, &OK
Gui, add, button,w80, Continue
Gui, show, autosize
return

ButtonOK:
Gui, Submit
Gui, destroy
EnvAdd, loopcount, 1
EnvAdd, EditBoxTextCount, 1
Gosub CreateGUI
return

ButtonContinue:
ExitApp
Back to top
View user's profile Send private message Send e-mail
toralf



Joined: 31 Jan 2005
Posts: 3910
Location: Bremen, Germany

PostPosted: Sat Apr 16, 2005 6:32 am    Post subject: Reply with quote

I couldn't resist and tweak it a bit further
Code:
Editcount = 0
Gosub CreateGUI
return

CreateGUI:
If (Editcount >= 1)
 {
   Gui, Add, Text,, Previously Entered Text:
   Loop, %Editcount%
    {
      var := a_index - 1
      var := EditBoxText%var%
      Gui, Add, Text,, %var%
    }
 }
Gui, Add, Text,, Type Anything:
Gui, Add, Edit, r1 w50  vEditboxText%Editcount%
Gui, add, button,default w80, &OK
Gui, add, button,w80, Continue
Gui, show
return

ButtonOK:
  Gui, Submit
  Gui, destroy
  Editcount++
  Gosub CreateGUI
return

ButtonContinue:
  ExitApp


Sorry
_________________
Ciao
toralf
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Chris
Site Admin


Joined: 02 Mar 2004
Posts: 10716

PostPosted: Sat Apr 16, 2005 11:39 am    Post subject: Reply with quote

Refinements are welcome, so please continue to do so whenever the mood strikes Smile
Back to top
View user's profile Send private message Send e-mail
Guest






PostPosted: Sat Apr 16, 2005 5:57 pm    Post subject: Reply with quote

Thank you for your suggestions.

Interesting... When you return to the "ButtonOK" subroutine from the "CreateGUI" subroutine, the "ButtonOK" subroutine isn't a subroutine any more--it functions as normal, autoexecute scripting. Thus, the "return" statement isn't treated as the end of a subroutine anymore, but instead waits for user input after showing a GUI.

In other words, calling a subroutine from within a subroutine will, on return, destroy the integrity of the former subroutine. Not to look a gift-horse in the mouth, but is this style of scripting a loop-hole or was it intended to be this way? (Yes, "loop-hole" was a pun.)

(In my programming I try not to go more than 1 subroutine deep for read-ability reasons. Some else's VB code I delt with a couple of days ago went 8 subroutines deep, and I was cursing their name all the way there and back.)

PS: Chris, you rock.
Back to top
Chris
Site Admin


Joined: 02 Mar 2004
Posts: 10716

PostPosted: Sat Apr 16, 2005 6:44 pm    Post subject: Reply with quote

Quote:
When you return to the "ButtonOK" subroutine from the "CreateGUI" subroutine, the "ButtonOK" subroutine isn't a subroutine any more--it functions as normal, autoexecute scripting. Thus, the "return" statement isn't treated as the end of a subroutine anymore, but instead waits for user input after showing a GUI.
It may seem that way, but actually it's more simple than that. When a GUI is created and shown, it isn't waiting for anything. Instead, when you click one of its buttons or trigger a g-label some other way, the OS observes that you have performed an "event". It sends that event to the process that owns the window (namely AutoHotkey.exe), which then notifies the script of the event by launching it's g-label (subroutine).

Therefore, the following two lines will execute immediately without waiting for the user to do anything:

Gosub CreateGUI
return
Back to top
View user's profile Send private message Send e-mail
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group