none
Problem with for loop and graphicswindow RRS feed

  • Question

  • Hello all, I have a problem involving my for loop and the graphics window. This is most likely a simple problem because I am new to the graphicswindow and controls function. Here is the code (below is the problem):

    Sub Enter
      Shapes.Remove(varTitle)
      Shapes.Remove(varButton2)
      varButton3 = Controls.AddButton("Continue",0,160)
      For i = 1 To 5 Step 1
        For j = 1 To 5 Step 1
          Controls.Remove(varTitle[j])
          Controls.Remove(varText[j])
        EndFor
        
        'Get friend's name
        varTitle[1]= Shapes.AddText("Enter your friend "+i+"'s name: ")
        Shapes.Move(varTitle[1],0,0)
        varText[1] = Controls.AddTextBox(0,16)
        varFriend[i] = Controls.GetTextBoxText(varText[1])
        
        'Get friend's phone number
        varTitle[2] = Shapes.AddText("Enter your friend's phone number: ")
        Shapes.Move(varTitle[2],0,48)
        varText[2] = Controls.AddTextBox(0,72)
        varPhoneNumber = Controls.GetTextBoxText(varText[2])
        arrFriend[varFriend[i]]["Phone"] = varPhoneNumber
        
        'Get friend's email address
        varTitle[3] = Shapes.AddText("Enter your friend's email address: ")
        Shapes.Move(varTitle[3],0,104)
        varText[3] = Controls.AddTextBox(0,128)
        varEmail = Controls.GetTextBoxText(varText[3])
        arrFriend[varFriend[i]]["Email"] = varEmail
        
        varTime = Clock.Time
        arrFriend[varFriend[i]]["Time"] = varTime
      EndFor
    EndSub

    I want the for loop to show "Enter your friend 1's name:", "Enter your friend 2's name:", so on and so forth until you reach the fifth friend. The problem is, the for loop seems to go straight to friend 5, showing "Enter your friend 5's name: " right when I turn it on.

    Please help me!

    P.S: I'm new to the graphicswindow and controls, and I'm trying to change the textwindow into graphicswindow. Am I doing it correctly or is there a better way?

    Monday, November 4, 2013 2:13 AM

Answers

  • Hi Greenio3

    When you write a program in the GW based on one from a TW program there are going to be differences especially in the way the program interacts with the user.

    The reason name goes straight to 5 is because the program doesn't wait for user input.

    So in the TW:

    For i = To 5
      TextWindow.Write("Enter name ")
      name[i] = TextWindow.Read()
    EndFor

    the program waits for the user to press enter before TextWindow.Read() will return anything.

    In the GW you will need to store (return) each name[i] in response to an event, like ButtonClicked. Each time the button is clicked you can increase the index i= i+ 1.

    flag = "Enter Data"  'start "Enter Data()" 
    
    button = Controls.AddButton("Next", 10, 60)
    
    message = Shapes.AddText("")
    txtBox = Controls.AddTextBox(10, 30)
    
    Controls.ButtonClicked = OnButtonClicked
    
    While "true"
      If flag = "Enter Data" Then
        EnterData()
        flag = 0
      EndIf
    EndWhile
    
    Sub OnButtonClicked
      flag = "Enter Data"
    EndSub
    
    Sub EnterData
      i = i + 1
      Shapes.SetText(message, "Enter name " + i)
      name[i] = Controls.GetTextBoxText(txtBox)
    EndSub

    Monday, November 4, 2013 9:10 PM
    Moderator
  • Adding iterator i to 16, 32 & 48 isn't enough to space them out by height for next forms!

    I believe you gotta multiply i to some GAP variable like i did in mine!

    Also, you can just show 1 form at a time, put a NEXT button to await user. Then clear screen and display next form.


    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 7, 2013 2:23 AM
    Answerer

