none
Graphicswindow.Clear()? RRS feed

  • Question

  • I am making a calculator that helps with finding the area of different shapes. I have made the buttons but I need help with clearing the graphics window any help? Here is my code so far. P.S. I am really new to small basic and so I might not understand everything.

    b2 = Controls.AddButton("Rectangle!", 80, 40)

    b1 =Controls.AddButton("Square!",20,10)

    b3 = Controls.AddButton("Triangle!", 80, 10)

    b4 = Controls.AddButton("Circle!", 20, 40)


    If Controls.LastButtonClicked = B1 Then

    Graphics.WindowClear

    EndIF

    Friday, March 16, 2012 9:22 PM

Answers

  • GraphicsWindow.Clear() will get rid of everything including the buttons.  To do this we need an event handler for the button click.

    In the example below only the square causes the Clear.

    b1 =Controls.AddButton("Square!",20,10)
    b2 = Controls.AddButton("Rectangle!", 80, 40)
    b3 = Controls.AddButton("Triangle!", 80, 10)
    b4 = Controls.AddButton("Circle!", 20, 40)
    
    Controls.ButtonClicked = OnButtonClicked
    
    Sub OnButtonClicked
      If Controls.LastClickedButton = b1 Then
        GraphicsWindow.Clear()
      EndIf
    EndSub
        

    Friday, March 16, 2012 9:31 PM
    Moderator

