none
How to get the program to wait until my subroutine has completed? RRS feed

  • Question

  • Hi all!

    I have submitted to programs already for the Challenge of the Month - November 2012: Text Challenge 2. I have done just a text one, and a basic graphics version where the program gives you 20 seconds to input data before it saves it and clears the textbox, but I was putting it a step further and making a button event so the user can take as much time as they want to fill in the data.

    When I run "Controls.ButtonClicked = OnButtonClick" after my question, the program WON'T wait for the subroutine to be completed before going to the next question.

    My button sub:

    Sub OnButtonClick
      buttonClicked = Controls.LastClickedButton
      If buttonClicked = Enter Then
        i = i + 1
        UserAns[i] = Controls.GetTextBoxText(Resp)
        Controls.SetTextBoxText(Resp, "")
    EndIf
    EndSub

    Is there a conflict? Am I missing something? Please get back to me soon :)

    Thanks kindly!

    -JOman


    TextWindow.Write("Do you like Small Basic? Y/N: ")
    yn = TextWindow.Read()
    If yn = "Y" Then
    TextWindow.WriteLine("High-Five! You are awesome!")
    ElseIf yn = "N" Then
    TextWindow.WriteLine("Deep down inside, you like Small Basic :)")
    EndIf

    Wednesday, November 7, 2012 12:42 AM

Answers

  • Joman Mied  ,   this will works as you expected.

    The point is to use multiline textbox.

    It accept "Enter" code........ that is text.getcharacter(13)

    and if accept "Enter", it goes to next question immediately.

    GraphicsWindow.Title = "Text Challenge 2 - November 2012 (Graphic Version with Button)
    GraphicsWindow.Width = 728
    GraphicsWindow.Height = 500
    GraphicsWindow.Top = 0
    GraphicsWindow.Left = 0
    i = 0
    GraphicsWindow.BackgroundColor = "OrangeRed"
    GraphicsWindow.BrushColor = "Black"
    Resp = Controls.AddmultilineTextBox(189, 20)  '<--------
    Controls.SetSize(Resp, 300, 35)               '<-------- 
    GraphicsWindow.BrushColor = "White"
    GraphicsWindow.DrawText(80, 1, "Use the provided text box to type in your answers then hit 'Enter' to save and clear the box.")
    GraphicsWindow.DrawText(0, 45, "Questions appear here:")
    Program.Delay(5000)
    GraphicsWindow.DrawText(0, 60, "1. Welcome, What is your name?")
    GetAnswer()
    GraphicsWindow.DrawText(0, 80, "2. Hello, " + UserAns[1] + ", how are you? Great, Good, or Bad?")
    GetAnswer()
    If UserAns[2] = "Great" or UserAns[2] = "great" Then
    GraphicsWindow.DrawText(0, 80, "3. That's awesome to hear!")
    ElseIf UserAns[2] = "Good" Or UserAns[2] = "good" Then
    GraphicsWindow.DrawText(0, 80, "3. That's good to hear.")
    ElseIf UserAns[2] = "Bad" Or UserAns[2] = "bad" Then
    GraphicsWindow.DrawText(0, 80, "3. I am sorry to hear that.")
    EndIf
    GraphicsWindow.DrawText(300, 80, "What is your hobby?")
    GetAnswer()
    TextWindow.Write(UserAns)

    Sub GetAnswer
      ans=" "
      While text.IsSubText(ans,Text.GetCharacter(13))="False"  '  if   "Enter"  exist  or not
        ans=Controls.GetTextBoxText(Resp)
        Program.Delay(500)
      EndWhile
      i = i + 1
      UserAns[i] = Controls.GetTextBoxText(Resp)
      Controls.SetTextBoxText(Resp, "")
    EndSub


    • Edited by NaochanONEditor Wednesday, November 7, 2012 4:05 AM
    • Marked as answer by Joman Mied Wednesday, November 7, 2012 4:48 AM
    Wednesday, November 7, 2012 4:03 AM
    Answerer
  • Dayum. I guess some things really are impossible.


    Try  LPX272
    • Marked as answer by Joman Mied Thursday, November 8, 2012 12:14 AM
    Wednesday, November 7, 2012 7:12 AM
    Answerer

