AutoHotkey Community

It is currently May 26th, 2012, 8:16 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 10 posts ] 
Author Message
PostPosted: April 15th, 2005, 8:05 pm 
Offline

Joined: January 11th, 2005, 8:12 pm
Posts: 10
Location: Orem, UT
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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 15th, 2005, 8:41 pm 
Offline
User avatar

Joined: August 11th, 2004, 1:47 am
Posts: 5347
Location: UK
Hmm, not exactly sure what you're trying to achieve. Have you tried the same without loops?

_________________
GitHubScriptsIronAHK Contact by email not private message.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 15th, 2005, 9:43 pm 
Offline

Joined: January 11th, 2005, 8:12 pm
Posts: 10
Location: Orem, UT
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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 15th, 2005, 10:40 pm 
Offline
User avatar

Joined: August 11th, 2004, 1:47 am
Posts: 5347
Location: UK
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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 16th, 2005, 12:00 am 
Offline

Joined: April 15th, 2005, 11:36 pm
Posts: 7
Location: Pakistan
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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 16th, 2005, 1:33 am 
Offline

Joined: March 2nd, 2004, 3:36 pm
Posts: 10720
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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 16th, 2005, 7:32 am 
Offline

Joined: January 31st, 2005, 9:50 am
Posts: 3910
Location: Bremen, Germany
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
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 16th, 2005, 12:39 pm 
Offline

Joined: March 2nd, 2004, 3:36 pm
Posts: 10720
Refinements are welcome, so please continue to do so whenever the mood strikes :)


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 16th, 2005, 6:57 pm 
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.


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: April 16th, 2005, 7:44 pm 
Offline

Joined: March 2nd, 2004, 3:36 pm
Posts: 10720
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


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 10 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: BrandonHotkey, poserpro, sjc1000, Yahoo [Bot] and 56 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group