All replies

  • GraphicsWindow.Clear() will get rid of everything including the buttons.  To do this we need an event handler for the button click.

    In the example below only the square causes the Clear.

    b1 =Controls.AddButton("Square!",20,10)
    b2 = Controls.AddButton("Rectangle!", 80, 40)
    b3 = Controls.AddButton("Triangle!", 80, 10)
    b4 = Controls.AddButton("Circle!", 20, 40)
    
    Controls.ButtonClicked = OnButtonClicked
    
    Sub OnButtonClicked
      If Controls.LastClickedButton = b1 Then
        GraphicsWindow.Clear()
      EndIf
    EndSub
        

    Friday, March 16, 2012 9:31 PM
    Moderator
  • How might I add a textbox for user input and thanks so much for your quick reply!
    Friday, March 16, 2012 9:34 PM
  • Add a textbox and a button to accept the entered data, something like this:

    For i = 1 To 5
      box[i]= Controls.AddTextBox(50,50*i)
      Controls.SetSize(box[i],200,25)
      ok[i] = Controls.AddButton("Enter",270,50*i)
    EndFor
    
    Controls.ButtonClicked = OnButtonClicked
    
    Sub OnButtonClicked
      For i = 1 To 5
        If (Controls.LastClickedButton = ok[i]) Then
          GraphicsWindow.ShowMessage(Controls.GetTextBoxText(box[i]),"Box"+i)
        EndIf
      EndFor
    EndSub

    Friday, March 16, 2012 9:41 PM
    Moderator
  • hey litdev or anyone else reading this can u tell me what is wrong with this

    b1 =Controls.AddButton("Square!",20,10)


    b2 = Controls.AddButton("Rectangle!", 80, 40)


    b3 = Controls.AddButton("Triangle!", 80, 10)


    b4 = Controls.AddButton("Circle!", 20, 40)



    Controls

    .ButtonClicked = OnButtonClicked



    Sub

    OnButtonClicked


      IfControls.LastClickedButton = b1 Then


        GraphicsWindow.Clear()


        Fori = 1 To1


          box = Controls.AddTextBox(50,50*i)


          Controls.SetSize(box[i],200,25)


          Enter =  Controls.AddButton("Enter", 250, 175)


          IfControls.LastClickedButton = box Then


            Controls.GetTextBoxText(box)


            Area = box * box


            GraphicsWindow.Clear()


            GraphicsWindow.DrawBoundText(300, 300, 60, box * box)


          EndIf


        EndFor


      EndIf


      EndSub

    Sunday, March 18, 2012 1:29 AM
  • Sup CodingCrazy!

    Well, I've perused your code a little and I've found some inconsistencies: ¯\(º_o)/¯

    • At the For Loop, it runs just once, since you specified To 1. I believe you meant To 2
    • Just below it, you've used a normal variable box= Controls.AddTextBox(50,50*i). But at the following line, you use box as an array -> box[i]
    • That If Button = Enter Then is completely out of place inside the For Loop. You try to verify if the user clicked at the Enter Button inside the same block of code you create the text boxes!

    Here it is your program re-tweaked :P

    '================================================' b1= Controls.AddButton("Square!", 20, 10) b2= Controls.AddButton("Rectangle!", 80, 40) b3= Controls.AddButton("Triangle!", 80, 10) b4= Controls.AddButton("Circle!", 20, 40) Controls.ButtonClicked= OnButtonClicked '================================================' Sub OnButtonClicked Button= Controls.LastClickedButton If Button = b1 Then GraphicsWindow.Clear() For i= 1 To 2 box[i]= Controls.AddTextBox(50,50*i) Controls.SetSize(box[i] 200,25) EndFor

    Enter= Controls.AddButton("Enter" 280,175) EndIf If Button = Enter Then x= Controls.GetTextBoxText(box[1]) y= Controls.GetTextBoxText(box[2]) Area= x*y GraphicsWindow.Clear() GraphicsWindow.DrawBoundText(300,300 60 Area) EndIf EndSub '================================================'

    Sunday, March 18, 2012 4:35 AM
    Answerer
  • Sorry I don't know how to start a new thread but Im having some trouble with this code. Everything works fine its just I want to get rid of the buttons when clicked

    raphicsWindow

    .Title = "Tap The Squares"


    For

    i = 1 To10


      x = Math.GetRandomNumber(1000)


      y = Math.GetRandomNumber(500)


      Program.Delay(1500)


       b = Controls.AddButton("Alien" x, y)


    EndFor


     

    Sunday, March 18, 2012 9:01 PM
  • GraphicsWindow.Title = "Tap The Squares"


    i = 1 To10


      x = Math.GetRandomNumber(1000)


      y = Math.GetRandomNumber(500)


      Program.Delay(1500)


       b = Controls.AddButton("Alien" x, y)


    EndFor


    Controls.HideControl

    For

    Sunday, March 18, 2012 9:10 PM
  • GraphicsWindow.Title = "Tap The Squares"


    i = 1 To10


      x = Math.GetRandomNumber(1000)


      y = Math.GetRandomNumber(500)


      Program.Delay(1500)


       b = Controls.AddButton("Alien" x, y)


    EndFor


    Controls.HideControl

    For

    Your code is lacking some typing. I guess you meant something like this?
    GraphicsWindow.Title = "Tap the Squares"
    GraphicsWindow.Width = 1280
    GraphicsWindow.Height= 800
    
    For i = 1 To 10
      x = Math.GetRandomNumber(1000)
      y = Math.GetRandomNumber(500)
      b = Controls.AddButton("Alien" x, y)
      Program.Delay(100)
    EndFor
    
    Controls.HideControl(b)
    GraphicsWindow.DrawText(100,50 b)


    Sunday, March 18, 2012 10:18 PM
    Answerer
  • Well actually I wanted the buttons to disappear after the click and when I tested what you wrote it never went away + button10 has a bug but I fixed no prob
    Sunday, March 18, 2012 10:47 PM
  • CodingCrazy, I believe you haven't figured out an interesting detail that happens when you create a Controls or Shapes object.

    When Controls.AddButton() is executed, it returns a string name which is meant to be used to reffer to it.

    When the 1st button is created, it returns "Button1", 2nd button returns "Button2", 3rd "Button3" and so forth.

    You can store each of those names in a different variable each time one is created, for your comfort and ease of management.

    At the program of yours above, b = "Button10", because that was the last button created at the end of the For Loop!

    To deal w/ the other 9 previous buttons, you'll have to reffer to them using their respective names, since variable b reffers to the 10th button only!

    For example, to hide the 5th button created, you'd have to issue: Controls.HideControl("Button5").

    Controls.HideControl(b) would only hide the 10th button!

    To make things easier, you coulda used an array variable like b[], instead of just plain b.

    This way:

    b[i]= Controls.AddButton("Alien" x, y)

    Hope it helps. Happy crazy coding! :D

    Sunday, March 18, 2012 11:03 PM
    Answerer
  • Well actually I wanted the buttons to disappear after the click and when I tested what you wrote it never went away + button10 has a bug but I fixed no prob

    Well, I haven't added up anything to your original code. Just corrected enough to run it!
    Sunday, March 18, 2012 11:10 PM
    Answerer
  • Anywayz, here's a code example of what you want. Hope you study it ^_^:

    GraphicsWindow.Title=  "Tap the Squares"
    GraphicsWindow.Width=  1280
    GraphicsWindow.Height= 800
    Program.Delay(100)
    
    For number= 1 To 10
      x= Math.GetRandomNumber(1000)
      y= Math.GetRandomNumber(500)
      Controls.AddButton("Click Me!" x,y)
      Program.Delay(500)
    EndFor
    
    Controls.ButtonClicked= Disappear
    
    '++++++++++++++++++++++++++++++++++++++++++++'
    
    Sub Disappear
      
      button= Controls.LastClickedButton
      
      Controls.HideControl(button)
      
    EndSub

    As you can see here, there was no need to store any Control Button in a variable. Controls.LastClickedButton knew the name of the button clicked ;-P

    Sunday, March 18, 2012 11:36 PM
    Answerer
  • Oh my Gosh Thank You So Much You answered all of my questions I am now on track to make my game! Thanks a lot!
    Monday, March 19, 2012 12:27 AM