All replies

  • Hello again!

    Most of Microsoft's programming languages are based on triggered events.

    When you set a subroutine as a target for an event, that is gonna be called as many times as the event triggered.

    And there's more to it, for each triggered subroutine, an asynchronous parallel running instance of it is created. They are run at the same time as the main program instance! So they don't pause & wait for one another to complete!

    Now, what happens if the triggered subroutine is still active & running when another event occurs? You bet it, another instance of the same subroutine, all of them running at the same time!!!

    So watch out, that type of situation is prone to bugs!


    Click on "Propose As Answer" if some post solves your problem or "Vote As Helpful" if some post has been useful to you! (^_^)

    Wednesday, November 7, 2012 2:08 AM
    Answerer
  • Look at that: GoToLoop is showing me up yet again! Just kidding!

    Anyway, thanks for the tips, but are you able to provide a solution? Like, is there a way to get an "asynchronous parallel" to stop running? Can I code it a different way so that doesn't happen?


    TextWindow.Write("Do you like Small Basic? Y/N: ")
    yn = TextWindow.Read()
    If yn = "Y" Then
    TextWindow.WriteLine("High-Five! You are awesome!")
    ElseIf yn = "N" Then
    TextWindow.WriteLine("Deep down inside, you like Small Basic :)")
    EndIf

    Wednesday, November 7, 2012 2:24 AM
  • Now, how can we force a wait for them?

    There are a couple strategies for it. My favorite is have a While...EndWhile loop in the main program, which waits for a variable under the control of the event subroutine to change its value, indicating that the event had happened!

    In your example, you've got the variable buttonClicked. So it's a good candidate as a "flag" variable for the While loop to have as its exit condition:

    buttonClicked = ""
    
    While buttonClicked = ""
      Program.Delay(100)
    EndWhile

    Click on "Propose As Answer" if some post solves your problem or "Vote As Helpful" if some post has been useful to you! (^_^)

    Wednesday, November 7, 2012 2:25 AM
    Answerer
  • So, if I am following you correctly, I can assign a value ("") to buttonClicked and have a while statement watching for it to be changed? I don't know how it'll know buttonClicked has been changed again after it has been once. Know what I mean? Like, after buttonClicked has been changed once (a click of the button), how does it change the value for a second time? And, since I already have it "looking over" Controls.LastClickedButton, how can I have a second value assigned to it? I am having a hard time understanding your paradigm.

    TextWindow.Write("Do you like Small Basic? Y/N: ")
    yn = TextWindow.Read()
    If yn = "Y" Then
    TextWindow.WriteLine("High-Five! You are awesome!")
    ElseIf yn = "N" Then
    TextWindow.WriteLine("Deep down inside, you like Small Basic :)")
    EndIf

    Wednesday, November 7, 2012 2:36 AM
  • It won't know! You'd have to create as many While loops as necessary. Better yet, make a subroutine w/ that loop.

    This way, you can use WaitForButtonClick() anywhere you need it! :P

    Sub WaitForButtonClick
    
      buttonClicked = ""
    
      While buttonClicked = ""
        Program.Delay(100)
      EndWhile
    
    EndSub

    Click on "Propose As Answer" if some post solves your problem or "Vote As Helpful" if some post has been useful to you! (^_^)

    Wednesday, November 7, 2012 2:41 AM
    Answerer
  • MIND = BLOWN! And I don't mean that's amazing or unbelievable, I mean that my mind exploded trying to think of how I can accomplish that.

    OMG So confusing!

    __________________________________________________________________________________________

    I often wonder why I began programming...


    TextWindow.Write("Do you like Small Basic? Y/N: ")
    yn = TextWindow.Read()
    If yn = "Y" Then
    TextWindow.WriteLine("High-Five! You are awesome!")
    ElseIf yn = "N" Then
    TextWindow.WriteLine("Deep down inside, you like Small Basic :)")
    EndIf



    • Edited by Joman Mied Wednesday, November 7, 2012 2:47 AM What do you mean I edited it? I didn't :P
    Wednesday, November 7, 2012 2:45 AM
  • Since it's all new to you, it's gonna take some couple minutes for you to fully understand it.  :P

    See that way -> call WaitForButtonClick(), the main program will halt until buttonClicked has something in it.

    Immediately after any button is clicked inside the GraphicsWindow, a parallel instance is created for OnButtonClick. Both OnButtonClick and the While buttonClicked = "" are running at the same time.

    But what is the 1st thing that OnButtonClick does? Look -> buttonClicked = Controls.LastClickedButton

    buttonClicked is not empty anymore. And consequently, the While loop within the main instance breaks free!


    Click on "Propose As Answer" if some post solves your problem or "Vote As Helpful" if some post has been useful to you! (^_^)

    Wednesday, November 7, 2012 2:53 AM
    Answerer
  • Okay, so I should add WaitForButtonClick and that'll put me a step in the right direction, or accomplish what I set out to do?

    TextWindow.Write("Do you like Small Basic? Y/N: ")
    yn = TextWindow.Read()
    If yn = "Y" Then
    TextWindow.WriteLine("High-Five! You are awesome!")
    ElseIf yn = "N" Then
    TextWindow.WriteLine("Deep down inside, you like Small Basic :)")
    EndIf

    Wednesday, November 7, 2012 2:58 AM
  • If this helps, here is my full code (or as full as I got to so far; planning to add the rest on my questions later...) :

    '(C) 2012 by JOman Mied.
    'Basic Setup:
    GraphicsWindow.Title = "Text Challenge 2 - November 2012 (Graphic Version with Button)
    GraphicsWindow.Width = 728
    GraphicsWindow.Height = 500
    GraphicsWindow.Top = 0
    GraphicsWindow.Left = 0
    i = 0
    
    'Actual GUI setup:
    '  TextBox
    GraphicsWindow.BackgroundColor = "OrangeRed"
    GraphicsWindow.BrushColor = "Black"
    Resp = Controls.AddTextBox(189, 20)
    Controls.SetSize(Resp, 300, 25)
    '  Enter button
    GraphicsWindow.BrushColor = "Red"
    Enter = Controls.AddButton("Enter", 489, 20)
    Controls.SetSize(Enter, 50, 25)
    '  Other Text On Screen
    GraphicsWindow.BrushColor = "White"
    GraphicsWindow.DrawText(80, 1, "Use the provided text box to type in your answers then hit 'Enter' to save and clear the box.")
    GraphicsWindow.DrawText(0, 45, "Questions appear here:")
    '  15 second delay so the user can read what's been put up on the screen
    Program.Delay(15000)
    
    'Questions
    '  Question 1
    GraphicsWindow.DrawText(0, 60, "1. Welcome, What is your name?")
    Controls.ButtonClicked = OnButtonClick
    '  Question 2
    GraphicsWindow.DrawText(0, 80, "2. Hello, " + UserAns[1] + ", how are you? Great, Good, or Bad?")
    Controls.ButtonClicked = OnButtonClick
    '  Question 3
    If UserAns[2] = "Great" or UserAns[2] = "great" Then
      GraphicsWindow.DrawText(0, 80, "3. That's awesome to hear!")
    ElseIf UserAns[2] = "Good" Or UserAns[2] = "good" Then
      GraphicsWindow.DrawText(0, 80, "3. That's good to hear.")
    ElseIf UserAns[2] = "Bad" Or UserAns[2] = "bad" Then
      GraphicsWindow.DrawText(0, 80, "3. I am sorry to hear that.")
    EndIf
    Program.Delay(1000)
    GraphicsWindow.DrawText(300, 80, "What is your hobby?")
    Controls.ButtonClicked = OnButtonClick
    TextWindow.Write(UserAns)
    
    
    'Button Subroutine
    Sub OnButtonClick
      buttonClicked = Controls.LastClickedButton
      If buttonClicked = Enter Then
        i = i + 1
        UserAns[i] = Controls.GetTextBoxText(Resp)
        Controls.SetTextBoxText(Resp, "")
    EndIf
    EndSub


    TextWindow.Write("Do you like Small Basic? Y/N: ")
    yn = TextWindow.Read()
    If yn = "Y" Then
    TextWindow.WriteLine("High-Five! You are awesome!")
    ElseIf yn = "N" Then
    TextWindow.WriteLine("Deep down inside, you like Small Basic :)")
    EndIf

    Wednesday, November 7, 2012 3:00 AM
  • Hmmm. Some spots:

    • You won't be needing Program.Delay(). Instead, call WaitForButtonClick()
    • There's no need to set Controls.ButtonClicked = OnButtonClick more than once!
    • it's tiresome to keep testing whether user gonna type using upper or lower case letters! Just use Text.ConvertToLowerCase() in all answers. Like this -> UserAns[i] = Text.ConvertToLowerCase( Controls.GetTextBoxText(Resp) )

    In short, after each question, you have to wait/halt the program until the user clicks an OK/Confirm button.

    So, call WaitForButtonClick() after each one!


    Click on "Propose As Answer" if some post solves your problem or "Vote As Helpful" if some post has been useful to you! (^_^)

    Wednesday, November 7, 2012 3:10 AM
    Answerer
  • Okay, I think my brain's clicked into place here.

    I kinda want the Program.Delay() in the places I have it. It helps it slow down the pasting -er- drawing of text.

    But anyway, can I do something like:

    Sub WaitForButtonClick
      If Controls.ButtonClicked then
        OnButtonClick()
      EndIf
    End Sub
    Sub OnButtonClick
      buttonClicked = Controls.LastClickedButton
      If buttonClicked = Enter Then
        i = i + 1
        UserAns[i] = Controls.GetTextBoxText(Resp)
        Controls.SetTextBoxText(Resp, "")
    EndIf
    EndSub

    Or am I wrong?


    TextWindow.Write("Do you like Small Basic? Y/N: ")
    yn = TextWindow.Read()
    If yn = "Y" Then
    TextWindow.WriteLine("High-Five! You are awesome!")
    ElseIf yn = "N" Then
    TextWindow.WriteLine("Deep down inside, you like Small Basic :)")
    EndIf


    • Edited by Joman Mied Wednesday, November 7, 2012 3:17 AM touch-ups
    Wednesday, November 7, 2012 3:15 AM
  • I'm afraid not! Controls.ButtonClicked is neither a function nor a reading property where you could check for clicked states!

    Controls.ButtonClicked is to define which subroutine to call & create a separate running instance, whenever a button is clicked!


    Click on "Propose As Answer" if some post solves your problem or "Vote As Helpful" if some post has been useful to you! (^_^)

    Wednesday, November 7, 2012 3:25 AM
    Answerer
  • Okay, ummmm....

    What to do, what to do?

    I don't know.

    ...

    Then what should I put in the WaitForButtonClick sub?

    What can/should I change about my code so it'll be easier to manipulate?

    I am so confused...


    TextWindow.Write("Do you like Small Basic? Y/N: ")
    yn = TextWindow.Read()
    If yn = "Y" Then
    TextWindow.WriteLine("High-Five! You are awesome!")
    ElseIf yn = "N" Then
    TextWindow.WriteLine("Deep down inside, you like Small Basic :)")
    EndIf


    • Edited by Joman Mied Wednesday, November 7, 2012 3:40 AM
    Wednesday, November 7, 2012 3:39 AM
  • I am sorry I am such an idiot.

    I just can't seem to grasp what you are saying/suggesting.

    -PLUS- I don't know all the different pieces of code and their outcome. Guess I should look around more before posting my stupidity on this forum.


    TextWindow.Write("Do you like Small Basic? Y/N: ")
    yn = TextWindow.Read()
    If yn = "Y" Then
    TextWindow.WriteLine("High-Five! You are awesome!")
    ElseIf yn = "N" Then
    TextWindow.WriteLine("Deep down inside, you like Small Basic :)")
    EndIf

    Wednesday, November 7, 2012 4:00 AM
  • Then what should I put in the WaitForButtonClick sub?

    Sub WaitForButtonClick is just a code snippet. Since you'd need so many While loops (that is, after each question), to spare the constant re-typing, the best course is create a subroutine and call it anywhere you need it!

    P.S.: I'm afraid there is a bug inside Sub OnButtonClick. The line -> i = i + 1. Just follow me; if a user clicks a button 10 times, i = i + 1 gonna happen 10 times! Even though the main part of the code can still be at the 1st question!!!


    Click on "Propose As Answer" if some post solves your problem or "Vote As Helpful" if some post has been useful to you! (^_^)

    Wednesday, November 7, 2012 4:01 AM
    Answerer
  • Joman Mied  ,   this will works as you expected.

    The point is to use multiline textbox.

    It accept "Enter" code........ that is text.getcharacter(13)

    and if accept "Enter", it goes to next question immediately.

    GraphicsWindow.Title = "Text Challenge 2 - November 2012 (Graphic Version with Button)
    GraphicsWindow.Width = 728
    GraphicsWindow.Height = 500
    GraphicsWindow.Top = 0
    GraphicsWindow.Left = 0
    i = 0
    GraphicsWindow.BackgroundColor = "OrangeRed"
    GraphicsWindow.BrushColor = "Black"
    Resp = Controls.AddmultilineTextBox(189, 20)  '<--------
    Controls.SetSize(Resp, 300, 35)               '<-------- 
    GraphicsWindow.BrushColor = "White"
    GraphicsWindow.DrawText(80, 1, "Use the provided text box to type in your answers then hit 'Enter' to save and clear the box.")
    GraphicsWindow.DrawText(0, 45, "Questions appear here:")
    Program.Delay(5000)
    GraphicsWindow.DrawText(0, 60, "1. Welcome, What is your name?")
    GetAnswer()
    GraphicsWindow.DrawText(0, 80, "2. Hello, " + UserAns[1] + ", how are you? Great, Good, or Bad?")
    GetAnswer()
    If UserAns[2] = "Great" or UserAns[2] = "great" Then
    GraphicsWindow.DrawText(0, 80, "3. That's awesome to hear!")
    ElseIf UserAns[2] = "Good" Or UserAns[2] = "good" Then
    GraphicsWindow.DrawText(0, 80, "3. That's good to hear.")
    ElseIf UserAns[2] = "Bad" Or UserAns[2] = "bad" Then
    GraphicsWindow.DrawText(0, 80, "3. I am sorry to hear that.")
    EndIf
    GraphicsWindow.DrawText(300, 80, "What is your hobby?")
    GetAnswer()
    TextWindow.Write(UserAns)

    Sub GetAnswer
      ans=" "
      While text.IsSubText(ans,Text.GetCharacter(13))="False"  '  if   "Enter"  exist  or not
        ans=Controls.GetTextBoxText(Resp)
        Program.Delay(500)
      EndWhile
      i = i + 1
      UserAns[i] = Controls.GetTextBoxText(Resp)
      Controls.SetTextBoxText(Resp, "")
    EndSub


    • Edited by NaochanONEditor Wednesday, November 7, 2012 4:05 AM
    • Marked as answer by Joman Mied Wednesday, November 7, 2012 4:48 AM
    Wednesday, November 7, 2012 4:03 AM
    Answerer
  • GoToLoop~

    So I'd have to include i = i + 1 in the WaitForButtonClick or something?

    Read my post that came after the one starting w/:

    Okay, ummmm....

    What to do, what to do?


    TextWindow.Write("Do you like Small Basic? Y/N: ")
    yn = TextWindow.Read()
    If yn = "Y" Then
    TextWindow.WriteLine("High-Five! You are awesome!")
    ElseIf yn = "N" Then
    TextWindow.WriteLine("Deep down inside, you like Small Basic :)")
    EndIf


    • Edited by Joman Mied Wednesday, November 7, 2012 4:12 AM
    Wednesday, November 7, 2012 4:04 AM
  • NaochanON~

    Thanks for the suggestion!

    I had considered using a multiline textbox, but had wanted a button to make it more graphical.

    Thanks again!


    TextWindow.Write("Do you like Small Basic? Y/N: ")
    yn = TextWindow.Read()
    If yn = "Y" Then
    TextWindow.WriteLine("High-Five! You are awesome!")
    ElseIf yn = "N" Then
    TextWindow.WriteLine("Deep down inside, you like Small Basic :)")
    EndIf

    Wednesday, November 7, 2012 4:14 AM
  • Well, here's my contribution to help you out to understand the subject of triggered events:

    Sub OnButtonClick
      
      buttonClicked = Controls.LastClickedButton
      
      If buttonClicked = Enter Then
        UserAns[question] = Controls.GetTextBoxText(Resp)
        Controls.SetTextBoxText(Resp, "")
        question = 0
      EndIf
     
    EndSub Sub WaitForButtonClick buttonClicked = "" While buttonClicked = "" Program.Delay(100) EndWhile EndSub

    And here the questions modified to work w/ the subroutines:

    Controls.ButtonClicked = OnButtonClick
    
    '  Question 1
    Program.Delay(1000)
    GraphicsWindow.DrawText(0,60  "1. Welcome, What is your name?")
    Sound.PlayClick()
    question = 1
    WaitForButtonClick()
    
    
    '  Question 2
    Program.Delay(1000)
    GraphicsWindow.DrawText(0,80  "2. Hello, " + UserAns[1] + "! How are you? [Great, Good, or Bad]")
    Sound.PlayClick()
    question = 2
    WaitForButtonClick()
    
    '  Reply
    UserAns[2] = Text.ConvertToLowerCase(UserAns[2])

    If UserAns[2] = "great" Then GraphicsWindow.DrawText(0,100 "_ That's awesome to hear!") ElseIf UserAns[2] = "good" Then GraphicsWindow.DrawText(0,100 "_ That's good to hear!") ElseIf UserAns[2] = "bad" Then GraphicsWindow.DrawText(0,100 "_ I am sorry to hear that!") Else
      GraphicsWindow.DrawText(0,100  "_ Sorry! I don't follow?")
    EndIf '  Question 3
    Program.Delay(1000) GraphicsWindow.DrawText(0,120 "3. What is your hobby, " + UserAns[1] + "?") Sound.PlayClick() question = 3 WaitForButtonClick() TextWindow.WriteLine(UserAns)

    Click on "Propose As Answer" if some post solves your problem or "Vote As Helpful" if some post has been useful to you! (^_^)

    • Edited by GoToLoopEditor Wednesday, November 7, 2012 4:39 AM
    • Marked as answer by Joman Mied Wednesday, November 7, 2012 4:48 AM
    • Unmarked as answer by Joman Mied Thursday, November 8, 2012 12:01 AM
    Wednesday, November 7, 2012 4:25 AM
    Answerer
  • Okay. Now I understand...

    Wow I am retarded.


    TextWindow.Write("Do you like Small Basic? Y/N: ")
    yn = TextWindow.Read()
    If yn = "Y" Then
    TextWindow.WriteLine("High-Five! You are awesome!")
    ElseIf yn = "N" Then
    TextWindow.WriteLine("Deep down inside, you like Small Basic :)")
    EndIf


    • Edited by Joman Mied Wednesday, November 7, 2012 4:41 AM
    Wednesday, November 7, 2012 4:36 AM
  • Yeepee! Just remember: all the other trigger event types has the same scheme, they exist parallel from one another.

    It's a little hard to tame them, but you're gonna succeed in no time! :D

    I was right it would take only couple minutes to figure the subject out!


    Click on "Propose As Answer" if some post solves your problem or "Vote As Helpful" if some post has been useful to you! (^_^)

    Wednesday, November 7, 2012 4:42 AM
    Answerer
  • Hang on. Now my program is retro-fitted with your solution, but it still isn't taking button input. I tried modding it a bit, but to no avail.

    Dayum. I guess some things really are impossible.


    TextWindow.Write("Do you like Small Basic? Y/N: ")
    yn = TextWindow.Read()
    If yn = "Y" Then
    TextWindow.WriteLine("High-Five! You are awesome!")
    ElseIf yn = "N" Then
    TextWindow.WriteLine("Deep down inside, you like Small Basic :)")
    EndIf

    Wednesday, November 7, 2012 6:13 AM
  • Dayum. I guess some things really are impossible.


    Try  LPX272
    • Marked as answer by Joman Mied Thursday, November 8, 2012 12:14 AM
    Wednesday, November 7, 2012 7:12 AM
    Answerer
  • Hey Joman Mied!

    If you have your program running correctly now, that would be nice if you re-publish it at Challenge of the Month (Nov 2012)!

    P.S.: As you have already noticed, there are many approaches to deal w/ buttons and other triggered events. NaochanON showed you many alternative methods.

    For the sake of reinforcement, here's a very simple one:

    GraphicsWindow.BackgroundColor = "Orange"
    Controls.ButtonClicked = OK
    
    GraphicsWindow.BrushColor = "Black"
    textForm = Controls.AddTextBox(100,100)
    
    GraphicsWindow.FontBold = "True"
    
    GraphicsWindow.BrushColor = "Green"
    Controls.AddButton("OK"     280,95)
    
    GraphicsWindow.BrushColor = "Blue"
    Controls.AddButton("Clear"  330,95)
    
    GraphicsWindow.BrushColor = "Red"
    Controls.AddButton("Exit"   334,130)
    
    Sub OK
      
      button = Controls.GetButtonCaption( Controls.LastClickedButton )
      
      If button = "OK" Then
        GraphicsWindow.Title = Controls.GetTextBoxText(textForm)
        Sound.PlayClick()
      ElseIf button = "Clear" Then    
        Controls.SetTextBoxText(textForm, "")
        Sound.PlayBellRing()
      Else
        Sound.PlayChimeAndWait()
        Program.End()
      EndIf
      
    EndSub

    Click on "Propose As Answer" if some post solves your problem or "Vote As Helpful" if some post has been useful to you! (^_^)

    Wednesday, November 7, 2012 8:20 PM
    Answerer
  • Dayum. I guess some things really are impossible.


    Try  LPX272

    Thanks, NaochanON! That helped a lot :)

    And, you were more straight forward than GoToLoop   (-_-)

    By seeing the source code from LPX272, I understand what I did wrong.

    Thanks again!


    TextWindow.Write("Do you like Small Basic? Y/N: ")
    yn = TextWindow.Read()
    If yn = "Y" Then
    TextWindow.WriteLine("High-Five! You are awesome!")
    ElseIf yn = "N" Then
    TextWindow.WriteLine("Deep down inside, you like Small Basic :)")
    EndIf

    Thursday, November 8, 2012 12:16 AM
  • Joman Mied:

    And, you were more straight forward than GoToLoop   (-_-)

    By seeing the source code from LPX272, I understand what I did wrong.

    GoToLoop:

    There are a couple strategies for it. My favorite is have a While...EndWhile loop in the main program, which waits for a variable under the control of the event subroutine to change its value, indicating that the event had happened!

    Explanation -> In fact, I've resisted the urge to go in details about other different approaches to tackle triggered events. Instead, I've kept focus on your question -> "How to get the program to wait until my subroutine has completed?"

    That's what I've provided you -> Make a While loop which halts the program until a button is clicked!

    The other reason is that you had done nothing wrong to your code. You've just adapted your TextWindow questionnaire to work on GraphicsWindow. And was not aware how much it was to be different from the former.

    What makes one approach different from the other mostly, is how much you split the code between the main instance and the triggered instances.

    In the modifications I did to your program, I'd say about 60% of the "action" stays in the main instance, which controls the questions & replies; and 40% goes for the triggered Sub OnButtonClick, which stores & checks the button clicked, keep the answers and clears the textbox field.

    The only thing you didn't know was how to make the main portion await for the confirmation button!

    What I don't understand is that you also said that the program was not taking the button input?! I don't understand why it wasn't so, b/c it was working alright here.

    I've published only the parts I've modified. But this time I'm gonna re-publish the whole thing, which contains both the original unmodified part from you plus the parts I've tweaked. Perhaps you had some problems to paste it:

    '(C) 2012 by JOman Mied.
    'Basic Setup:
    GraphicsWindow.Title = "Text Challenge 2 - November 2012 (Graphic Version with Button)
    GraphicsWindow.Width = 728
    GraphicsWindow.Height = 500
    GraphicsWindow.Top = 0
    GraphicsWindow.Left = 0
    i = 0
    
    'Actual GUI setup:
    '  TextBox
    GraphicsWindow.BackgroundColor = "OrangeRed"
    GraphicsWindow.BrushColor = "Black"
    Resp = Controls.AddTextBox(189, 20)
    Controls.SetSize(Resp, 300, 25)
    '  Enter button
    GraphicsWindow.BrushColor = "Red"
    Enter = Controls.AddButton("Enter", 489, 20)
    Controls.SetSize(Enter, 50, 25)
    '  Other Text On Screen
    GraphicsWindow.BrushColor = "White"
    GraphicsWindow.DrawText(80, 1, "Use the provided text box to type in your answers then hit 'Enter' to save and clear the box.")
    GraphicsWindow.DrawText(0, 45, "Questions appear here:")
    
    '  Above is the original intact code  part  ========================================================================
    
    '+++++++++++++++++++++++
    
    '  Below lies the modified code part        ========================================================================
    
    Controls.ButtonClicked = OnButtonClick
    
    '  Question 1
    Program.Delay(1000)
    GraphicsWindow.DrawText(0,60  "1. Welcome, What is your name?")
    Sound.PlayClick()
    question = 1
    WaitForButtonClick()
    
    
    '  Question 2
    Program.Delay(1000)
    GraphicsWindow.DrawText(0,80  "2. Hello, " + UserAns[1] + "! How are you? [Great, Good, or Bad]")
    Sound.PlayClick()
    question = 2
    WaitForButtonClick()
    
    '  Reply
    UserAns[2] = Text.ConvertToLowerCase(UserAns[2])
    
    If UserAns[2] = "great" Then
      GraphicsWindow.DrawText(0,100  "_ That's awesome to hear!")
    ElseIf UserAns[2] = "good" Then
      GraphicsWindow.DrawText(0,100  "_ That's good to hear!")
    ElseIf UserAns[2] = "bad" Then
      GraphicsWindow.DrawText(0,100  "_ I am sorry to hear that!")
    Else
      GraphicsWindow.DrawText(0,100  "_ Sorry! I don't follow?")
    EndIf
    
    '  Question 3
    Program.Delay(1000)
    GraphicsWindow.DrawText(0,120  "3. What is your hobby, " + UserAns[1] + "?")
    Sound.PlayClick()
    question = 3
    WaitForButtonClick()
    
    TextWindow.WriteLine(UserAns)
    
    ' Modified event & while loop subroutines  ======================================================================
    
    Sub OnButtonClick
      
      buttonClicked = Controls.LastClickedButton
      
      If buttonClicked = Enter Then
        UserAns[question] = Controls.GetTextBoxText(Resp)
        Controls.SetTextBoxText(Resp, "")
        question = 0
      EndIf
      
    EndSub
    
    Sub WaitForButtonClick
      
      buttonClicked = ""
      
      While buttonClicked = ""
        Program.Delay(100)
      EndWhile
      
    EndSub
    
    '===================================================================================================

    Click on "Propose As Answer" if some post solves your problem or "Vote As Helpful" if some post has been useful to you! (^_^)

    Thursday, November 8, 2012 4:16 AM
    Answerer