none
Goto and Sub command fighting. Help? RRS feed

  • Question

  • Here is the code to my updated Shell program, Macron 64. (Download Macron 32 at TechNet...Type in "the theme to my shell") but, anyway, near the end of my Word Processor program, (in the "buttonclicked" sub loop) I have a "Goto" command, and whenever I click the "Exit" button it takes me to my desktop, but program screen freezes and repeatedly flashes. Help? (problem code in bold)

    GraphicsWindow.Clear()
    GraphicsWindow.Title="Macron 64-bit"
    Controls.ButtonClicked=buttonclick
    GraphicsWindow.CanResize="False"
    GraphicsWindow.BackgroundColor="LightGreen"
    GraphicsWindow.BrushColor="Black"
    GraphicsWindow.FontSize=15
    GraphicsWindow.Clear()
    GraphicsWindow.DrawText(5,1,"Welcome! Please type in a username!")
    loginbox=Controls.AddMultiLineTextBox(5,20)
    login=Controls.AddButton("Login",5,100)
    Sub buttonclick
      If Controls.LastClickedButton=login Then
        name=Controls.GetTextBoxText(loginbox)
        GraphicsWindow.Clear()
        GraphicsWindow.DrawText(10,10,"Welcome to Macron 64,"+" "+name+"!")
        continue1=Controls.AddButton("Continue",5,30)
      endif
      If Controls.LastClickedButton=continue1 Then
        main:
        GraphicsWindow.Clear()
        GraphicsWindow.BrushColor="Black"
        GraphicsWindow.FontSize=30
        GraphicsWindow.DrawText(5,2,name+"'s Desktop")
        GraphicsWindow.FontSize=15
        wordmachine=Controls.AddButton("Macron Word 2013",5,40)
        calculator=Controls.AddButton("Macron Calculator 2013",5,80)
        game=Controls.AddButton("Macron Game 2013",5,120)
        dictionary=Controls.AddButton("Macron Dictionary 2013",5,160)
        exit=Controls.AddButton("Exit",5,200)
      endif
      If Controls.LastClickedButton=exit Then
        GraphicsWindow.Clear()
        Program.End()
      endif
      If Controls.LastClickedButton=wordmachine Then
        GraphicsWindow.Clear()
        GraphicsWindow.FontSize=30
        GraphicsWindow.DrawText(5,1,"Macron Word 2013")
        wordbox=Controls.AddMultiLineTextBox(5,45)
        GraphicsWindow.FontSize=15
        Controls.SetSize(wordbox,500,380)
        savedoc=Controls.AddButton("Save",520,45)
        loaddoc=Controls.AddButton("Load",520,85)
        exitdoc=Controls.AddButton("Exit",520,125)
      endif
      If Controls.LastClickedButton=savedoc Then
        recallsave=Controls.GetTextBoxText(wordbox)
      endif
      If Controls.LastClickedButton=loaddoc Then
        Controls.SetTextBoxText(wordbox,recallsave)
      endif
      If Controls.LastClickedButton=exitdoc Then
        Goto main
      endif
    endsub



     
    Friday, May 10, 2013 7:41 PM

Answers

  • The only good thing about SmallBasic having a GoTo is it teaches why it should never be used!

    Seriously, try not to use it.

    Lets trace the code:

    1] you press the exitdoc

    2] it jumps to main

    3] clears the window and creates some buttons (the flicker bit)

    4] carries on down inside the buttonclick sub and the LastClickedButton is still exitdoc so it all happens again and never leaves the subroutine



    Friday, May 10, 2013 9:06 PM
    Moderator
  • Perhaps like this, import RMJ514
    Friday, May 10, 2013 9:29 PM
    Moderator

All replies

  • The only good thing about SmallBasic having a GoTo is it teaches why it should never be used!

    Seriously, try not to use it.

    Lets trace the code:

    1] you press the exitdoc

    2] it jumps to main

    3] clears the window and creates some buttons (the flicker bit)

    4] carries on down inside the buttonclick sub and the LastClickedButton is still exitdoc so it all happens again and never leaves the subroutine



    Friday, May 10, 2013 9:06 PM
    Moderator
  • how do i get back to the [desktop] now, after i press exitdoc?
    Friday, May 10, 2013 9:20 PM
  • Perhaps like this, import RMJ514
    Friday, May 10, 2013 9:29 PM
    Moderator
  • Oh, my, a little over my head, but I "doLearn" fast:)

    Thank you!

    Friday, May 10, 2013 9:48 PM
  • All I did was move the contents of the button press code to Subroutines.

    A subroutine is something that does a specific task.  In this case it sets up a screen and buttons on it.  I created 3 of them for the main layouts you have, Login, Desktop and WordMachine, but you may have one for any layout you want.

    Then call the required subroutine depending on the button pressed.

    No GoTo - there is always a better way - if you think you need one then you should really think about how it can be done better or ask.

    Friday, May 10, 2013 9:56 PM
    Moderator
  • Thank you. And I'm sure if I asked, I would be answered. So many good people here :)
    Saturday, May 11, 2013 12:09 AM