All replies

  • This is the example I've come up with. For now I'm just gonna post it. Laters...  :D

    Run it online -> XLP892-0

    '================================================'
    ' InputBox GUI (v1.01)
    ' by GoToLoop (2013/Nov)

    ' XLP892-0

    ' http://social.msdn.microsoft.com/Forums/en-US
    '/c36749cd-673f-47a6-b5fe-1db6718038d3
    ' /problem-with-for-loop-and-graphicswindow
    '================================================'

    ' -------------------------------------------------------------------------------------------------------------'
    LF  = Text.GetCharacter(10)
    TAB = Text.GetCharacter(9)

    NUM = 5
    QUESTIONS = 3

    OFFSET = 40
    GAP = 60

    GW = 600
    GH = 400
    ' -------------------------------------------------------------------------------------------------------------'
    GraphicsWindow.CanResize = "False"
    GraphicsWindow.Width = GW
    GraphicsWindow.Height = GH
    GraphicsWindow.BackgroundColor = "DarkBlue"

    GraphicsWindow.Clear()
    CreateInputBoxes()
    HideAllBoxes()

    CreateConfirmButton()
    HideConfirmButton()

    Controls.ButtonClicked = ConfirmBox
    GraphicsWindow.KeyDown = ExitCheck

    EnterForms()
    DisplayForms()

    ClearScreen()
    HideAllBoxes()
    HideConfirmButton()

    GraphicsWindow.Hide()
    ' -------------------------------------------------------------------------------------------------------------'
    Sub CreateConfirmButton
     
      GraphicsWindow.BrushColor = "Green"
      confirmBtn = Controls.AddButton("Confirm"  (GW - 80)/2, GH - 100)
     
    EndSub
    ' -------------------------------------------------------------------------------------------------------------'
    Sub HideConfirmButton
     
      Controls.HideControl(confirmBtn)
     
    EndSub
    ' -------------------------------------------------------------------------------------------------------------'
    Sub ShowConfirmButton
     
      Controls.ShowControl(confirmBtn)
     
    EndSub
    ' -------------------------------------------------------------------------------------------------------------'
    Sub CreateInputBoxes
     
      GraphicsWindow.BrushColor = "DarkSlateGray"
     
      For i = 1 To QUESTIONS
        boxes[i] = Controls.AddTextBox(OFFSET, i*GAP)
        Controls.SetSize(boxes[i]  GW - 80, 25)
      EndFor
     
    EndSub
    ' -------------------------------------------------------------------------------------------------------------'
    Sub ClearAllBoxes
     
      For i = 1 To QUESTIONS
        Controls.SetTextBoxText(boxes[i], "")
      EndFor
     
    EndSub
    ' -------------------------------------------------------------------------------------------------------------'
    Sub HideAllBoxes
     
      For i = 1 To QUESTIONS
        Controls.HideControl(boxes[i])
      EndFor
     
    EndSub
    ' -------------------------------------------------------------------------------------------------------------'
    Sub ShowAllBoxes
     
      For i = 1 To QUESTIONS
        Controls.ShowControl(boxes[i])
      EndFor
     
    EndSub
    ' -------------------------------------------------------------------------------------------------------------'
    Sub ClearScreen
      ' Clears GraphicsWindow w/o erasing Shapes nor Controls objects.
     
      Stack.PushValue("BackgroundColor", GraphicsWindow.BrushColor)
     
      GraphicsWindow.BrushColor = GraphicsWindow.BackgroundColor
      GraphicsWindow.FillRectangle(0,0  GW,GH)
     
      GraphicsWindow.BrushColor = Stack.PopValue("BackgroundColor")
     
    EndSub
    ' -------------------------------------------------------------------------------------------------------------'
    Sub EnterForms
     
      GraphicsWindow.BrushColor = "Red"
     
      ShowAllBoxes()
      ShowConfirmButton()
     
      txt = "Enter your friend"
     
      For idx = 1 To NUM
        ClearAllBoxes()
        ClearScreen()
        
        GraphicsWindow.DrawText(OFFSET, GAP*0 + OFFSET  txt + " #" + idx + "'s name:")
        GraphicsWindow.DrawText(OFFSET, GAP*1 + OFFSET  txt + "'s phone number:")
        GraphicsWindow.DrawText(OFFSET, GAP*2 + OFFSET  txt + "'s email address:")
        
        AwaitForConfirmation()
        SaveForms()
      EndFor
     
    EndSub
    ' -------------------------------------------------------------------------------------------------------------'
    Sub SaveForms
     
      forms[idx]["Name"]  = Controls.GetTextBoxText(boxes[1])
      forms[idx]["Phone"] = Controls.GetTextBoxText(boxes[2])
      forms[idx]["Email"] = Controls.GetTextBoxText(boxes[3])
      forms[idx]["Time"]  = Clock.Time
     
    EndSub
    ' -------------------------------------------------------------------------------------------------------------'
    Sub DisplayForms
     
      fields = Array.GetAllIndices(forms[1])
     
      For idx = 1 To NUM
        arr = forms[idx]
        
        TextWindow.WriteLine("Form #" + idx + ": " + LF)
        
        For ask = 1 To QUESTIONS + 1
          elem = fields[ask]
          TextWindow.WriteLine(elem + "->"+ TAB + arr[elem])
        EndFor
        
        TextWindow.WriteLine(LF)
      EndFor
     
    EndSub
    ' -------------------------------------------------------------------------------------------------------------'
    Sub AwaitForConfirmation
     
      hasConfirmed = "False"
     
      While hasConfirmed <> "True"
        Program.Delay(100)
      EndWhile
     
    EndSub
    ' -------------------------------------------------------------------------------------------------------------'
    Sub ConfirmBox
     
      btn = Controls.GetButtonCaption(Controls.LastClickedButton)
     
      If btn = "Confirm" Then
        hasConfirmed = "True"
      EndIf
     
    EndSub
    ' -------------------------------------------------------------------------------------------------------------'
    Sub ExitCheck
     
      key = GraphicsWindow.LastKey
     
      If key = "Escape" Then
        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! (^_^)

    Monday, November 4, 2013 6:54 AM
    Answerer
  • Hi Greenio3

    When you write a program in the GW based on one from a TW program there are going to be differences especially in the way the program interacts with the user.

    The reason name goes straight to 5 is because the program doesn't wait for user input.

    So in the TW:

    For i = To 5
      TextWindow.Write("Enter name ")
      name[i] = TextWindow.Read()
    EndFor

    the program waits for the user to press enter before TextWindow.Read() will return anything.

    In the GW you will need to store (return) each name[i] in response to an event, like ButtonClicked. Each time the button is clicked you can increase the index i= i+ 1.

    flag = "Enter Data"  'start "Enter Data()" 
    
    button = Controls.AddButton("Next", 10, 60)
    
    message = Shapes.AddText("")
    txtBox = Controls.AddTextBox(10, 30)
    
    Controls.ButtonClicked = OnButtonClicked
    
    While "true"
      If flag = "Enter Data" Then
        EnterData()
        flag = 0
      EndIf
    EndWhile
    
    Sub OnButtonClicked
      flag = "Enter Data"
    EndSub
    
    Sub EnterData
      i = i + 1
      Shapes.SetText(message, "Enter name " + i)
      name[i] = Controls.GetTextBoxText(txtBox)
    EndSub

    Monday, November 4, 2013 9:10 PM
    Moderator
  • Also Greenio3

    it looks like you might have a couple of different screens for input and output, so here's a good sample of an event loop that someone else posted a while ago on this forum.


    I just found the thread where this sample was originally posted by Florian Auer: http://social.msdn.microsoft.com/Forums/en-US/08eb645a-440c-4c69-b9fc-ea79ba1b8845/please-help-me-out?forum=smallbasic
    • Edited by Jibba jModerator Tuesday, November 5, 2013 1:18 PM found original post
    Monday, November 4, 2013 9:24 PM
    Moderator
  • Excellent answer. I have another question though. If I want to show what the user entered previously (five times), would this work?

    For j = 0 To 5 Step 1
        GraphicsWindow.DrawText(0,0,"Are these correct?")
        GraphicsWindow.DrawText(0,i+16,"Name: "+arrFriend[varFriend[i]]["Name"])
        GraphicsWindow.DrawText(0,i+32,"Phone Number: "+arrFriend[varFriend[i]]["Phone"])
        GraphicsWindow.DrawText(0,i+48,"Email Address: "+arrFriend[varFriend[i]]["Email"])
      EndFor

    Thursday, November 7, 2013 1:16 AM
  • Adding iterator i to 16, 32 & 48 isn't enough to space them out by height for next forms!

    I believe you gotta multiply i to some GAP variable like i did in mine!

    Also, you can just show 1 form at a time, put a NEXT button to await user. Then clear screen and display next form.


    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 7, 2013 2:23 AM
    Answerer